(四)、性能分析、条件查询、自动生成器
1.性能分析插件
我们在平时的开发中,会遇到一些慢sql。测试!
MybatisPlus也提供性能分析插件,如果超过这个时间就停止运行!
- 导入插件
// 性能分析插件 @Bean @Profile({"dev","test"}) //设置dev test 环境开启,保证我们的效率 public PerformanceInterceptor performanceInterceptor(){ PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor(); // 在工作中,不允许用户等待 performanceInterceptor.setMaxTime(1000); //设置sql执行的最大时间,如果超过了则不执行 performanceInterceptor.setFormat(true); // 是否格式化代码 return performanceInterceptor; }
然后再我们的配置环境为dev或者test环境。
#设置开发环境 spring.profiles.active=dev
- 测试使用
@Test void contextLoads() { // 参数: 参数是一个wrapper,条件构造器。我们不添加条件就是查询全部信息 List<user> users = userMapper.selectList(null); // 语法堂便利信息 users.forEach(System.out::println);; }
如果在执行的时间里: 那么将会查出来结果,否则不会查出结果。
2.条件查询器Weapper
十分重要: Wrapper.
我们写一些复杂的sql就可以使用它来替代!
(1).不为空,大于且等于
@Test void contextLoads(){ // 查询name不为空的,并且邮箱不为空的用户,年龄大于12 QueryWrapper<user> wrapper = new QueryWrapper<>(); // 链式编程 wrapper .isNotNull("name") //名字不为空 .isNotNull("email") //邮箱不为空 .ge("age",12); //g是大于,e是等于 userMapper.selectList(wrapper).forEach(System.out::println); //遍历 }
(2).仅相等 且 唯一
@Test void test2(){ // 查询到指定的名字 QueryWrapper<user> wrapper = new QueryWrapper<>(); wrapper.eq("name","吉士先生"); // eq 就是说equal(); System.out.println(userMapper.selectOne(wrapper)); // 查询一个数据,出现多个结果使用List 或者 Map会出错 }
(3). 区间 和 查询的数目
@Test void test3(){ //查询年龄在20-30岁之间的用户 QueryWrapper<user> wrapper = new QueryWrapper<>(); wrapper.between("age","0","30"); //查询某一个字段的区间 System.out.println(userMapper.selectCount(wrapper)); // 查询结果数 }
(4).不包含某个值 且 以某个开头
@Test void test4(){ // 查询名字中不包含 吉 且 邮箱以 t 开头的 QueryWrapper<user> wrapper = new QueryWrapper<>(); wrapper .notLike("name","吉") //查询名字中不包含 吉 的数据 %吉% .likeRight("email","t"); // 查询开头是 t 的数据 也就是 t% List<Map<String, Object>> maps = userMapper.selectMaps(wrapper); maps.forEach(System.out::println); }
(5).IN 查询
@Test void test5(){ // id 在子查询中查出来 IN查询 QueryWrapper<user> wrapper = new QueryWrapper<>(); wrapper.inSql("id","select id from user where id<15"); // IN 查询 List<Object> objects = userMapper.selectObjs(wrapper); objects.forEach(System.out::println); }
(6).排序
@Test void test6(){ //通过id进行排序 QueryWrapper<user> wrapper = new QueryWrapper<>(); wrapper.orderByDesc("id"); // 降序 DESC 升序 ASC userMapper.selectList(wrapper).forEach(System.out::println); }
3.代码自动生成
dao、pojo、service、controller都给我自己去编写完成!
AutoGenerator 是 MyBatis-Plus 的代码生成器,通过 AutoGenerator 可以快速生成 Entity、
Mapper、Mapper XML、Service、Controller 等各个模块的代码,极大的提升了开发效率。
只需要改实体类名字 和包名 还有 数据库配置即可
我们在测试类上创建一个类,然后引入一下内容
package com.jsxs; import com.baomidou.mybatisplus.annotation.DbType; import com.baomidou.mybatisplus.annotation.FieldFill; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.generator.AutoGenerator; import com.baomidou.mybatisplus.generator.config.DataSourceConfig; import com.baomidou.mybatisplus.generator.config.GlobalConfig; import com.baomidou.mybatisplus.generator.config.PackageConfig; import com.baomidou.mybatisplus.generator.config.StrategyConfig; import com.baomidou.mybatisplus.generator.config.po.TableFill; import com.baomidou.mybatisplus.generator.config.rules.DateType; import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy; import java.util.ArrayList; // 代码自动生成 public class JsxsCode { public static void main(String[] args) { // 需要构建一个代码自动生成器 对象 AutoGenerator mpg = new AutoGenerator(); // 配置策略 // 1. 全局配置 GlobalConfig gc = new GlobalConfig(); String projectPath = System.getProperty("user.dir"); gc.setOutputDir(projectPath+"/src/main/java"); // 输出目录 gc.setAuthor("吉士先生"); //作者 gc.setOpen(false); //是否弹出文件夹在桌面 -否 gc.setFileOverride(false); //是否覆盖原来生成的 gc.setServiceName("%sService"); //去Service的I前缀 正则表达式 gc.setIdType(IdType.ID_WORKER); //主键生成策略 gc.setDateType(DateType.ONLY_DATE); //日期类型 gc.setSwagger2(true); //设置swagger // 2.丢到容器中 mpg.setGlobalConfig(gc); //3.设置数据源 DataSourceConfig dsc = new DataSourceConfig(); dsc.setUsername("root"); dsc.setPassword("121788"); dsc.setUrl("jdbc:mysql://localhost:3306/mybatis_plus?userUnicode=true&characterEncoding=utf-8"); dsc.setDriverName("com.mysql.jdbc.Driver"); dsc.setDbType(DbType.MYSQL); //数据库类型 mpg.setDataSource(dsc); //4.包的配置 PackageConfig pc = new PackageConfig(); pc.setModuleName("blog"); //模块名字 pc.setParent("com.jsxs"); //代码放在这里 pc.setEntity("entity"); //实体类的包名 pc.setMapper("mapper"); // 名字 pc.setService("service"); //名字 pc.setController("controller"); //名字 mpg.setPackageInfo(pc); // 5.策略配置->插件 StrategyConfig strategy = new StrategyConfig(); strategy.setInclude("user"); //********映射的表名********* strategy.setNaming(NamingStrategy.underline_to_camel); strategy.setColumnNaming(NamingStrategy.underline_to_camel); strategy.setEntityLombokModel(true); //自动Lombok // 逻辑删除 strategy.setLogicDeleteFieldName("deleted"); // 自动填充配置 TableFill gmtCreate = new TableFill("gmt_create", FieldFill.INSERT); //插入 TableFill gmtModified = new TableFill("gmt_modified", FieldFill.INSERT_UPDATE); //修改 ArrayList<TableFill> tableFills = new ArrayList<>(); tableFills.add(gmtCreate); tableFills.add(gmtModified); //添加到策略 strategy.setTableFillList(tableFills); // 乐观锁 strategy.setVersionFieldName("version"); strategy.setRestControllerStyle(true); strategy.setControllerMappingHyphenStyle(true); mpg.setStrategy(strategy); mpg.execute();; } }
需要导入俩依赖:MyBatis-Plus 从 3.0.3之后移除了代码生成器与模板引擎的默认依赖,需要手动添加相关依赖:
<!-- 自动生成代码依赖--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>3.3.2</version> </dependency> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity-engine-core</artifactId> <version>2.2</version> </dependency>