有时候公司的业务中存在批量新增、更新、删除、查询的需求,有的人直接在业务层循环去处理,效率低并且容易使服务宕机
动态sql
查询
dao层入参需要加 @Param(“userList”)
select id from test where id in <foreach item="item" collection="userList" separator="," open="(" close=")" index=""> #{item.id , jdbcType=VARCHAR} </foreach>
增加
insert into Test(demoID, userId, createTime, "createUser", "type") values <foreach collection="testList" item="test" separator=","> (#{test.demoID,jdbcType=VARCHAR}, #{test.userId,jdbcType=VARCHAR}, #{test.createTime,jdbcType=TIMESTAMP}, #{test.createUser,jdbcType=TIMESTAMP}, #{test.type,jdbcType=VARCHAR}) </foreach>
修改
<update id="updateTest"> <foreach collection="Test" item="Test" separator=";"> update Test <set> <if test="Test.completionRatio != null and Test.completionRatio > 0"> CompletionRatio = #{Test.completionRatio,jdbcType=DECIMAL}, </if> </set> where id= #{Test.id} </foreach> </update>
删除
同查询
delete from test where id in <foreach item="item" collection="userList" separator="," open="(" close=")" index=""> #{item.id , jdbcType=VARCHAR} </foreach>
问题
传入的请求具有过多的参数。该服务器支持最多 2100 个参数。请减少参数的数目,然后重新发送该请求
解决方案链接: https://jiannuan.blog.csdn.net/article/details/124014506