目录
- 模糊、分页、不统计数量、使用自定义的selectByPage方法,传入参数Page和LambdaQueryWrapper
- 模糊、分页、统计数量、使用自定义的selectByPage方法,传入参数Page和LambdaQueryWrapper
代码案例
- 模糊、分页、不统计数量、使用自定义的selectByPage方法,传入参数Page和LambdaQueryWrapper
@Component
public interface UserMapper extends BaseMapper<User> {
IPage<User> selectByPage(IPage<User> userPage, @Param(Constants.WRAPPER) Wrapper<User> userWrapper);
}
<select id="selectByPage" resultType="com.ychen.mybatis.demo01.model.User">
select * from user ${ew.customSqlSegment}
</select>
@RequestMapping("/test4")
@ResponseBody
public String test4(){
// 构建wrapper对象
LambdaQueryWrapper<User> userLambdaQueryWrapper = Wrappers.lambdaQuery();
// 模糊查询
userLambdaQueryWrapper.like(User::getUsername , "a");
// 分页
Page<User> mapPage = new Page<>(1, 2, false);
// 调用mapper层方法
IPage<User> mapIPage = userMapper.selectByPage(mapPage , userLambdaQueryWrapper);
System.out.println("总页数: "+mapIPage.getPages());
System.out.println("总记录数: "+mapIPage.getTotal());
// 打印出查询结果
mapIPage.getRecords().forEach(System.out::println);
return "success";
}
- 模糊、分页、统计数量、使用自定义的selectByPage方法,传入参数Page和LambdaQueryWrapper
@RequestMapping("/test")
@ResponseBody
public String test(){
Integer current = 1;
Integer size = 3;
String username = "c";
String sex = "1";
// 构建wrapper对象
LambdaQueryWrapper<User> userLambdaQueryWrapper = Wrappers.lambdaQuery();
// 构建page
Page<User> mapPage = new Page<>(current, size, true);
// 调用mapper层方法
IPage<User> mapIPage = userMapper.selectByPage(username, sex, mapPage , userLambdaQueryWrapper);
System.out.println("总页数: "+mapIPage.getPages());
System.out.println("总记录数: "+mapIPage.getTotal());
// 打印出查询结果
mapIPage.getRecords().forEach(System.out::println);
return "success";
}
IPage<User> selectByPage(@Param("username") String username, @Param("sex") String sex, IPage<User> userPage, Param(Constants.WRAPPER) Wrapper<User> userWrapper);
<select id="selectByPage" resultType="com.ychen.mybatis.model.User">
select * from user
<where>
<if test="username !=null and username!=''">
and username like concat('%',#{username},'%')
</if>
<if test="sex !=null and sex!=''">
and sex = #{sex}
</if>
</where>
${ew.customSqlSegment}
</select>