Mybatis简化代码之一:配置通用Mapper| 8月更文挑战

876 阅读3分钟

这是我参与8月更文挑战的第9天

通用Mapper

1、集成

  • 1、java代码集成
  • 2、Spring集成
  • 3、SpringBoot集成(只针对此方式)

1)导入相关依赖

    <dependency>
      <groupId>tk.mybatis</groupId>
      <artifactId>mapper-spring-boot-starter</artifactId>
      <version>版本号</version>
    </dependency>

注意:引入该依赖时,和Mybatis官方的 starter没有冲突,但是官方的自动配置不会生效

2)配置文件信息

在4.0及之后的版本不在强制配置mappers参数,自定义类需通过注解@RegisterMapper可以完成注册

在yml中的配置:

mapper:
  mappers:
    - tk.mybatis.mapper.common.Mapper
    - tk.mybatis.mapper.common.Mapper2
  notEmpty: true

在properties中的配置

mapper.mappers=tk.mybatis.mapper.common.Mapper,tk.mybatis.mapper.common.Mapper2
mapper.notEmpty=true

3)核心MapperScan注解

可以给带有 @Configuration 的类配置该注解,或者直接配置到 Spring Boot 的启动类上

此MapperScan注解使用的是tk.mybatis.spring.annotation.MapperScan


2、关系映射

  • 1、对应接口只需要继承Mapper接口即可,类型必须提供
    public interface UserMapper extends Mapper<User>

实体类中的@Id注释需要标记主键,否则在调用ByPrimaryKey等方法时,所有字段都会作为联合主键来使用

  • 2、如果使用 XML 方式,需要提供接口对应的 XML 文件,如果是自编接口,同样可在XML文件和接口中进行编译

3、数据库映射

通用 Mapper 中,默认情况下是将实体类字段按照驼峰转下划线形式的表名列名进行转换。

  • 1、可以使用@NameStyle标注该类,并按照指定形式对该类中的字段进行转换
    normal,                     //原值
    camelhump,                  //驼峰转下划线
    uppercase,                  //转换为大写
    lowercase,                  //转换为小写
    camelhumpAndUppercase,      //驼峰转下划线大写形式
    camelhumpAndLowercase,      //驼峰转下划线小写形式

这个注解可以在类上进行配置,优先级高于对应的 style 全局配置。

  • 2、@Table注解配置 name 属性后,直接使用提供的表名,不再根据实体类名进行转换

  • 3、@Column 注解支持 name, insertable 和 updateable 三个属性,name配置映射的列名

  • 4、@Transient注解,标志某些字段不是表中的字段(简单数据类型),如果是Map,List则不需要配置

4、主键策略

  • 1、数据库主键支持自增,其次数据库提供的jdbc支持getGeneratedKeys 方法
@Id
@KeySql(useGeneratedKeys = true)
private Long id;
@Id
@GeneratedValue(generator = "JDBC")
private Long id;

上方注释同XML代码

<insert id="insert" useGeneratedKeys="true" keyProperty="id">
    insert into country (id, countryname, countrycode)
    values (#{id},#{countryname},#{countrycode})
</insert>
  • 2、第二种获取主键方式
@Id
//DEFAULT 需要配合 IDENTITY 参数(ORDER默认AFTER)
@KeySql(dialect = IdentityDialect.DEFAULT)
private Integer id;
//建议直接指定数据库
@Id
@KeySql(dialect = IdentityDialect.MYSQL)
private Integer id;

或:

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;

对应的XML代码

<insert id="insertAuthor">
    <selectKey keyProperty="id" resultType="int" order="AFTER">
      SELECT LAST_INSERT_ID()
    </selectKey>
    insert into country (id, countryname, countrycode)
    values (#{id},#{countryname},#{countrycode})
</insert>

二、通过序列和任意 SQL 获取主键值

@Id
@KeySql(sql = "select SEQ_ID.nextval from dual", order = ORDER.BEFORE)
private Integer id;

或:

@Id
@GeneratedValue(
  strategy = GenerationType.IDENTITY,
  generator = "select SEQ_ID.nextval from dual")
private Integer id;

对应的XML代码如下:

<insert id="insertAuthor">
  <selectKey keyProperty="id" resultType="int" order="BEFORE">
    select SEQ_ID.nextval from dual
  </selectKey>
  insert into country (id, countryname, countrycode)
  values (#{id},#{countryname},#{countrycode})
</insert>

5、配置介绍

通用mapper提供如下配置参数

参数属性
mappers4.0之前需要配置,之后不不需要配置
IDENTITY取回主键的方式,配置数据库信息
ORDER中的order属性,可选值为BEFORE和AFTER
notEmptyinsertSelective 和 updateByPrimaryKeySelective 中,是否判断字符串类型 !=''。
style实体和表转换时的默认规则
enableMethodAnnotation可以控制是否支持(getter 和 setter)在方法上使用注解,默认false
 useSimpleType | 默认 true,启用后判断实体类属性是否为表字段时校验字段是否为简单类型,如果不是就忽略该属性,这个配置优先级高于所有注解。

通用Service

由于大部分CURD操作都是在Service中进行操作,所以要封装通用Service类给与代码支持

1、新建service接口,方便实现重写,用泛型代替未知参数类型BaseService

2、新建抽象service实现类BaseServiceImpl

用泛型D来接收通用mapper,用泛型T来接收实体类

public abstract class BaseService <D extends Mapper<T>,T extends Serializable> implements baseServicei<T>

3、service类皆继承BaseServiceImpl并将泛型替代成已知的数据类型

4、调用service类中的方法