// 第几页 int currentPage = 2; // 每页数量 int pageSize = 5; // 排序 String orderBy = "id desc"; PageHelper.startPage(currentPage, pageSize, orderBy); List<Map<String, Object>> todos = testMapper.findTodos(); PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(todos); log.info(pageInfo.toString());
return todos;
like 模糊查询
在代码中加上%
1
List<User> test = userMapper.findUserByLikeName2("%" +name+"%");
1 2 3
<selectid="findUserByLikeName2"parameterType="java.lang.String"resultMap="user"> select * from t_user where name like #{name,jdbcType=VARCHAR} </select>
动态 SQL
if
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<selectid="findActiveBlogWithTitleLike"resultType="Blog"> SELECT * FROM BLOG WHERE state = ‘ACTIVE’ <iftest="title != null"> AND title like #{title} </if> <iftest="author != null and author.name != null"> AND author_name like #{author.name} </if> </select>
choose、when、otherwise
从多个条件中选择一个使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<selectid="findActiveBlogLike"resultType="Blog"> SELECT * FROM BLOG WHERE state = ‘ACTIVE’ <choose> <whentest="title != null"> AND title like #{title} </when> <whentest="author != null and author.name != null"> AND author_name like #{author.name} </when> <otherwise> AND featured = 1 </otherwise> </choose> </select>
where
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<selectid="findActiveBlogLike"resultType="Blog"> SELECT * FROM BLOG <where> <iftest="state != null"> state = #{state} </if> <iftest="title != null"> AND title like #{title} </if> <iftest="author != null and author.name != null"> AND author_name like #{author.name} </if> </where> </select>