Mybatis批处理(批量查询,更新,插入)
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>
相关推荐
smalllove 2020-11-03
wuhuaguwuhuaguo 2020-09-15
guying 2020-08-16
wqcong 2020-06-14
chenzhiwei 2020-06-13
talkingDB 2020-05-05
83206837 2020-05-01
Yasin 2020-03-03
Amzaing00 2020-01-06
GechangLiu 2020-01-05
shenghua 2013-03-27
文山羊 2019-12-16
乾坤一碼農 2019-12-11
bluestartlxp 2019-11-27
86427217 2015-04-18
thankinglove 2015-09-06
圆圆的世界CSDN 2019-11-07
longxiang 2010-04-07
happygongzhuo 2008-09-04