Mybatis中通用Mapper的InsertList()用法
關(guān)于通用mapper中的的insertList()方法
針對通用Mapper中批量新增時是否需要自增ID或者自定義ID時需要使用不同包下的insertList()
通常批量插入的ID非自增的ID(及自定義生成ID策略),所以tk.mybatis.mapper.additional.insert.InsertListMapper包下的insertList()經(jīng)常用在項目組中
配合@Intercepts 自定義 Mybatis 攔截 update 操作(添加和修改)
tk.mybatis.mapper.common.special.InsertListMapper包下的insertList()方法
- pom導(dǎo)入:
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-base</artifactId>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-core</artifactId>
</dependency>使用該方法的實體類主鍵必須是自增的(需要在實體類中指出)。
如果實體的主鍵名為’id’,同時主鍵自增。在不修改代碼的情況下,使用insertList()方法實現(xiàn)的批量插入數(shù)據(jù)后通用mapper能自動回寫主鍵值到實體對象中。
- 如以下實體類和對應(yīng)mapper:
@Data
@Table(name = "user")
public class User {
@Id
@KeySql(useGeneratedKeys = true)
private Integer id;
private String username;
private String desc;
}
public interface UserMapper extends InsertListMapper<User> {
}如果實體類主鍵名不是id,同時實體類主鍵是自增的,想要實現(xiàn)實體類主鍵回寫,需要重寫insertList()方法,其實就是修改了注解上的值,把@Options注解上的keyProperty值改為自己實體類的主鍵名
- 如以下實體類和對應(yīng)的mapper:
@Data
@Table(name = "user")
public class User {
@Id
@KeySql(useGeneratedKeys = true)
private Integer uid;
private String username;
private String desc;
}
public interface UserMapper extends Mapper<User>, InsertListMapper<User> {
@Options(keyProperty = "uid",useGeneratedKeys = true)
@InsertProvider(type = SpecialProvider.class, method = "dynamicSQL")
int insertList(List<User> recordList);
}tk.mybatis.mapper.additional.insert.InsertListMapper包下的insertList()方法
- pom導(dǎo)入:
<!-- https://mvnrepository.com/artifact/tk.mybatis/mapper-extra -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-extra</artifactId>
<version>1.1.5</version>
</dependency>- 該方法不支持主鍵策略,需要在實體類中指定主鍵。
- 該方法執(zhí)行后不會回寫實體類的主鍵值。
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot集成xxl-job實現(xiàn)超牛的定時任務(wù)的步驟詳解
XXL-JOB是一個分布式任務(wù)調(diào)度平臺,其核心設(shè)計目標(biāo)是開發(fā)迅速、學(xué)習(xí)簡單、輕量級、易擴(kuò)展,現(xiàn)已開放源代碼并接入多家公司線上產(chǎn)品線,開箱即用,本文給大家介紹了SpringBoot集成xxl-job實現(xiàn)超牛的定時任務(wù),需要的朋友可以參考下2023-10-10
淺談SpringBoot內(nèi)嵌Tomcat的實現(xiàn)原理解析
這篇文章主要介紹了淺談SpringBoot內(nèi)嵌Tomcat的實現(xiàn)原理解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
使用Java將字節(jié)數(shù)組轉(zhuǎn)成16進(jìn)制形式的代碼實現(xiàn)
在很多場景下,需要進(jìn)行分析字節(jié)數(shù)據(jù),但是我們存起來的字節(jié)數(shù)據(jù)一般都是二進(jìn)制的,這時候就需要我們將其轉(zhuǎn)成16進(jìn)制的方式方便分析,本文主要介紹如何使用Java將字節(jié)數(shù)組格式化成16進(jìn)制的格式并輸出,需要的朋友可以參考下2024-05-05
解決idea刪除模塊后重新創(chuàng)建顯示該模塊已經(jīng)被注冊的問題
這篇文章主要介紹了解決idea刪除模塊后重新創(chuàng)建顯示該模塊已經(jīng)被注冊的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02

