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