国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Mybatis-Plus 自定義mapper批量新增修改函數(shù)

這篇具有很好參考價(jià)值的文章主要介紹了Mybatis-Plus 自定義mapper批量新增修改函數(shù)。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

version

<dependency>
	<groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.5.3.1</version>
</dependency>

MybatisPlusConfig

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 954L
 * @wechat w_954L
 * @email admin@954l.com
 * @create 2020/10/6 21:39
 */
@Configuration
public class MybatisPlusConfig {

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(){
        MybatisPlusInterceptor mp = new MybatisPlusInterceptor();
        mp.addInnerInterceptor(new PaginationInnerInterceptor());
        return mp;
    }

    @Bean
    public CustomizedSqlInjector customizedSqlInjector() {
        return new CustomizedSqlInjector();
    }

}

CustomizedSqlInjector

import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector;
import com.baomidou.mybatisplus.core.metadata.TableInfo;

import java.util.List;

/**
 * @author 954L
 * @wechat w_954L
 * @email admin@954l.com
 * @create 2023/8/10 16:26
 */
public class CustomizedSqlInjector extends DefaultSqlInjector {

    @Override
    public List<AbstractMethod> getMethodList(Class<?> mapperClass, TableInfo tableInfo) {
        List<AbstractMethod> methodList = super.getMethodList(mapperClass, tableInfo);
        methodList.add(new InsertBatchMethod("insertBatch"));
        methodList.add(new UpdateBatchMethod("updateBatch"));
        return methodList;
    }

}

InsertBatchMethod

import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.executor.keygen.NoKeyGenerator;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;

/**
 * @author 954L
 * @wechat w_954L
 * @email admin@954l.com
 * @create 2023/8/10 16:23
 */
@Slf4j
public class InsertBatchMethod extends AbstractMethod {

    protected InsertBatchMethod(String methodName) { super(methodName); }

    @Override
    public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
        final String sql = "<script>insert into %s %s values %s</script>";
        final String fieldSql = prepareFieldSql(tableInfo);
        final String valueSql = prepareValuesSql(tableInfo);
        final String sqlResult = String.format(sql, tableInfo.getTableName(), fieldSql, valueSql);
        SqlSource sqlSource = languageDriver.createSqlSource(configuration, sqlResult, modelClass);
        return this.addInsertMappedStatement(mapperClass, modelClass, "insertBatch",
                sqlSource, new NoKeyGenerator(), null, null);
    }

    private String prepareFieldSql(TableInfo tableInfo) {
        StringBuilder fieldSql = new StringBuilder();
        fieldSql.append(tableInfo.getKeyColumn()).append(",");
        tableInfo.getFieldList().forEach(x -> fieldSql.append(x.getColumn()).append(","));
        fieldSql.delete(fieldSql.length() - 1, fieldSql.length());
        fieldSql.insert(0, "("); fieldSql.append(")");
        return fieldSql.toString();
    }

    private String prepareValuesSql(TableInfo tableInfo) {
        final StringBuilder valueSql = new StringBuilder();
        valueSql.append("<foreach collection=\"list\" item=\"item\" index=\"index\" open=\"(\" separator=\"),(\" close=\")\">");
        valueSql.append("#{item.").append(tableInfo.getKeyProperty()).append("},");
        tableInfo.getFieldList().forEach(x -> valueSql.append("#{item.").append(x.getProperty()).append("},"));
        valueSql.delete(valueSql.length() - 1, valueSql.length());
        valueSql.append("</foreach>");
        return valueSql.toString();
    }
}

UpdateBatchMethod

import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;

/**
 * @author 954L
 * @wechat w_954L
 * @email admin@954l.com
 * @create 2023/8/10 16:23
 */
@Slf4j
public class UpdateBatchMethod extends AbstractMethod {

    protected UpdateBatchMethod(String methodName) { super(methodName); }

