mybatis 批量更新数据

mybatis 批量更新数据

点击查看参考文章


JAVA定时任务代码

    /**
     * 每天凌晨1点执行定时更新用户vip时长、状态
     */
    @Scheduled(cron="0 0 1 * * ?")
    //@Scheduled(cron="0 1/1 * * * ? ")
    public void updateVipTask(){
        String baseurl = Global.getConfig("rw.kawu.baseurl");
        String secret = Global.getConfig("rw.kawu.secret");   //应用标识     
        String method = "/data/api/getAllUserPermission";

        Map<String,String> params=new TreeMap<String, String>();
        params.put("secret",secret); 

        JSONObject result= Client.sendGet(baseurl+method, params);
        if(result.getInteger("code").intValue()==200){
            JSONArray jsonArray = result.getJSONArray("data");
            List<Map<String, Object>> vipDaysLs = Lists.newArrayList(); 
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                Map<String, Object> map = Maps.newHashMap();
                map.put("userName", jsonObject.getString("ssoUserId"));
                map.put("totalDays", jsonObject.getInteger("effectiveTotleDay"));
                Integer remainingDays = jsonObject.getInteger("effectiveLeftDay");
                map.put("remainingDays", remainingDays);
                map.put("isVip", remainingDays.intValue()>0 ? 1 : 0);
                vipDaysLs.add(map);
            }
            // 根据 userName 更新用户的 isVip totalDays remainingDays
            fyWebuserService.updateVipDays(vipDaysLs);
        }
    }

mybatis xml 代码

    <update id="updateVipDays" parameterType="java.util.List">
        <!-- 写法一 效率低 -->
        <!-- <foreach collection="vipDaysLs" item="item" open="" close="" separator=";">
          update fy_webuser 
            <set>
                total_days = #{item.totalDays},
                remaining_days = #{item.remainingDays},
                is_vip = #{item.isVip}
            </set>
          where  user_name = #{item.userName}
        </foreach> -->

        <!-- 写法二 -->
        update fy_webuser  
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="total_days =case" suffix="end,">  
                 <foreach collection="list" item="item">  
                     <if test="item.totalDays !=null">  
                         when user_name = #{item.userName} then #{item.totalDays}  
                     </if>  
                 </foreach>  
            </trim>  
            <trim prefix="remaining_days =case" suffix="end,">  
                 <foreach collection="list" item="item">  
                     <if test="item.remainingDays !=null">  
                         when user_name = #{item.userName} then #{item.remainingDays}  
                     </if>  
                 </foreach>  
            </trim>  
            <trim prefix="is_vip =case" suffix="end">  
                 <foreach collection="list" item="item">  
                     <if test="item.isVip !=null">  
                         when user_name = #{item.userName} then #{item.isVip}  
                     </if>  
                 </foreach>  
            </trim>  
        </trim>  
         where user_name in
        <foreach collection="list" item="item" separator="," open="(" close=")">  
            #{item.userName}  
        </foreach>  
    </update>
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis是一个开源的持久层框架,它可以帮助开发者简化数据库操作的代码。它提供了一种将SQL语句与Java代码进行分离的方式,通过配置文件来实现数据库操作。 在MyBatis中,批量更新数据是一种高效的操作方式,可以减少与数据库的交互次数,提升性能。MyBatis提供了两种方式来实现批量更新数据: 1. 使用foreach标签:通过foreach标签可以遍历一个集合,并将集合中的元素作为参数传递给SQL语句。在SQL语句中可以使用动态SQL来生成批量更新的语句。例如: ```xml <update id="batchUpdate" parameterType="java.util.List"> UPDATE table_name SET column1 = #{listItem.property1}, column2 = #{listItem.property2} WHERE id = #{listItem.id} </update> ``` 在Java代码中,可以将需要批量更新数据封装成一个List对象,然后调用MyBatis的update方法执行批量更新操作。 2. 使用批量操作的API:MyBatis提供了SqlSession的批量操作API,可以通过调用`insert`、`update`、`delete`等方法来实现批量操作。例如: ```java List<Object> dataList = new ArrayList<>(); // 添加需要更新数据到dataList中 try (SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH)) { for (Object data : dataList) { sqlSession.update("namespace.batchUpdate", data); } sqlSession.commit(); } ``` 在上述代码中,通过SqlSession的`openSession`方法创建一个批量操作的SqlSession对象,然后循环调用`update`方法执行批量更新操作,最后调用`commit`方法提交事务。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值