Mapper4 是一個可以實現(xiàn)任意 MyBatis 通用方法的框架番刊,項目提供了常規(guī)的增刪改查操作以及Example相關(guān)的單表操作。通用 Mapper 是為了解決 MyBatis 使用中 90% 的基本操作影锈,使用它可以很方便的進(jìn)行開發(fā)芹务,可以節(jié)省開發(fā)人員大量的時間。
1鸭廷,添加pom
<dependency>
? ?<groupId>tk.mybatis</groupId>?
???????<artifactId>mapper-spring-boot-starter</artifactId>
? ? <version>2.1.5</version>
</dependency>
2枣抱,添加配置
#mybatis駝峰和告警配置
mybatis:
????configuration:
????????map-underscore-to-camel-case: true
????????auto-mapping-unknown-column-behavior: WARNING
mapper:
????identity: MYSQL?# 取主鍵的方式
????before: true?# 主鍵遞增
????not-empty: true?# 按主鍵插入或更新時,是否判斷字符串 != ''
????style: camelhump?# 實體類與表中字段的映射方式:駝峰轉(zhuǎn)轉(zhuǎn)下劃線
????wrap-keyword: '{0}'?# 自動配置關(guān)鍵字辆床,配置后不需要使用 @Column 指定別名
????safe-delete: true?# 刪除時必須設(shè)置查詢條件
????safe-update: true?# 更新時必須設(shè)置查詢條件
????use-java-type: true?# 是否映射Java基本數(shù)據(jù)類型
????mappers: tk.mybatis.mapper.common.Mapper
mapper:
? ??identity: MYSQL
3佳晶,在實體類上加@Table和@Id注解(沒有主鍵可不加)
4,dao層接口必須繼承tk.mybatis.mapper.common.Mapper接口讼载,泛型內(nèi)填對應(yīng)實體類
5轿秧,啟動類上加tk.mybatis.spring.annotation.MapperScan注解,里面值填dao包路徑
6咨堤,完成菇篡,這時候往service里注入dao接口會自動實現(xiàn)以下方法: