亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Mybatis中通用Mapper的InsertList()用法

 更新時間:2025年02月08日 09:27:05   作者:大金海  
文章介紹了通用Mapper中的insertList()方法在批量新增時的使用方式,包括自增ID和自定義ID的情況,對于自增ID,使用tk.mybatis.mapper.additional.insert.InsertListMapper包下的insertList()方法;對于自定義ID,需要重寫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導入:
        <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能自動回寫主鍵值到實體對象中。

  • 如以下實體類和對應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值改為自己實體類的主鍵名

  • 如以下實體類和對應的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導入:
        <!-- 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)文章

最新評論