Justagreenonion 2020-05-14
<!--批量增加测试--> <insert id="insertList" parameterType="java.util.List"> insert into t_enterprise_water_ele ( /*方法一*/ -- WATER_ELE_ID, -- ENTERPRISE_ID, -- ENTERPRISE_USCC, -- ENTERPRISE_NAME, -- YEARMONTH, -- WATER_SIZE, -- WATER_AMOUNT, -- ELE_SIZE, -- ELE_AMOUNT, -- STATUS, -- OPERATOR, -- OPERATE_TIME /*方法二*/ <include refid="Base_Column_List"/> ) VALUES <foreach collection="list" item="item" index="index" separator=","> ( #{item.waterEleId,jdbcType=VARCHAR}, #{item.enterpriseId,jdbcType=VARCHAR}, #{item.enterpriseUscc,jdbcType=VARCHAR}, #{item.enterpriseName,jdbcType=VARCHAR}, #{item.yearmonth,jdbcType=VARCHAR}, #{item.waterSize,jdbcType=DECIMAL}, #{item.waterAmount,jdbcType=VARCHAR}, #{item.eleSize,jdbcType=DOUBLE}, #{item.eleAmount,jdbcType=VARCHAR}, #{item.status,jdbcType=INTEGER}, #{item.operator,jdbcType=VARCHAR}, #{item.operateTime,jdbcType=TIMESTAMP} ) </foreach> </insert>
int insertList(List<WaterEle> list);
foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合。 foreach元素的属性主要有 item,index,collection,open,separator,close。 item表示集合中每一个元素进行迭代时的别名 index指定一个名字,用于表示在迭代过程中,每次迭代到的位置 open表示该语句以什么开始 separator表示在每次进行迭代之间以什么符号作为分隔 符 close表示以什么结束 在使用foreach的时候最关键的也是最容易出错的就是collection属性,该属性是必须指定的,但是在不同情况 下,该属性的值是不一样的,主要有一下3种情况: 1.如果传入的是单参数且参数类型是一个List的时候,collection属性值为list 2.如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array 3.如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可以封装成map
使用批量插入执行的SQL语句应该等价于:
insert into redeem_code (batch_id, code, type, facevalue,create_user,create_time) values (?,?,?,?,?,? ),(?,?,?,?,?,? ),(?,?,?,?,?,? ),(?,?,?,?,?,? )
int deleteByBatch(@Param(value = "array")String[] array);
int deleteByBatch(@Param(value = "list")List list);
<delete id="deleteByBatch" parameterType="java.lang.String"> delete from t_enterprise_output_value where OUTPUT_ID IN <foreach collection="array" item="outputId" open="(" separator="," close=")"> #{outputId} </foreach> </delete>
因为表中没有fileIds字段,所以如果传map进入的话,需要在map中定义该字段
map.put("fileIds","1,2,3");
//美元符$直接注入(原样注入)
<delete id="deleteByPrimaryKey" parameterType="java.util.Map"> DELETE FROM t_attachment WHERE FILE_ID IN (${fileIds}) </delete>
DELETE FROM t_attachment WHERE FILE_ID IN (1,2,3)
适用于表中该字段是int或者bigint类型,不适用于varchar。
DELETE FROM t_attachment WHERE FILE_ID IN ("1","2","3")
数组/集合 -->(1,2,3,4) String userIdList = formData.get("userIdList"); String[] users = userIdList.split(","); String str = ""; for (String user : users) { str += "\"" + user + "\"" + ","; } String substring = str.substring(0, str.lastIndexOf(",")); System.out.println(substring);
如果删除不是以主键为条件,而是多个条件同时成立才可以删除 userIds可以是数组或者list或者map 上面的SYSCODE和ROLE_ID,可以直接传递 int deleteByUserIdSysRoleBatch(@Param(value="sysCode") String sysCode,@Param(value="roleId") String roleId,@Param(value="userIds")int[] userIds); int deleteByUserIdSysRoleBatch(@Param(value="sysCode") String sysCode,@Param(value="roleId") String roleId,@Param(value="userIds")List<Integer> userIds);
<delete id="deleteByUserIdSysRoleBatch"> delete from t_user_role where SYSCODE = #{sysCode,jdbcType=VARCHAR} AND ROLE_ID = #{roleId,jdbcType=VARCHAR} AND USER_ID IN <foreach collection="userIds" item="item" index="index" open="(" separator="," close=")"> #{item} </foreach> </delete>
<delete id="deleteByRole" parameterType="java.util.Map"> DELETE FROM t_user_role <where> <if test="userIdList != null"> USER_ID IN (#{userIdList,jdbcType=VARCHAR}) </if> <if test="roleId != null"> AND ROLE_ID=#{roleId,jdbcType=VARCHAR} </if> <if test="sysCode != null"> AND SYSCODE=#{sysCode} </if> </where> </delete>
<select id="selectOrder" parameterType="java.util.Map" resultMap="OrderMap"> SELECT * FROM t_ WHERE ORDER_ID in <foreach collection="list" item="item" index="index" open="(" separator="," close=")"> #{item} </foreach> </select>
<select id="selectOrder" parameterType="java.util.Map" resultMap="OrderMap"> SELECT * FROM t_ WHERE ORDER_ID in (${orderList}) </select>
==> Preparing: SELECT * FROM t_order WHERE ORDER_ID in (?) ==> Parameters: 1,2,5(String) <== Columns: ORDER_ID, GOODS_ID, UNIT_PRICE, SOLD_NUM, ORDER_STATUS, OPERATOR, OPERATE_TIME <== Row: 1, 356346093, 43.00, 3, 109002, yyadmin, 2017-12-16 21:01:20.0 <== Total: 1
注意"1,2,5"传过来的是String类型,实际sql语句如下:
SELECT * FROM t_order WHERE ORDER_ID in (#{orderList}) SELECT * FROM t_order WHERE ORDER_ID in (‘1,2,5‘)只能查到一条
将mapper.xml中的sql语句改为
SELECT * FROM t_order WHERE ORDER_ID in (${orderList}) SELECT * FROM t_order WHERE ORDER_ID in (1,2,5)就可以全部查到。
SELECT * FROM t_order WHERE ORDER_ID in (#{orderList}) SELECT * FROM t_order WHERE ORDER_ID in (‘1,2,5‘)查询不到
将mapper.xml中的sql语句改为
SELECT * FROM t_order WHERE ORDER_ID in (${orderList}) SELECT * FROM t_order WHERE ORDER_ID in (‘1‘,‘2‘,‘5‘)就可以全部
‘1,2,5‘转为‘1‘,‘2‘,‘5‘步骤如下:
String categoryIdList = (String) formData.get("categoryIdList"); if (!StringUtils.isEmpty(categoryIdList)) { String[] split = categoryIdList.split(","); String sqlParamter = ""; for (String s : split) { sqlParamter += "‘" + s + "‘,"; } String substring = sqlParamter.substring(0, sqlParamter.length() - 1); formData.put("categoryIdList", substring); }