通用Mapper使用教程
1.引入
1.1 作用
替我们生成常用的正删改查的sql语句
1.2 通用mapper官方地址
https://gitee.com/free
https://github.com/abel533/Mapper
2. 快速入门
2.1 快速搭建(基于Spring Boot)
- 添加依赖
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.1.5</version>
</dependency>
这里我们使用最新的版本
- 写配置文件
# 配置数据库
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/tk_mapper?serverTimezone=UTC &useUnicode=true&characterEncoding=UTF-8&useSSL=false
username: root
password: root
# mybatis 配置
mybatis:
mapper-locations: classpath:mapper/*Mapper.xml
# 下面这个是用来指定pojo的包,这样在自定义的Mapper.xml文件中可以直接使用类名而不需要使用全类名
#type-aliases-package:
- 添加注解
#@MapperScan 注解配置
你可以给带有 @Configuration 的类配置该注解,或者直接配置到 Spring Boot 的启动类上,如下:
@tk.mybatis.spring.annotation.MapperScan(basePackages = "扫描包")
@SpringBootApplication
public class TkMapperApplication { {...}
当然你也可以在每个Mapper接口上使用`@Mapper`注解,但那样就会比较麻烦
- 创建数据库,添加测试数据
# 数据库的初始语句
CREATE TABLE `table_emp` (
`emp_id` int NOT NULL AUTO_INCREMENT ,
`emp_name` varchar(500) NULL ,
`emp_salary` double(15,5) NULL ,
`emp_age` int NULL ,
PRIMARY KEY (`emp_id`)
);
INSERT INTO `table_emp` (`emp_name`, `emp_salary`, `emp_age`) VALUES ('tom', '1254.37', '27');
INSERT INTO `table_emp` (`emp_name`, `emp_salary`, `emp_age`) VALUES ('jerry', '6635.42', '38');
INSERT INTO `table_emp` (`emp_name`, `emp_salary`, `emp_age`) VALUES ('bob', '5560.11', '40');
INSERT INTO `table_emp` (`emp_name`, `emp_salary`, `emp_age`) VALUES ('kate', '2209.11', '22');
INSERT INTO `table_emp` (`emp_name`, `emp_salary`, `emp_age`) VALUES ('justin', '4203.15', '30')
- 编写实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "table_emp")
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer empId;
private String empName;
private Double empSalary;
private Integer empAge;
}
- 编写mapper接口
import tk.mybatis.mapper.common.Mapper;
// 只需要继承就好,不需要写方法
public interface EmployeeMapper extends Mapper<Employee> {
}
- 编写service层
@Service
public class EmployeeService {
@Autowired
private EmployeeMapper employeeMapper;
public List<Employee> selectList(){
return employeeMapper.selectAll();
}
}
- 测试类测试
@SpringBootTest
@RunWith(SpringRunner.class)
class TkMapperApplicationTests {
@Autowired
private EmployeeService employeeService;
// 测试查询所有
@Test
void testSelectList(){
List<Employee> employees = employeeService.selectList();
employees.forEach(System.out::println);
}
}
- 结果
可以看到我们并没有写方法,就可以进行基本的增删改查,这些都是通用mapper给我们做好的!
3 .常用注解
3.1@Table注解
这个注解是用来建立实体类和数据库表之间的对应关系。默认通用mapper是开启驼峰命名的,但有时,实体类名和表名并不只是驼峰转下划线的形式,所以要使用这个注解的name属性来指定,使用如下:
// 默认规则是Employee-->employee;使用注解后就是Employee-->table_emp
@Table(name = "table_emp")
public class Employee {
}
3.2@Column
通用mapper默认是开启驼峰命名的
这个注解的作用是建立实体类字段和数据库表字段之间的对应关系 。也就是如果不是默认的驼峰转下划线的格式,那么就需要这个注解的name属性指定字段映射规则,当然也可以给所有的字段都加上注解,这样会更加清晰明了。
3.3 @Id 注解
在数据库中设置了主键,但是在实体类中没有设置主键时,比如我们测试根据主键查询,会发现生成的代码
SELECT emp_id,emp_name,emp_salary_apple,emp_age FROM table_emp WHERE emp_id = ?
AND emp_name = ? AND emp_salary_apple = ? AND emp_age = ?
之所以会生成上面这样的 WHERE 子句是因为通用 Mapper 将实体类中的所有字段都拿来放在一起作为联合主键。所以我们需要在实体类的主键上添加这个注解来告诉通用mapper这个字段是主键。
3.4@GeneratedValue 注解
作用是让通用 Mapper 在执行 insert 操作之后将数据库自动生成的主键值回写到实体类对象中。 通常和@Id
注解一起使用。
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer empId;
3.5@KeySql注解
主键策略注解,用于配置如何生成主键。
这是通用 Mapper 的自定义注解,改注解的目的就是替换 @GeneratedValue
注解。
可以跟进源码看有三个属性来进行获取id回写。使用详情:
@Id
// @GeneratedValue(strategy = GenerationType.IDENTITY)
@KeySql(useGeneratedKeys = true)
private Integer empId;
3.6@Transient注解
用于标记不与数据库表字段对应的实体类字段 。
@Transient
private String otherThings; //非数据库表中字段
4.具体方法
4.1 insertselective是新增那些除了主键的非空值
4.2 批量插入操作
通用mapper提供了这样的接口,我们需要继承这个接口InsertListMapper
就好,注意有两个这个接口,他们的两个的insertList
方法的限制条件不同,下面给出两个的对比,需要注意一下:
tk.mybatis.mapper.additional.insert.InsertListMapper
不支持主键策略,插入前需要设置好主键的值
特别注意:2018-04-22 后,该方法支持 @KeySql 注解的 genId 方式
tk.mybatis.mapper.common.special.InsertListMapper
批量插入,支持批量插入的数据库可以使用,例如MySQL,H2等,另外该接口限制实体包含`id`属性并且必须为自增列
4.3 批量查询和批量删除
使用tk.mybatis.mapper.additional.idlist.IdListMapper
就可以实现批量查询和删除。
…
5. 文档源码地址
https://gitee.com/njitzyd/tk-mapper