前言
上一篇博客中,我们完成了springboot
使用spring data jpa
但是在我们实际工作中,可能大部分的同学还是使用mybatis
比较多,所以今天我们在这里实现一下springboot
使用mybatis
实现对user
表的增删改查并且进行单元测试
实现
1、添加mybaits
依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
并且在yml
中配置一下数据源跟mybatis的配置。
server:
port: 8989
mybatis:
mapper-locations: classpath:/mybaits/mapper/*.xml
config-location: classpath:/mybatis/config/mybatis-config.xml
spring:
datasource:
username: root
password: root
url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8
driver-class-name: com.mysql.jdbc.Driver
在上面的配置中
mybatis:
mapper-locations: classpath:/mybaits/mapper/*.xml
config-location: classpath:/mybatis/config/mybatis-config.xml
mapper-locations