TK通用Mapper和Mybatis-Plus使用對(duì)比

網(wǎng)址

SpringBoot使用

tk較長(zhǎng)時(shí)間未更新了,好在支持當(dāng)前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只需要把原來(lái)mybatis前綴改成mybatis-plus前綴即可
mybatis-plus:
  configuration:
    map-underscore-to-camel-case: true
    #指定默認(rèn)的枚舉TypeHandler,mybatis-plus這個(gè)通用EnumTypeHandler如果檢測(cè)到無(wú)@EnumValue注解會(huì)報(bào)錯(cuò)
    default-enum-type-handler: com.baomidou.mybatisplus.core.handlers.MybatisEnumTypeHandler
#mybatis-plus也可以不注冊(cè)默認(rèn)EnumTypeHandler,而直接指定enum目錄,會(huì)自動(dòng)把目錄內(nèi)的類都注冊(cè)typeHandler  
#type-enums-package:  com.example.enums 

#tk 開(kāi)啟不忽略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 {
}

實(shí)體類

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

@Data
//tk 標(biāo)識(shí)表名
@Table(name = "user")
//mybatis-plus如果屬性里指定了TypeHandler,需要開(kāi)啟autoResultMap=true,才會(huì)正常處理
@TableName(autoResultMap = true)
public class User {

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

    private String name;

    private Integer age;

    //tk默認(rèn)會(huì)忽略Enum類型屬性
    //3.5以上可以配置mapper.enum-as-simple-type=true 啟用
    //4.0以上可以通過(guò)配置@Column或者@ColumnType就不會(huì)忽略了
    @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

這兩個(gè)框架,在對(duì)象上聲明的typeHandler使用內(nèi)置的函數(shù)都是生效的。但是對(duì)Mapper中自定義的函數(shù)無(wú)效,需要配置@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,目前支持?jǐn)?shù)組,因?yàn)榉盒筒脸脑驎?huì)導(dǎo)致List,Set等反序列會(huì)丟失類型,導(dǎo)致數(shù)據(jù)被反序列化為Map。
這個(gè)限制mybatis開(kāi)發(fā)者在寫(xiě)一個(gè)patch,
希望以后能支持泛型,畢竟Array操作上有點(diǎn)不方便
還有一個(gè)方案是生成json的時(shí)候帶上類型,有Jackson的實(shí)現(xiàn)方案,不過(guò)生成的JSON不簡(jiǎn)潔,還有生成以后類名和包名都不能改動(dòng)。

[
  "[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 內(nèi)置支持枚舉,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 上找到

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容