SSM笔记-MyBatis_foreach用法,批量新增

笔记-MyBatis_foreach用法,批量新增

小白自学记,第一次写,留个笔记,免得忘记了~~~~
不多说,直接记录
Dao层接口:

	/**
	 * 批量新增用户数据
	 * @param listInfo 
	 * @return
	 * @throws Exception
	 */
	int batchInsert(List<UserInfo> listInfo) throws Exception;

Service层和ServiceImpl:

/**
     * 批量新增用户数据
     * @param listInfo
     * @return
     * @throws Exception
     */
    boolean batchInsert(List<UserInfo> listInfo) throws Exception;
@Override
	public boolean batchInsert(List<UserInfo> listInfo) throws Exception {
		// TODO Auto-generated method stub
		int result = testListMapper.batchInsert(listInfo);		
		return result==0?false:true;
	}

mapper.xml:

<!-- 批量新增用户 -->
    <insert id="batchInsert" parameterType="java.util.List">
        insert into <include refid="userInfo" />
            <trim prefix="(" suffix=")" suffixOverrides=",">
            		nickName,
	                userName,
	                userPwd,
	                realPwd,
	                userGender,
	                phoneNo,
	                registerDate,
	                ext3,
        	</trim>
        values
        <foreach collection="list" index="index" item="userInfo" separator=",">
        	<trim prefix="(" suffix=")" suffixOverrides=",">
            	<if test="userInfo.nickName != null and userInfo.nickName != ''">
            		#{userInfo.nickName},
            	</if>
	            <if test="userInfo.userName != null and userInfo.userName != ''">
	                #{userInfo.userName},
	            </if>
	            <if test="userInfo.userPwd != null and userInfo.userPwd != ''">
	                #{userInfo.userPwd},
	            </if>
	            <if test="userInfo.realPwd != null and userInfo.realPwd != ''">
	                #{userInfo.realPwd},
	            </if>
	            <if test="userInfo.userGender != null and userInfo.userGender != ''">
	                #{userInfo.userGender},
	            </if>
	            <if test="userInfo.phoneNo != null and userInfo.phoneNo != ''">
	                #{userInfo.phoneNo},
	            </if>
	            <if test="userInfo.address != null and userInfo.address != ''">
	                #{userInfo.address},
	            </if>
	                now(),
	            <if test="userInfo.ext1 != null and userInfo.ext1 != ''">
	                #{userInfo.ext1},
	            </if>
	            <if test="userInfo.ext2 != null and userInfo.ext2 != ''">
	                #{userInfo.ext2},
	            </if>
	                0,
        	</trim>
    	</foreach>
    </insert>

MyBatis的foreach详解参考:https://www.cnblogs.com/fnlingnzb-learner/p/10566452.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值