UserDao.java dao中,函数的参数需要设置@Param
List<User> getUsers(@Param("teamMember")User user, @Param("teamIds")List<Long> teamIds);
mapper.xml 中,使用
对象. 的形式获取参数 (如:teamMember.teamId)
teamIds != null and teamIds.size() > 0 判断list是否为空以及是否有数据
${item} 有两种用法,${item} 和 #{item},使用#号,会在生成sql的时候自动用占位符占位。
<select id="getUsers" resultMap="UserMap"> select tm.team_id, u.uid, u.real_name name, tm.member_type, tm.create_time,tm.member_number,u.avatar,u.position,t.name team_name,t.city_name from lop_team_member tm left join user_info u on tm.uid = u.uid LEFT JOIN lop_team t ON tm.team_id = t.team_id <where> <if test="teamMember.teamId != null"> and tm.team_id = #{teamMember.teamId,jdbcType=BIGINT} </if> <if test="teamMember.name != null"> and u.real_name = #{teamMember.name,jdbcType=VARCHAR} </if> <if test="teamIds != null and teamIds.size() > 0"> and tm.team_id not in <foreach collection="teamIds" index="index" item="item" open="(" separator="," close=")"> ${item} </foreach> </if> and u.phone is null </where> </select>