一、前言
使用Mybatis的开发者,大多数都会遇到一个问题,就是要写大量的SQL在xml文件中,除了特殊的业务逻辑SQL之外,还有大量结构类似的增删改查SQL。而且,当数据库表结构改动时,对应的所有SQL以及实体类都需要更改。这工作量和效率的影响或许就是区别增删改查程序员和真正程序员的屏障。这时,通用Mapper便应运而生……
二、什么是通用Mapper
通用Mapper就是为了解决单表增删改查,基于Mybatis的插件。开发人员不需要编写SQL,不需要在DAO中增加方法,只要写好实体类,就能支持相应的增删改查方法。
三、如何使用
以MySQL为例,假设存在这样一张表:
CREATE TABLE `user_table` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT '',
`create_time` datetime DEFAULT NULL,
`create_user` varchar(32) DEFAULT NULL,
`update_time` datetime DEFAULT NULL,
`update_user` varchar(32) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
主键是id,自增。下面以这张表为例介绍如何使用通用Mapper。
3.1 配置Maven依赖
<!--tk.mybatis依赖-->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.1.5</version>
</dependency>
引入依赖的时候要特别注意是否会和springboot相互冲突
3.2 配置文件添加配置
mybatis:
mapper-locations: classpath:mapper/*.xml
config-location: classpath:mybatis-config/mybatis-config.xml
通用mapper的mybatis-config.xml是啥???
3.3 编写实体类
package com.example.demo.entity;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Data
// @Table指定该实体类对应的表名,如表名为base_customer,类名为BaseCustomer可以不需要此注解
@Table(name = "user_table")
public class UserTableEntity{
// @Id表示该字段对应数据库表的主键id
// @GeneratedValue中strategy表示使用数据库自带的主键生成策略.
// @GeneratedValue中generator配置为"JDBC",在数据插入完毕之后,会自动将主键id填充到实体类中.类似普通mapper.xml中配置的selectKey标签(待确认)
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(name = "name")
private String name;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@Column(name = "create_time")
private Date createTime;
@Column(name = "create_name")
private String createName;
}
说明
- 表名默认使用类名,驼峰转下划线(只对大写字母进行处理),如UserInfo默认对应的表名为user_info。
- 表名可以使用@Table(name = “tableName”)进行指定,对不符合第一条默认规则的可以通过这种方式指定表名。
- 字段默认和@Column一样,都会作为表字段,表字段默认为Java对象的Field名字驼峰转下划线形式。
- 可以使用@Column(name = “fieldName”)指定不符合第3条规则的字段名。
- 使用@Transient注解可以忽略字段,添加该注解的字段不会作为表字段使用。
- 建议一定是有一个@Id注解作为主键的字段,可以有多个@Id注解的字段作为联合主键。
- 如果是MySQL的自增字段,加上@GeneratedValue(generator = “JDBC”)即可。如果是其他数据库,可以参考官网文档。
3.4 编写Mapper接口
package com.example.demo.mapper;
import com.example.demo.entity.UserTableEntity;
import tk.mybatis.mapper.common.Mapper;
//Mapper继承tkMabatis的Mapper接口
public interface UserTableMapper extends Mapper<UserTableEntity> {
}
一旦继承了Mapper,继承的Mapper就拥有了Mapper所有的通用方法:
Select
方法:List<T> select(T record);
说明:根据实体中的属性值进行查询,查询条件使用等号
方法:T selectByPrimaryKey(Object key);
说明:根据主键字段进行查询,方法参数必须包含完整的主键属性,查询条件使用等号
方法:List<T> selectAll();
说明:查询全部结果,select(null)方法能达到同样的效果
方法:T selectOne(T record);
说明:根据实体中的属性进行查询,只能有一个返回值,有多个结果是抛出异常,查询条件使用等号
方法:int selectCount(T record);
说明:根据实体中的属性查询总数,查询条件使用等号
Insert
方法:int insert(T record);
说明:保存一个实体,null的属性也会保存,不会使用数据库默认值
方法:int insertSelective(T record);
说明:保存一个实体,null的属性不会保存,会使用数据库默认值
Update
方法:int updateByPrimaryKey(T record);
说明:根据主键更新实体全部字段,null值会被更新
方法:int updateByPrimaryKeySelective(T record);
说明:根据主键更新属性不为null的值
Delete
方法:int delete(T record);
说明:根据实体属性作为条件进行删除,查询条件使用等号
方法:int deleteByPrimaryKey(Object key);
说明:根据主键字段进行删除,方法参数必须包含完整的主键属性
3.5 启动类添加@MapperScan
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tk.mybatis.spring.annotation.MapperScan;
// tk.mybatis.spring.annotation.MapperScan包下的@MapperScan
@MapperScan(basePackages = {"com.example.demo.mapper"})
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
3.6 测试
@JsonFormat(pattern = “yyyy-MM-dd HH:mm:ss”, timezone = “GMT+8”)
@DateTimeFormat(pattern = “yyyy-MM-dd HH:mm:ss”)的使用??
Mybatis通用Mapper(tk.mybatis)的使用