mybatis批量操作(增删改)sql语句

批量插入

对应sql

insert into tabelname(colum1,colum2...)  values(aa,aa...),(bb,bb...)
 <!-- 批量插入数据 -->
    <insert id="insertBatch" parameterType="java.util.List"
        useGeneratedKeys="true">
        <selectKey resultType="long" keyProperty="id" order="AFTER">
            SELECT
            LAST_INSERT_ID()
        </selectKey>
        insert into  wd_solr(fayu_id,tablename,name,logo,description,section_no,look_count,favorite_count,create_uid,create_time,update_time,timestamp)
        values
        <foreach collection="list" item="wdSolr" index="index"
            separator=",">
            (
            #{wdSolr.fayuId},#{wdSolr.tablename},#{wdSolr.name},#{wdSolr.logo},
            #{wdSolr.description},#{wdSolr.sectionNo},#{wdSolr.lookCount},#{wdSolr.favoriteCount},
            #{wdSolr.createUid},#{wdSolr.createTime},#{wdSolr.updateTime},#{wdSolr.timestamp}
            )
        </foreach>
    </insert>

批量更新

对应Sql

update tablename set 
         colum1= case id when 1 then aa
         when 2 then bb
         when 3 then cc
         ....
         end,
         colum2=case id when 1 then aa
         when 2 then bb
         when 3 then cc
         ....
         end,
where id in (1,2,3....)
<!-- 批量更新数据 -->
    <update id="updateBatch">
        update wd_solr set
        name =
        <foreach collection="list" item="wdSolr" index="index"
            separator=" " open="case id" close="end">
            when #{wdSolr.id} then
            #{wdSolr.name}
        </foreach>
        ,logo =
        <foreach collection="list" item="wdSolr" index="index"
            separator=" " open="case id" close="end">
            when #{wdSolr.id} then
            #{wdSolr.logo}
        </foreach>        
        ,timestamp =
        <foreach collection="list" item="wdSolr" index="index"
            separator=" " open="case id" close="end">
            when #{wdSolr.id} then #{wdSolr.timestamp}
        </foreach>
        where id in
        <foreach collection="list" item="wdSolr" index="index" 
            separator="," open="(" close=")">
            #{wdSolr.id}
        </foreach>
    </update>

批量删除

对应sql

delete from tablename where id in (1,2,3....)
<!-- 批量删除数据 -->
    <delete id="deleteBatch">
        delete from wd_solr where
        id in (
        <foreach collection="list" item="wdSolr" index="index"
            separator=",">
            #{wdSolr.id}
        </foreach>
        )
    </delete>

注:批量插入、更新不建议使用,如果数据过大,参数过多同时操作于一条sql会极度占用连接资源,建议使用循环操作单条执行语句。

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要记录 Mybatis 执行的增删操作的前后数据变化,可以使用 Mybatis 的插件机制来实现。具体实现步骤如下: 1. 定义一个拦截器,实现 Mybatis 的 Interceptor 接口,用于拦截 Mybatis 执行的 SQL 语句。 2. 在拦截器,通过反射获取执行 SQL 语句的 Mapper 接口和方法,并获取方法的参数和返回值。 3. 根据参数和返回值,判断执行的是增、删、操作,还是查询操作。 4. 如果是增、删、操作,记录操作前的数据,执行操作,记录操作后的数据,并将操作前后的数据变化插入到数据库。 5. 如果是查询操作,直接执行操作,不进行记录操作前后的数据变化。 以下是一个示例代码: ```java @Intercepts({ @Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class}), @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}) }) public class DataChangeInterceptor implements Interceptor { @Autowired private DataChangeService dataChangeService; @Override public Object intercept(Invocation invocation) throws Throwable { Object[] args = invocation.getArgs(); MappedStatement mappedStatement = (MappedStatement) args[0]; Object parameter = args[1]; Object result = null; String sqlId = mappedStatement.getId(); SqlCommandType sqlCommandType = mappedStatement.getSqlCommandType(); if (sqlCommandType == SqlCommandType.INSERT || sqlCommandType == SqlCommandType.UPDATE || sqlCommandType == SqlCommandType.DELETE) { // 记录操作前的数据 Object originalData = getOriginalData(parameter); // 执行操作 result = invocation.proceed(); // 记录操作后的数据 Object newData = getNewData(parameter); // 将操作前后的数据变化插入到数据库 dataChangeService.insertDataChange(sqlId, sqlCommandType, originalData, newData); } else { // 如果是查询操作,直接执行操作 result = invocation.proceed(); } return result; } private Object getOriginalData(Object parameter) { // TODO: 获取操作前的数据 return null; } private Object getNewData(Object parameter) { // TODO: 获取操作后的数据 return null; } } ``` 使用时,只需要在 Mybatis 的配置文件配置该拦截器即可: ```xml <plugins> <plugin interceptor="com.example.DataChangeInterceptor"/> </plugins> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值