TK通用Mapper和Mybatis-Plus使用对比

网址

SpringBoot使用

tk较长时间未更新了,好在支持当前Mybatis 3.5版本,pom里排除下org.mybatis包

        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>2.1.5</version>
            <exclusions>
                <exclusion>
                    <groupId>org.mybatis</groupId>
                    <artifactId>*</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.3.1</version>
        </dependency>

配置Application.yml

#mybatis-plus只需要把原来mybatis前缀改成mybatis-plus前缀即可
mybatis-plus:
  configuration:
    map-underscore-to-camel-case: true
    #指定默认的枚举TypeHandler,mybatis-plus这个通用EnumTypeHandler如果检测到无@EnumValue注解会报错
    default-enum-type-handler: com.baomidou.mybatisplus.core.handlers.MybatisEnumTypeHandler
#mybatis-plus也可以不注册默认EnumTypeHandler,而直接指定enum目录,会自动把目录内的类都注册typeHandler  
#type-enums-package:  com.example.enums 

#tk 开启不忽略Enum
mapper:
  enum-as-simple-type: true

配置Mapper目录

  • tk需要使用tk自定义的MapperScan
import org.springframework.context.annotation.Configuration;
import tk.mybatis.spring.annotation.MapperScan;

@Configuration
@MapperScan("com.example.tkmybatisdemo.mapper")
public class TkMapperConfig {
}
  • mybatis-plus 使用mybatis自带的MapperScan
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@MapperScan("com.example.tkmybatisdemo.mapper")
public class MybatisPlusConfig {
}

实体类

tk 使用JPA注解,Mybatis-plus使用的自定义注解

@Data
//tk 标识表名
@Table(name = "user")
//mybatis-plus如果属性里指定了TypeHandler,需要开启autoResultMap=true,才会正常处理
@TableName(autoResultMap = true)
public class User {

    //tk指定id列
    @Id
    private Long id;

    private String name;

    private Integer age;

    //tk默认会忽略Enum类型属性
    //3.5以上可以配置mapper.enum-as-simple-type=true 启用
    //4.0以上可以通过配置@Column或者@ColumnType就不会忽略了
    @Column
    private GenderEnum gender;

    private String mobile;
    
    //tk指定typehandler
    @ColumnType(typeHandler = JacksonTypeHandler.class)
    //mybatis-plus指定typehandler
    @TableField(typeHandler = JacksonTypeHandler.class)
    private UserInfo[] infos;

    private Date createTime;

    private Date updateTime;

}

定义Mapper

这两个框架,在对象上声明的typeHandler使用内置的函数都是生效的。但是对Mapper中自定义的函数无效,需要配置@Result指定typeHandler。

  • TK通用mapper
@Repository
public interface UserMapper extends Mapper<User> {

    @Results(value = {
            @Result(column = "infos", property = "infos", typeHandler = JacksonTypeHandler.class)
    })
    @Select("select * from user")
    List<User> listAll();
}
  • Mybatis-Plus
@Repository
public interface User2Mapper extends BaseMapper<User> {

    @Results(value = {
            @Result(column = "infos", property = "infos", typeHandler = JacksonTypeHandler.class)
    })
    @Select("select * from user")
    List<User> listAll();
}

通用JsonTypehander

通用的JacksonTypeHandler,目前支持数组,因为泛型擦除的原因会导致List,Set等反序列会丢失类型,导致数据被反序列化为Map。
这个限制mybatis开发者在写一个patch
希望以后能支持泛型,毕竟Array操作上有点不方便
还有一个方案是生成json的时候带上类型,有Jackson的实现方案,不过生成的JSON不简洁,还有生成以后类名和包名都不能改动。

[
  "[Lcom.example.tkmybatisdemo.entity.UserInfo;",
  [
    {
      "@class": "com.example.tkmybatisdemo.entity.UserInfo",
      "info1": "1111",
      "info2": [
        "java.util.ArrayList",
        [
          {
            "@class": "com.example.tkmybatisdemo.entity.UserInfo2",
            "info21": "2222",
            "info22": "33333"
          }
        ]
      ]
    }
  ]
]

通用枚举EnumTypeHandler

mybatis-plus 内置支持枚举,tk需要配置,可以参考另一篇文章 Mybatis通用枚举 Enum TypeHandler

public enum GenderEnum {
    UNKNOWN(0, "未知"),
    MALE(1, "男"),
    FEMALE(2, "女"),
    ;

    @JsonValue
    @EnumValue
    @Getter
    private final int code;

    @Getter
    private final String description;

    GenderEnum(int code, String description) {
        this.code = code;
        this.description = description;
    }

    private static final HashMap<Integer, GenderEnum> values = new HashMap<>();

    static {
        for (final GenderEnum type : GenderEnum.values()) {
            GenderEnum.values.put(type.getCode(), type);
        }
    }

    @JsonCreator(mode = JsonCreator.Mode.DELEGATING)
    public static GenderEnum of(int code) {
        return GenderEnum.values.get(code);
    }
}

本文的代码都可以在github 上找到

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容