網(wǎng)址
- TK通用Mapper
官方網(wǎng)站:https://mybatis.io/
Github: https://github.com/abel533/Mapper - Mybatis-Plus
官方網(wǎng)站:https://mp.baomidou.com/
Github: https://github.com/baomidou/mybatis-plus
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只需要把原來mybatis前綴改成mybatis-plus前綴即可
mybatis-plus:
configuration:
map-underscore-to-camel-case: true
#指定默認(rèn)的枚舉TypeHandler,mybatis-plus這個(gè)通用EnumTypeHandler如果檢測(cè)到無@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 開啟不忽略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,需要開啟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以上可以通過配置@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ù)無效铺坞,需要配置@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開發(fā)者在寫一個(gè)patch腐晾,
希望以后能支持泛型,畢竟Array操作上有點(diǎn)不方便
還有一個(gè)方案是生成json的時(shí)候帶上類型丐一,有Jackson的實(shí)現(xiàn)方案藻糖,不過生成的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 上找到