0%

SpringBoot-整合Mybaits通用Mapper

前言

通用Mapper就是为了解决单表增删改查,基于Mybatis的插件。开发人员不需要编写SQL,不需要在DAO中增加方法,只要写好实体类,就能支持相应的增删改查方法。(类似JPA)

pom.xml文件

添加web、mapper、mysql和lombok依赖。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<properties>
<java.version>1.8</java.version>
<mybatis.mapper.version>2.1.4</mybatis.mapper.version>
</properties>

<dependencies>
<!--web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- 通用Mapper -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>${mybatis.mapper.version}</version>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>

<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

配置文件

mapper参数配置可参考mapper配置介绍

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
spring:
#DataSource Config
datasource:
url: jdbc:mysql://localhost:3306/test?serverTimezone=GMT
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver

mapper:
mappers:
- tk.mybatis.mapper.common.Mapper #mapper的父接口
- com.huzh.springbootmybatismapper.common.CommonMapper #自定义mapper的父接口
not-empty: true #insert和update中,是否判断字符串类型!='',少数方法会用到
identity: mysql
enum-as-simple-type: true # 允许bean 接受 enum 类型

自定义mapper接口

1
2
public interface CommonMapper<T> extends Mapper<T>, MySqlMapper<T> {
}

实体类

  • @Entity:指这个类映射到数据库表。
  • @Data:包含了get,set和toString。
  • @Id:一个实体类中至少需要一个标记 @Id 注解的字段,存在联合主键时可以标记多个。当类中没有存在标记 @Id 注解的字段时,你可以理解为类中的所有字段是联合主键。使用所有的 ByPrimaryKey 相关的方法时,有 where 条件的地方,会将所有列作为条件。
    更多注解可参考数据库映射
1
2
3
4
5
6
7
8
9
10
11
12
13
@Entity
@Data
public class User {

@Id
private Integer id;
private String name;
private Integer age;
private String email;

@Transient
private String otherThings; //非数据库表中字段
}

接口类

继承自定义mapper接口

1
2
public interface UserMapper extends CommonMapper<User> {
}

controller类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@RestController
public class UserController {

@Autowired
private UserMapper userMapper;

@RequestMapping("/selectAll")
public String selectAll() {
User user = userMapper.selectByPrimaryKey("1");
System.out.println("================>" + user);

List<User> userList = userMapper.selectAll();
System.out.println("================>" + userList);

//使用对象传参,适用于1个字段或者多个字段联合主键使用
User user1 = new User();
user1.setId(1);
User user2 = userMapper.selectByPrimaryKey(user1);
System.out.println("================>" + user2);

return "success";
}
}

启动类

如果没有使用 @MapperScan 注解,就需要在接口上增加 @Mapper 注解,否则 MyBatis 无法判断扫描哪些接口。

1
2
3
4
5
6
7
8
9
@SpringBootApplication
// 注意:这里的 MapperScan 是 tk.mybatis.spring.annotation.MapperScan 这个包下的
@MapperScan(basePackages = "com.huzh.springbootmybatismapper.mapper")
public class SpringbootMybatisMapperApplication {

public static void main(String[] args) {
SpringApplication.run(SpringbootMybatisMapperApplication.class, args);
}
}

欢迎关注我的其它发布渠道