方法1:xml方式传Map
(优点:1.sql与java代码解耦;
2.传入多个查询条件时,可以在xml中自由定义,如添加<if test="fieldName != null"></if>, order by time desc 等)
CommentMapper.java 添加代码
/**
* 分页查询方法1:xml方式传Map
* @param pg
* @param map
* @return
*/
Page<CommentVo> pageCommentVoByMap(Page<CommentVo> pg, Map<String, Object> map);
CommentMapper.xml 添加代码
<select id="pageCommentVoByMap" parameterType="java.util.Map" resultType="com.geline.system.pojo.CommentVo">
SELECT id, user_id, user_name, user_avatar, reply_num, praise_num, content, create_time
FROM biz_comment
WHERE 1=1 and is_deleted=0
<if test="map.feedbackType != null and map.feedbackType != ''">
and feedback_type = #{map.feedbackType}
</if>
order by create_time desc
</select>
CommentServiceImpl.java 添加代码
public PageResultVo<CommentVo> pageCommentVo(Integer pageNum, Integer pageSize, String feedbackType) {
Map<String, Object> example = new HashMap<>();
example.put("feedbackType", feedbackType);
Page<CommentVo> pageCommentVo = baseMapper.pageCommentVoByMap(new Page(pageNum, pageSize), example);
return PageResultVo.build(pageCommentVo);
}
方法2:注解方式(传简单参数)
(优点:适合查询简单的过滤条件,如 where type = #{type} ;
缺点:不适合有复杂的过滤条件,如判断 !=null && !="",建议使用QueryWrapper方法处理或xml方式)
CommentMapper.java 添加代码
//分页查询方法2:注解方式(直接传参数)
@Select({
"SELECT",
"id, user_id, user_name, user_avatar, reply_num, praise_num, content, create_time",
"FROM biz_comment",
"WHERE 1=1 and is_deleted=0 and feedback_type = #{feedbackType}"
})
Page<CommentVo> selectPageCommentVo(Page<CommentVo> pg, String feedbackType);
CommentServiceImpl.java 添加代码:
@Override
public PageResultVo<CommentVo> pageCommentVo(Integer pageNum, Integer pageSize, String feedbackType) {
Page<CommentVo> pageCommentVo = baseMapper.selectPageCommentVo(new Page(pageNum, pageSize), feedbackType);
return PageResultVo.build(pageCommentVo);
}
方法3:注解方式(传Map参数)
(优点:适合有多个的过滤条件,如 where type = #{map.type} and name like '%${map.name}%';
缺点:不适合有复杂的过滤条件,如判断 !=null && !="",建议使用QueryWrapper方法处理或xml方式)
CommentMapper.java 添加代码
//分页查询方法3:注解方式(传Map参数)
@Select({
"SELECT",
"id, user_id, user_name, user_avatar, reply_num, praise_num, content, create_time",
"FROM biz_comment",
"WHERE 1=1 and is_deleted=0 and feedback_type = #{map.feedbackType}"
})
Page<CommentVo> selectPageCommentVoByMap(Page<CommentVo> pg, Map<String, Object> map);
CommentServiceImpl.java 添加代码
@Override
public PageResultVo<CommentVo> pageCommentVo(Integer pageNum, Integer pageSize, String feedbackType) {
Map<String, Object> example = new HashMap<>();
example.put("feedbackType", feedbackType);
Page<CommentVo> pageCommentVo = baseMapper.selectPageCommentVoByMap(new Page(pageNum, pageSize), example);
return PageResultVo.build(pageCommentVo);
}