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

