现在业界互联网流行的数据操作层框架 Mybatis,下面详解下 Springboot 如何整合 Mybatis ,这边没有使用 Mybatis Annotation 这种,是使用 xml 配置 SQL。
使用spring boot 的starter pom,需要导入 mybatis-spring-boot-starter 和 数据库连接相关的配置。
这里采用的是阿里巴巴的druid数据连接池。
pom.xml:
mysql
mysql-connector-java
runtime
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.2
com.alibaba
druid
1.0.20
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
org.mybatis.generator
mybatis-generator-maven-plugin
1.3.2
true
true
项目不使用application.properties文件 而使用更加简洁的application.yml文件:
将原有的resource文件夹下的application.properties文件删除,创建一个新的application.yml配置文件,
文件的内容如下:
server:
port: 8080
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/test
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
# 下面为连接池的补充设置,应用到上面所有数据源中
# 初始化大小,最小,最大
dbcp2:
initial-size: 1 # 初始化大小
max-idle: 20 # 最大
min-idle: 1 # 最小
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
time-between-eviction-runs-millis: 60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
min-evictable-idle-time-millis: 300000
validation-query: select 'x'
test-while-idle: true
test-on-borrow: false
test-on-return: false
pool-prepared-statements: true
max-open-prepared-statements: 20
mybatis:
#注意:一定要对应mapper映射xml文件的所在路径
mapper-locations: classpath:mapper/*.xml
# 注意:对应实体类的路径
type-aliases-package: com.example.demo.model
3.1 首先创建user表
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`age` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8
3.2 使用mybatis generator 自动生成代码
参考方法:https://blog.csdn.net/cllaure/article/details/81483858
最后生成的文件以及结构:
4.1 在启动类中,加入注解 @MapperScan,这个对应了项目中mapper(dao)所对应的包路径。
package com.example.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.example.demo.dao")//将项目中对应的mapper类的路径加进来就可以了
public class SpringbootMybatisDruidApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootMybatisDruidApplication.class, args);
}
}
4.2 测试Controller
package com.example.demo.controller;
import com.example.demo.model.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(value = "/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping(value = "/addUser")
public int addUser(User user){
return userService.addUser(user);
}
@RequestMapping(value = "/getUser/{id}")
public User getUser(@PathVariable Integer id){
return userService.getUser(id);
}
}
package com.example.demo.service;
import com.example.demo.model.User;
public interface UserService {
int addUser(User user);
User getUser(Integer id);
}
package com.example.demo.service.impl;
import com.example.demo.dao.UserMapper;
import com.example.demo.model.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService{
@Autowired
private UserMapper mapper;
@Override
public int addUser(User user) {
return mapper.insert(user);
}
@Override
public User getUser(Integer id) {
return mapper.selectByPrimaryKey(id);
}
}
我们可以看到,新增已经成功了。
其余类似,这里不做演示。
Spring boot Mybatis 整合(完整版)