拼装 in 条件
入参为数组或者列表
Mapper 接口
List<TUser> selectForeach4In(String[] names);
Mapper xml文件
<select id="selectForeach4In" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from t_user a
where a.user_name in
<!--collection="list"-->
<foreach collection="array" open="(" close=")" item="userName" separator=",">
#{userName}
</foreach>
</select>
使用
String[] names = new String[]{"Jack","Rose"};
List<TUser> users = mapper.selectForeach4In(names);
批量插入
<insert id="insertForeach4Batch" useGeneratedKeys="true" keyProperty="id">
insert into t_user (userName, realName,
sex, mobile,email,note,
position_id)
values
<foreach collection="list" separator="," item="user">
(
#{user.userName,jdbcType=VARCHAR},
#{user.realName,jdbcType=VARCHAR},
#{user.sex,jdbcType=TINYINT},
#{user.mobile,jdbcType=VARCHAR},
#{user.email,jdbcType=VARCHAR},
#{user.note,jdbcType=VARCHAR},
#{user.position.id,jdbcType=INTEGER}
)
</foreach>
</insert>