Mybatis plus入门(二):单表查询,自定义sql
目录
- 模糊、分页、不统计数量、使用自定义的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>
分类:
后端 / 对象关系映射
标签:
MyBatis Plus
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术