MybatisPlus實(shí)現(xiàn)insertBatchSomeColumn進(jìn)行批量增加
1、引入相關(guān)依賴
? ? ? ? <!--mybatis-plus啟動器--> ? ? ? ? <dependency> ? ? ? ? ? ? <groupId>com.baomidou</groupId> ? ? ? ? ? ? <artifactId>mybatis-plus-boot-starter</artifactId> ? ? ? ? ? ? <version>3.5.1</version> ? ? ? ? </dependency> ? ? ? ? ? ? ? ? <!--mybatis-plus擴(kuò)展插件依賴--> ? ? ? ? <dependency> ? ? ? ? ? ? <groupId>com.baomidou</groupId> ? ? ? ? ? ? <artifactId>mybatis-plus-extension</artifactId> ? ? ? ? ? ? <version>3.5.1</version> ? ? ? ? </dependency>
2、編寫sql注入器
package com.linmain.mysql.config;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.extension.injector.methods.InsertBatchSomeColumn;
import java.util.List;
/**
?* @Author linzhuoqi
?* @Date 2023/3/10
?* @Eamil 1580752420@qq.com
?* @Version
?* @Description ? ?自定義的sql注入器,插入了批量插入的方法
?*/
public class EasySqlInjector extends DefaultSqlInjector {
? ? @Override
? ? public List<AbstractMethod> getMethodList(Class<?> mapperClass, TableInfo tableInfo) {
? ? ? ? // 注意:此SQL注入器繼承了DefaultSqlInjector(默認(rèn)注入器),調(diào)用了DefaultSqlInjector的getMethodList方法,保留了mybatis-plus的自帶方法
? ? ? ? List<AbstractMethod> methodList = super.getMethodList(mapperClass, tableInfo);
? ? ? ? //增加了一個批量插入的方法
? ? ? ? methodList.add(new InsertBatchSomeColumn(i -> i.getFieldFill() != FieldFill.UPDATE));
? ? ? ? return methodList;
? ? }
}3、在mybatisPlus的配置類中注入插件
@Configuration
public class MyBatisPlusConfig {
?
? ? @Bean
? ? public MybatisPlusInterceptor mybatisPlusInterceptor(){
? ? ? ? MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
? ? ? ? //添加分頁插件
? ? ? ? interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
? ? ? ? //添加樂觀鎖插件
? ? ? ? interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
? ? ? ? return interceptor;
? ? }
?
? ? @Bean
? ? public EasySqlInjector easySqlInjector() {
? ? ? ? return new EasySqlInjector();
? ? }
?
}4、編寫dao層接口
package com.linmain.column.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.linmain.column.pojo.entity.ColTag;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
/**
?* 專欄標(biāo)簽(ColTag)表數(shù)據(jù)庫訪問層
?*
?* @Author linzhuoqi
?* @Date 2023-03-02 20:44:38
?* @Eamil 1580752420@qq.com
?* @Version
?* @Description
?*/
@Mapper
public interface ColTagDao extends BaseMapper<ColTag> {
? ? /**
? ? ?* 批量插入 僅適用于mysql
? ? ?*
? ? ?* @param entityList 實(shí)體列表
? ? ?* @return 影響行數(shù)
? ? ?*/
? ? Integer insertBatchSomeColumn(Collection<ColTag> entityList);
}5、最后
進(jìn)行正常的使用即可
colTagDao.insertBatchSomeColumn(colTags);
到此這篇關(guān)于MybatisPlus實(shí)現(xiàn)insertBatchSomeColumn進(jìn)行批量增加的文章就介紹到這了,更多相關(guān)MybatisPlus insertBatchSomeColumn批量增加內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JavaMail實(shí)現(xiàn)簡單郵件發(fā)送
這篇文章主要為大家詳細(xì)介紹了JavaMail實(shí)現(xiàn)簡單郵件發(fā)送,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-08-08
Spring動態(tài)加載bean后調(diào)用實(shí)現(xiàn)方法解析
這篇文章主要介紹了Spring動態(tài)加載bean后調(diào)用實(shí)現(xiàn)方法解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-08-08
SpringBoot3中token攔截器鏈的設(shè)計(jì)與實(shí)現(xiàn)步驟
本文介紹了spring boot后端服務(wù)開發(fā)中有關(guān)如何設(shè)計(jì)攔截器的思路,文中通過代碼示例和圖文講解的非常詳細(xì),具有一定的參考價值,需要的朋友可以參考下2024-03-03
Java解決No enclosing instance of type PrintListFromTailToHead
這篇文章主要介紹了Java解決No enclosing instance of type PrintListFromTailToHead is accessible問題的兩種方案的相關(guān)資料,需要的朋友可以參考下2016-07-07
MyBatis-Plus實(shí)現(xiàn)多數(shù)據(jù)源的示例代碼
這篇文章主要介紹了MyBatis-Plus實(shí)現(xiàn)多數(shù)據(jù)源的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11

