2025年Mybatis批处理(批量查询,更新,插入)

Mybatis批处理(批量查询,更新,插入)mybatis 批量查询 注意这里的 in 和 以及 in 的三种方式的 例 1 推荐 例 2 例 3 推荐 等价使用 例 1 List lt UBaseMenu gt findMenuName List lt String gt

大家好,我是讯享网,很高兴认识大家。

mybatis批量查询
注意这里的 in 和 以及 in ( )的三种方式的(例1(推荐),例2,例3(推荐))等价使用

例1:

List<UBaseMenu> findMenuName(List<String> valueList); 

讯享网
讯享网<select id="findMenuName" resultType="java.lang.String" parameterType="java.util.List"> select menu_name from menu where menu_id in <foreach collection="list" item="valueList" open="(" close=")" separator=","> #{ 
   valueList} </foreach> </select> 

例2:

List<ReturnCodeEntity> selectByIds(@Param("ids") List<Long> ids); 
讯享网<select id="selectByIds" parameterType="java.util.List" resultType="com.paic.ocss.gateway.model.entity.ReturnCodeEntity"> SELECT id, code, message, recommendation, status FROM openapi_return_code WHERE id in <trim prefix="(" suffix=")"> <foreach collection="ids" index="index" item="id" separator=","> #{ 
   id} </foreach> </trim> </select> 

例3:


讯享网

public List<User> findUserListByIdList(List<Long> idList); 
讯享网<select id="findUserListByIdList" parameterType="java.util.ArrayList" resultType="User"> select * from user user <where> user.ID in ( <foreach collection="list" item="id" index="index" separator=","> #{ 
   id} </foreach> ) </where> </select> 

批量插入:

mapper.java

int addResource(List<Resource> ResourceList); 
讯享网<insert id="addResource" parameterType="java.util.List"> insert into resource (object_id, res_id, res_detail_value, res_detail_name) values <foreach collection="list" item=" ResourceList " index="index" separator=","> (#{ 
   ResourceList.objectId,jdbcType=VARCHAR}, #{ 
   ResourceList.resId,jdbcType=VARCHAR}, #{ 
   ResourceList.resDetailValue,jdbcType=VARCHAR}, #{ 
   ResourceList.resDetailName,jdbcType=VARCHAR} ) </foreach> </insert> 

批量更新:

mapper.java

int updateRoles(List<String> roleList); 
讯享网<update id="updateRoles" parameterType="java.util.List"> update role set enabled = '0' where role_id in <foreach collection="list" item="roleIds" index="index" open="(" separator="," close=")"> #{ 
   roleIds} </foreach> </update> 
小讯
上一篇 2025-03-07 09:33
下一篇 2025-01-27 23:57

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/59013.html