Spring Boot整合Mybatis之通用Mapper
Jdk:1.8
idea:2018
Spring Boot :2.0.6
Mysql:8.0
前期准备
数据库驱动
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
连接池及设置
这个依赖不是必须的:如果使用了通用Mapper的话 则不必要引入
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
在配置文件中配置数据源
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/login?useUnicode=true&characterEncoding=utf8&useSSL=false
username: root
password: mysql123
##通用Mapper
官方文档 https://github.com/abel533/Mapper/wiki/1.3-spring-boot
引入通用Mapper
注意:引入该 starter 时,和 MyBatis 官方的 starter 没有冲突,但是官方的自动配置不会生效!
<!--若使用类通用mapper 则不需要设置Mybatis的启动器spring-boot-starter-jdbc 甚至spring-boot-starter -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.0.4</version>
</dependency>
配置Mapper
####@MapperScan 注解自动配置
你可以给带有 @Configuration
的类配置该注解,或者直接配置到 Spring Boot 的启动类上,如下:
@tk.mybatis.spring.annotation.MapperScan(basePackages = "扫描包")
@SpringBootApplication
public class SampleMapperApplication {}
注意:这里使用的 tk.mybatis.spring.annotation.MapperScan !
@Mapper 注解配置
如果你需要对通用 Mapper 进行配置,你可以在 Spring Boot 的配置文件中配置 mapper.
前缀的配置。
例如在 yml 格式中配置:
mapper:
mappers:
- tk.mybatis.mapper.common.Mapper
- tk.mybatis.mapper.common.Mapper2
notEmpty: true
使用
创建pojo
User.java
@Data
@Table(name = "user")
public class User {
@Id
@KeySql(useGeneratedKeys = true)
private Integer id;
private String name;
private String password;
private Address address;
}
Address.java
@Data
@Table(name = "address")
public class Address {
@Id
@KeySql(useGeneratedKeys = true)
private int id;
private String city;
}
创建Mapper
这里是Spring Jpa 的风格使用Mybatis,也可以使用自己写的XXXMapper.java及XXXMapper.xml的传统Mybatis方式
UserMapper.java
public interface UserMapper extends Mapper<User> {
}
新建测试类
需要提前引入
<!--测试-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
UserMapperTest.java
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserMapperTest {
@Autowired
private UserMapper userMapper;
@Test
public void testSelectByPrimaryKey(){
User user = userMapper.selectByPrimaryKey(2);
System.out.println(user);
}
}
纯Mybatis
pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.study.wxstudy</groupId>
<artifactId>com.study.wxstudy</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.0.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.0.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.22</version>
</dependency>
<!--jdbc相关-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<version>2.0.6.RELEASE</version>
</dependency>
<!--数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.12</version>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
</project>
配置文件
server:
port: 10088
spring:
datasource:
username: root
password: mysql
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://59.110.xx.15:3306/wxstudy?useUnicode=true&characterEncoding=utf8&useSSL=false
mybatis:
type-aliases-package: com.an.entities #实体的位置
mapper-locations: mapper/*.xml #mapper位置
启动类
//添加@MapperScan("com.an.mapper")注解
@org.springframework.boot.autoconfigure.SpringBootApplication
@MapperScan("com.an.mapper")
public class SpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootApplication.class);
}
}