一:查询拥有相同字段的数据
将需要查询相同字段作为数组传入,在mapper层传入数组,<foreach>一般是和in条件一起使用,collection是数组,需要注意的是在mapper方法中将要传入的数据使用@param注解制定传入参数名。
mapper层的代码
public interface UserMapper extends BaseMapper<UserDO> { /** * 分页查询数据 * * @param page 分页信息 * @param query 查询条件 * @param address 地址数组 * @return 分页数据 */ Page<UserDTO> queryPage(Page<?> page, @Param("query") UserPageVO query, @Param("address") String[] address); }
二:将address地址相同的数据合并展示,xml文件 其中的GROUP_CONCAT是用来合并的nysql函数
查询的结果显示:
这里传入一个数组为空时没有做任何的判断,mybatis会报错,所以需要用<if>标签判断传入的数组是否为空,为空则查找全部地区
<select id="queryPage" parameterType="com.myzy.talent.biz.business.user.model.vo.UserPageVO" resultType="com.myzy.talent.biz.business.user.model.dto.UserDTO">
SELECT address,GROUP_CONCAT(id SEPARATOR '、') id,GROUP_CONCAT(uname SEPARATOR '、') uname,GROUP_CONCAT(sex SEPARATOR '、') sex,
GROUP_CONCAT(age SEPARATOR '、') age,GROUP_CONCAT(email SEPARATOR '、') email
FROM biz_user
<where>
<if test="query.uname != null and query.uname != ''">
AND uname LIKE CONCAT('%',#{query.uname},'%')
</if>
<if test="query.sex != null and query.sex != ''">
AND sex LIKE CONCAT('%',#{query.sex},'%')
</if>
<if test="query.age != null and query.age != ''">
AND age LIKE CONCAT('%',#{query.age},'%')
</if>
<if test="query.address != null and query.address.length > 0">
AND address IN
<foreach collection="address" item="item" open="(" close=")" separator=",">
#{item}
</foreach>
</if>
<if test="query.address == null and query.address.length == 0">
AND address
</if>
<if test="query.email != null and query.email != ''">
AND email LIKE CONCAT('%',#{query.email},'%')
</if>
</where>
GROUP BY address
</select>