这里做一个springboot整合Mybatis-plus小demo项目,方便以后在某些项目中用到
项目的结构如图
001.png 002.png
下面贴上详细的配置或代码
1 build.gradle,jar依赖
plugins {
id 'org.springframework.boot' version '2.2.6.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
}
group = 'com.example'
version = '0.0.1'
sourceCompatibility = '1.8'
repositories {
maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/' }
maven{ url 'http://maven.aliyun.com/nexus/content/repositories/jcenter'}
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
compile group: 'com.baomidou', name: 'mybatis-plus-boot-starter', version: '3.3.1'
compile group: 'com.zaxxer', name: 'HikariCP', version: '3.4.2'
runtimeOnly 'mysql:mysql-connector-java'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
test {
useJUnitPlatform()
}
2 application.yml,配置数据源
spring:
datasource:
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.1.188:3306/db_demo
username: u_demo
password: u_demo_123456
hikari:
connection-timeout: 30000
auto-commit: true
max-lifetime: 1800000
pool-name: DatebookHikariCP
minimum-idle: 5
connection-test-query: SELECT 1
idle-timeout: 30000
maximum-pool-size: 15
3 model类,对应关t_demo表
@TableName("t_demo")
public class Demo {
private Long id;
private String name;
private int sex;
private String userNo;
private int age;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
public String getUserNo() {
return userNo;
}
public void setUserNo(String userNo) {
this.userNo = userNo;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
4 dao接口
package com.example.demo.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.model.Demo;
import org.apache.ibatis.annotations.Mapper;
/**
* @description:
* @author:xx@bb.com
* @time:2020/4/20 17:06
**/
@Mapper
public interface DemoMapper extends BaseMapper<Demo> {}
5 service接口实现类,service接口就两个方法,这里不贴了
package com.example.demo.service.impl;
import com.example.demo.dao.DemoMapper;
import com.example.demo.model.Demo;
import com.example.demo.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
/**
* @description:
* @author:xx@bb.com
* @time:2020/4/20 17:13
**/
@Service
public class DemoServiceImpl implements DemoService {
@Autowired
private DemoMapper demoMapper;
@Transactional
@Override
public void create(Demo demo) {
demoMapper.insert(demo);
}
@Override
public List<Demo> getList() {
return demoMapper.selectList(null);
}
}
6 controller请求类
package com.example.demo.controller;
import com.example.demo.model.Demo;
import com.example.demo.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @description:
* @author:xx@bb.com
* @time:2020/4/20 16:36
**/
@RestController
@RequestMapping("/hello")
public class IndexController {
@Autowired
private DemoService demoService;
@GetMapping("index")
public String index() {
return "Hello demo...";
}
@GetMapping("create")
public String create() {
Demo demo = new Demo();
demo.setName("xx");
demo.setUserNo("0001");
demoService.create(demo);
return "ok";
}
@GetMapping("size")
public int size() {
List<Demo> demoList = demoService.getList();
return null == demoList ? 0 : demoList.size();
}
}
7 Springboot的启动类,@MapperScan注解很重要
package com.example.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.**.dao")
public class Starter {
public static void main(String[] args) {
SpringApplication.run(Starter.class, args);
}
}
8 t_demo表的建表sql语句,一个简单的表
CREATE TABLE `t_demo` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(100) DEFAULT NULL,
`sex` int(11) DEFAULT '0',
`user_no` varchar(100) DEFAULT NULL,
`age` int(11) DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
好了,到此结束,希望也帮到需要的朋友:)
网友评论