mybatis批处理

批量查询

方法一、(推介)

dao接口:

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

xml:

<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>

方法二、

dao接口:

	List<ReturnCodeEntity> selectByIds(@Param("ids") List<Long> ids);

xml:

<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> 

方法三、

dao接口:

	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>

批量插入

dao:

	int addResource(List<Resource> ResourceList);

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>

批量修改

dao:

	int updateRoles(List<String> roleList);

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>

批量删除

方法一、

dao:

	int deleteByLogic(List<Integer> listId);

xml:


<delete  id="deleteByLogic"  parameterType = "java.util.List">
     delete from user where 1>2
         or id in
      <foreach collection="list"  item="item" open="(" separator="," close=")"  >
           #{item}
      </foreach>
</delete>

方法二、

dao:

	int deleteByLogic(Integer[] array);

xml:


<delete  id="deleteByLogic"  parameterType = "Integer[]">
     delete from user where 1>2
         or id in
      <foreach collection="array"  item="item" open="(" separator="," close=")"  >
           #{item}
      </foreach>
</delete>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值