码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-12-09 11:49:32      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:pre   代码   plain   sel   ids   lin   close   foreach   tail   

mybatis批量查询

 

注意这里的 in 和   <trim prefix="(" suffix=")"> 以及 in ( )的三种方式的(例1(推荐),例2,例3(推荐))等价使用

 例1:

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:

mapper接口代码:

public List<User> findUserListByIdList(List<Long> idList);    

xml代码:

技术图片
<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);

mapper.xml

技术图片
<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);

mapper.xml

技术图片
<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>
 
技术图片

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

标签:pre   代码   plain   sel   ids   lin   close   foreach   tail   

原文地址:https://www.cnblogs.com/zhuyeshen/p/12009819.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!