    @Override
    public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
        String sql = "<script>\n<foreach collection=\"list\" item=\"item\" separator=\";\">\nupdate %s %s where %s=#{%s} %s\n</foreach>\n</script>";
        String additional = tableInfo.isWithVersion() ? tableInfo.getVersionFieldInfo().getVersionOli("item", "item.") : "" + tableInfo.getLogicDeleteSql(true, true);
        String setSql = sqlSet(tableInfo.isWithLogicDelete(), false, tableInfo, false, "item", "item.");
        String sqlResult = String.format(sql, tableInfo.getTableName(), setSql, tableInfo.getKeyColumn(), "item." + tableInfo.getKeyProperty(), additional);
        SqlSource sqlSource = languageDriver.createSqlSource(configuration, sqlResult, modelClass);
        return this.addUpdateMappedStatement(mapperClass, modelClass, "updateBatch", sqlSource);
    }

}

RootMapper

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
 * @author 954L
 * @wechat w_954L
 * @email admin@954l.com
 * @create 2023/8/10 16:21
 */
public interface RootMapper<T> extends BaseMapper<T> {

    int insertBatch(@Param("list") List<T> list);

    int updateBatch(@Param("list") List<T> list);

}

使用方式
mapper接口實(shí)現(xiàn)自定義的RootMapper,即可調(diào)用批量新增修改函數(shù)文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-663731.html

