mybatis-puls条件查询、分页功能

MyBatis-Plus (opens new window)(简称 MP)是一个 MyBatis (opens new window)的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。

一、代码生成

在test中写,读者需要自行看里面需要填写的内容,里面都是适合我自己内容。

@Testvoid genCode(){// 1、创建代码生成器AutoGenerator mpg = new AutoGenerator();// 2、全局配置GlobalConfig gc = new GlobalConfig();String projectPath = System.getProperty("user.dir");gc.setOutputDir("D:\\大二上\\SpringBoot\\springboot01_08\\src\\main\\java");gc.setAuthor("wp");gc.setOpen(false); //生成后是否打开资源管理器gc.setFileOverride(false); //重新生成时文件是否覆盖gc.setServiceName("%sService");   //去掉Service接口的首字母Igc.setIdType(IdType.AUTO); //主键策略gc.setDateType(DateType.ONLY_DATE);//定义生成的实体类中日期类型gc.setSwagger2(false);//开启Swagger2模式mpg.setGlobalConfig(gc);// 3、数据源配置DataSourceConfig dsc = new DataSourceConfig();dsc.setUrl("jdbc:mysql://localhost:3306/123?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC");dsc.setDriverName("com.mysql.cj.jdbc.Driver");dsc.setUsername("root");dsc.setPassword("zjw241014");dsc.setDbType(DbType.MYSQL);mpg.setDataSource(dsc);// 4、包配置PackageConfig pc = new PackageConfig();pc.setModuleName("springboot01_08"); //模块名pc.setParent("com.iwei");pc.setController("controller");pc.setEntity("model");pc.setService("service");pc.setMapper("dao");mpg.setPackageInfo(pc);// 5、策略配置StrategyConfig strategy = new StrategyConfig();strategy.setInclude("myuser","student");strategy.setNaming(NamingStrategy.underline_to_camel);//数据库表映射到实体的命名策略strategy.setTablePrefix(pc.getModuleName() + "_"); //生成实体时去掉表前缀strategy.setColumnNaming(NamingStrategy.underline_to_camel);//数据库表字段映射到实体的命名策略strategy.setEntityLombokModel(true); // lombok 模型 @Accessors(chain = true) setter链式操作strategy.setRestControllerStyle(true); //restful api风格控制器strategy.setControllerMappingHyphenStyle(true); //url中驼峰转连字符mpg.setStrategy(strategy);// 6、执行mpg.execute();}

二、代码生成后项目结构 三、新建数据库中一个表

学生的数据库是我之前自己创建的,大家练习条件查询和分页功能可以就使用该数据库中的mysuer表。

CREATE TABLE myuser
(id BIGINT(20) NOT NULL COMMENT '主键ID',name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',age INT(11) NULL DEFAULT NULL COMMENT '年龄',email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱',PRIMARY KEY (id)
);
​
INSERT INTO myuser(id, name, age, email) VALUES
(1, 'Jone', 18, 'test1@baomidou.com'),
(2, 'Jack', 20, 'test2@baomidou.com'),
(3, 'Tom', 28, 'test3@baomidou.com'),
(4, 'Sandy', 21, 'test4@baomidou.com'),
(5, 'Billie', 24, 'test5@baomidou.com');

四、yaml配置

虽然mybatis-puls帮我们生成了很多代码,但是还是要在yaml里面连接数据库和一些基础配置

server:port: 80
spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/数据库名?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTCusername: root(用户名)password: 密码thymeleaf:cache: false
mybatis-plus:configuration:log-impl: org.apache.ibatis.logging.stdout.StdOutImplmapper-locations: classpath:mapper/*.xml

五、test启动类中条件查询测试

 @Testvoid eq(){QueryWrapper<Student> wrapper=new QueryWrapper<>();wrapper.eq("name","张俊伟");studentService.list(wrapper);}@Testvoid gt(){QueryWrapper<Student> wrapper=new QueryWrapper<>();wrapper.gt("age",18);studentService.list(wrapper);}@Testvoid lt(){QueryWrapper<Student> wrapper=new QueryWrapper<>();wrapper.lt("age",20);studentService.list(wrapper);}@Testvoid like(){QueryWrapper<Student> wrapper=new QueryWrapper<>();wrapper.like("name","张%");studentService.list(wrapper);}@Testvoid and(){QueryWrapper<Student> wrapper=new QueryWrapper<>();wrapper.lt("id",3).gt("age",18);studentService.list(wrapper);}@Testvoid or(){QueryWrapper<Student> wrapper = new QueryWrapper<>();wrapper.lt("age",18).or().gt("id",8);studentService.list(wrapper);}

六、分页功能

我们需要配置分页插件
在config包下

@Configuration
public class pageConfig {public class MybatisPlusConfig {@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();PaginationInnerInterceptor pageInterceptor = new PaginationInnerInterceptor();pageInterceptor.setOverflow(false);pageInterceptor.setMaxLimit(500L);pageInterceptor.setDbType(DbType.MYSQL);interceptor.addInnerInterceptor(pageInterceptor);return interceptor;}}
}

测试代码

 @Testvoid page(){IPage<Student> studentPage= new Page<>(2,4);studentService.page(studentPage,null);System.out.println(studentPage.getRecords());System.out.println(studentPage.getPages());System.out.println(studentPage.getTotal());System.out.println(studentPage.getCurrent());}

七、测试结果示例

mybatis-puls条件查询、分页功能相关推荐

  1. asp.net中条件查询+分页

    大家好,我是雄雄,欢迎关注公众号:雄雄的小课堂. 今天,分享的是asp.net中条件查询+分页的小案例. 如下图所示: 各个功能模块均已标注,先大致介绍一下业务. \1. 点击左侧图书类别,根绝类别编 ...

  2. springDataJpa入门教程(5)-单表动态条件查询+分页

    springDataJpa入门教程 springDataJpa入门教程(1)-基于springBoot的基本增删改查 springDataJpa入门教程(2)-Specification动态条件查询+ ...

  3. Mybatis Plus 入门 简单的CRUD 使用详解 条件查询 分页查询 DML操作 MP代码生成器

    Mybatis Plus入门 MP是 MybatisPlus,简称MP,是一个 Mybatis 的增强工具,在 Mybatis 的基础上只做增强不做改变.MP为简化开发.提高效率而生. 它已经封装好了 ...

  4. SSM整合 mybatis多条件查询与分页

    多条件查询与分页: 通过页面的houseName.floorage获取值传到前端视图(HouseSearchVO)实体类中的houseName,floorage建立houseSearchVO对象. 通 ...

  5. Mybatis Plus条件查询

    QueryWrapper 说明:继承自 AbstractWrapper ,自身的内部属性 entity 也用于生成 where 条件及 LambdaQueryWrapper, 可以通过 new Que ...

  6. vue+node多条件查询 分页_SpringBoot+JPA框架分页、带条件查询等操作

    前言 最近研究JPA框架,初学SpringBoot时也简单学过,但是不是很深入,所以这次主要是说一些进阶且常用.实用的操作! 前置准备 创建两张表或者让JPA自动建表,任意选择!学生表 package ...

  7. ASPNETPager条件查询分页的实现

    利用ASPNETPager支持URL分页的功能实现条件查询,翻页条件依然存在的效果,不使用viewstate,session之类的东西. 思路: 在条件查询按钮后台代码上进行页面的Redirect,U ...

  8. mysql 条件查询分页_百万数据下mysql条件查询及分页查询的注意事项

    接上一节<百万数据mysql分页问题>,我们加上查询条件:select id from news where cate = 1 order by id desc limit 500000 ...

  9. MyBatis框架学习笔记04:利用MyBatis实现条件查询

    文章目录 一.查询需求 二.打开MyBatisDemo项目 三.对学生表实现条件查询 (一)创建学生映射器配置文件 (二)在MyBatis配置文件里注册学生映射器配置文件 (三)创建学生映射器接口 ( ...

  10. Spring Data JPA 条件查询 分页查询

    条件查询 //Label : 实体类,传过来的参数 labelDao.findAll(new Specification<Label>(){@Overridepublic Predicat ...

最新文章

  1. Docker入门六部曲——基本引导
  2. 天天用着Redis集群,主从同步该知道吧?集群工作原理是否需要了解下?
  3. 《系统集成项目管理工程师》必背100个知识点-89行政收尾和合同收尾的区别
  4. xhr如何发送post请求_js实现ajax的post请求步骤
  5. feign.RetryableException null executing post
  6. 第9章 Python Web 框架考察点
  7. Centos7jdk安装
  8. C++(STL):08---vector元素访问
  9. 当ORACLE归档日志满后如何正确删除归档日志
  10. Maven学习总结(1)——Maven入门
  11. html5对属性布尔的值设定,如何在Javascript中设置HTML5必需属性?
  12. 主板开启网络唤醒(Wake on lan)
  13. linux环境下tomcat启动成功,部分请求页面出现404
  14. html鼠标悬停文字变颜色,鼠标悬停,文字颜色逐渐改变是怎样实现的??
  15. 2021赣网杯web和misc部分wp
  16. 我用FreeMind
  17. GDS List内容详情
  18. 算法系列——贝尔曼福特算法(Bellman-Ford)
  19. Angular属性绑定,class绑定,事件绑定,属性样式绑定
  20. 开发团队建设与管理的一些心得

热门文章

  1. SecureCRT右键复制设置
  2. 诺基亚手机运行linux,早年拥抱linux却死在同为linux内核的安卓系统上——诺基亚N900...
  3. vs2013建立html页面,使用 Visual Studio 2013 创建基本 ASP.NET 4.5 Web 窗体页
  4. ios设备解锁---iToolab UnlockGo mac
  5. IBM Rational Top Gun 培训的两点感悟
  6. mysql 安装 gun linux_Debian Gun/linux基本用法
  7. java调用序列_基于JAVA的苹果序列号接口调用代码实例
  8. Windows C++ 多线程编程示例
  9. [Linux]Linux中设置默认浏览器
  10. 网络黑客攻防学习平台之基础关第三题