到了這里,關(guān)于Mybatis-Plus 自定義mapper批量新增修改函數(shù)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • mybatis-plus 根據(jù)指定字段 批量 刪除/修改

    mybatis-plus 根據(jù)指定字段 批量 刪除/修改

    mybatis-plus 提供了根據(jù)id批量更新和修改的方法,這個(gè)大家都不陌生 但是當(dāng)表沒有id的時(shí)候怎么辦) 這個(gè)就不說(shuō)了,就是因?yàn)椴幌胧謱慡QL 所以才有這篇博客 mybatis plus 的 executeBatch 參考 mybatis plus 的updateBatchById 方法. 調(diào)用處: 接口 重寫方法 實(shí)現(xiàn) 這種寫法其實(shí)批量的效率還是比較慢的

    2024年02月13日
    瀏覽(17)
  • Mybatis-Plus批量添加或修改數(shù)據(jù)的三種方式

    Mybatis-Plus批量添加或修改數(shù)據(jù)的三種方式

    提供的方法 是遍歷每一個(gè)元素,判斷主鍵是否存在,如果存在則做更新,不存在添加 先獲取表中所有的主鍵 ,然后 判斷是否已存在,存在更新,不存在添加 on duplicate key update 是Mysql特有的語(yǔ)法,如下圖所示,表中id 為主鍵 再插入id為1的數(shù)據(jù),則提示主鍵已存在 改成如下

    2024年02月06日
    瀏覽(20)
  • Mybatis-Plus的SQL注入器實(shí)現(xiàn)批量插入/修改,效率比較

    Mybatis-Plus的SQL注入器實(shí)現(xiàn)批量插入/修改,效率比較

    mysql支持一條sql語(yǔ)句插入多條數(shù)據(jù)。但是Mybatis-Plus中默認(rèn)提供的saveBatch、updateBatchById方法并不能算是真正的批量語(yǔ)句,而是遍歷實(shí)體集合執(zhí)行INSERT_ONE、UPDATE_BY_ID語(yǔ)句。 mybatis-plus雖然做了分批請(qǐng)求、一次提交的處理。但如果jdbc不啟用配置rewriteBatchedStatements,那么批量提交的s

    2024年02月11日
    瀏覽(25)
  • Springboot優(yōu)雅單元測(cè)試之mapper的測(cè)試(基于mybatis-plus)

    基于springboot的工程,正常單元測(cè)試,可以利用IDEA的goto功能自動(dòng)生成對(duì)應(yīng)的測(cè)試類(測(cè)試方法),然后在生成的測(cè)試類加注解@SpringBootTest,執(zhí)行對(duì)應(yīng)的test方法即可。但是這樣默認(rèn)是會(huì)啟動(dòng)整個(gè)springboot應(yīng)用的,如果有web,還會(huì)啟動(dòng)web容器。這個(gè)時(shí)間比較久, 不夠優(yōu)雅 。 直接擼

    2024年02月11日
    瀏覽(19)
  • 在springboot中配置mybatis(mybatis-plus)mapper.xml掃描路徑的問(wèn)題

    在springboot中配置mybatis(mybatis-plus)mapper.xml掃描路徑的問(wèn)題

    我曾經(jīng)遇到過(guò)類似問(wèn)題: mybatis-plus的mapper.xml在src/main/java路徑下如何配置pom.xml和application.yml_idea 把mapper文件放到j(luò)ava下如何配置_梓沂的博客-CSDN博客 當(dāng)時(shí)只是找到解決問(wèn)題的辦法,但對(duì)mybatis配置來(lái)龍去脈并未深入了解,所以再次遇到問(wèn)題還是受此困擾。 重新復(fù)習(xí)mybatis plus和

    2024年02月10日
    瀏覽(23)
  • Mybatis-plus批量操作

    ? ? ? ? 使用Mybatis-plus可以很方便的實(shí)現(xiàn)批量新增和批量修改,不僅比自己寫foreach遍歷方便很多,而且性能也更加優(yōu)秀。但是Mybatis-plus官方提供的批量修改和批量新增都是根據(jù)id來(lái)修改的,有時(shí)候我們需求其他字段,所以就需要我們自己修改一下。 ? ? ? ? 在Mybatis-plus的IS

    2024年02月11日
    瀏覽(19)
  • Mybatis-Plus批量更新原理

    Mybatis-Plus批量更新原理

    IService的updateBatchById方法 默認(rèn)batchSize = 1000 com.baomidou.mybatisplus.extension.service.impl.ServiceImpl#updateBatchById 構(gòu)建了一個(gè)回調(diào),進(jìn)入executeBatch方法 在這個(gè)方法基本就能看出來(lái)了,執(zhí)行1000次方法后執(zhí)行一次flushStatements,也就是說(shuō)理論上是積累了1000個(gè)更新sql,才進(jìn)行一次數(shù)據(jù)庫(kù)更新 使用

    2024年02月05日
    瀏覽(20)
  • Mybatis-plus---的批量插入

    Mybatis-plus---的批量插入

    批量插入 一、繼承IService(偽批量) 二、insertBatchSomeColumn Mybatis-plus很強(qiáng),為我們誕生了極簡(jiǎn)CURD操作,但對(duì)于數(shù)據(jù)批量操作,顯然默認(rèn)提供的insert方法是不夠看的了,于是它和它來(lái)了!!! Mybatis-plus提供的兩種插入方式 ?????? ??繼承IService(偽批量) ????????insertBatchSo

    2024年02月16日
    瀏覽(21)
  • mybatis-plus 批量插入示例

    mybatis-plus 批量插入示例

    正常我們使用mybatis-plus插入的時(shí)候,首先想到的是??saveBatch?方法,不過(guò)看了下打印出來(lái)的sql和底層代碼,才發(fā)現(xiàn)它并不是真正的批量插入。 ? ? 實(shí)現(xiàn)層? ?ServiceImpl?中的代碼為 通過(guò)監(jiān)控控制臺(tái)發(fā)現(xiàn),它只是循環(huán)每1000條去插入,效率非常低。 ? 參考網(wǎng)友的文章,找到一個(gè)支

    2024年02月15日
    瀏覽(24)
  • spring boot集成mybatis-plus——Mybatis Plus 新增數(shù)據(jù)并返回主鍵 ID(圖文講解)

    spring boot集成mybatis-plus——Mybatis Plus 新增數(shù)據(jù)并返回主鍵 ID(圖文講解)

    ?更新時(shí)間 2023-01-10 15:37:37 大家好,我是小哈。 本小節(jié)中,我們將學(xué)習(xí)如何通過(guò) Mybatis Plus 框架給數(shù)據(jù)庫(kù)表新增數(shù)據(jù),主要內(nèi)容思維導(dǎo)圖如下: Mybatis Plus 新增數(shù)據(jù)思維導(dǎo)圖 為了演示新增數(shù)據(jù),在前面小節(jié)中,我們已經(jīng)定義好了一個(gè)用于測(cè)試的用戶表, 執(zhí)行腳本如下: 定義一

    2024年02月02日
    瀏覽(41)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包