mybatis mapper.xml生成_MybatisPlus(条件构造器与代码生成器)

性能分析插件

MP提供性能分析插件,如果超过这个时间就停止运行(慢SQL),用于输出每条 SQL 语句及其执行时间。(3.2版本移除了该插件,现在以较低版本测试)

  1. 导入插件

@Bean@Profile({"dev","test"})  //设置dev 、test环境开启,保证效率public PerformanceInterceptor performanceInterceptor(){    PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor();    performanceInterceptor.setMaxTime(1);  //ms  设置sql执行的最大时间,如果超过了这个时间就不执行    performanceInterceptor.setFormat(true);  //是否格式化代码    return performanceInterceptor;}
  1. 测试使用

0ab6b67894da15b706c0514a78865cfa.png

只要超过规定的时候就会抛出异常。

使用性能分析插件,可以提高效率

条件构造器

一些复杂的SQL可以用它来替代。

测试一:

   void contextLoads() {       //查询name不为空的用户,并且邮箱不为空的用户,年龄大于等于12        QueryWrapper wrapper = new QueryWrapper<>();        wrapper.isNotNull("name").isNotNull("email").ge("age",12);        userMapper.selectList(wrapper).forEach(System.out::println); //    }}

0efd5cd85e36f5047bf5072ecedeb1fb.png

测试二:

@Testvoid test2(){    //查询名字等于ironman    QueryWrapper wrapper = new QueryWrapper<>();    wrapper.eq("name","zhangxi");    User user = userMapper.selectOne(wrapper);  //查询一个数据,出现多个结果使用List或者Map    System.out.println(user);}

da0b9906f089b30b87f966217bcd2384.png

测试三:

@Testvoid test3(){    //查询年龄在20~30岁之间的用户    QueryWrapper wrapper = new QueryWrapper<>();    wrapper.between("age",20,30);//区间    Integer count = userMapper.selectCount(wrapper);//查询结果数    System.out.println(count);}

测试四:

//模糊查询@Testvoid test4(){    QueryWrapper wrapper = new QueryWrapper<>();    //左和右 %e%    wrapper.notLike("name","e").likeRight("email","t");    List<Map<String, Object>> maps = userMapper.selectMaps(wrapper);    maps.forEach(System.out::println);}

测试五:

@Testvoid test5(){    QueryWrapper wrapper = new QueryWrapper<>();    //id在子查询中查出来    wrapper.inSql("id","select id from user where id < 3");        List<Object> objects = userMapper.selectObjs(wrapper);    objects.forEach(System.out::println);}

测试六:

@Testvoid test6(){    QueryWrapper wrapper = new QueryWrapper<>();    //通过id进行排序    wrapper.orderByDesc("id");    List users = userMapper.selectList(wrapper);    users.forEach(System.out::println);}

代码生成器

AutoGenerator 是 MyBatis-Plus 的代码生成器,通过 AutoGenerator 可以快速生成 Entity、Mapper、Mapper XML、Service、Controller 等各个模块的代码,极大的提升了开发效率。

测试:

package com.zhang.mybatis_plus;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 CodeGen {    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/zhang");        gc.setAuthor("zhang");        gc.setOpen(false);        gc.setFileOverride(false);//是否覆盖        gc.setServiceName("%sService");//去service的I前缀        gc.setIdType(IdType.ID_WORKER);        gc.setDateType(DateType.ONLY_DATE);        gc.setSwagger2(true);        mpg.setGlobalConfig(gc);        //2. 设置数据源        DataSourceConfig dsc = new DataSourceConfig();        dsc.setUrl("jdbc:mysql://localhost:3306/mybatis_plus?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8");        dsc.setDriverName("com.mysql.cj.jdbc.Driver");        dsc.setUsername("root");        dsc.setPassword("01194610");        dsc.setDbType(DbType.MYSQL);        mpg.setDataSource(dsc);        //3. 包的配置        PackageConfig pc = new PackageConfig();        pc.setModuleName("blog");        pc.setParent("com.zhang");        pc.setEntity("entity");        pc.setMapper("mapper");        pc.setService("service");        pc.setController("controller");        mpg.setPackageInfo(pc);        //4, 策略配置        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 gmtModify = new TableFill("gmt_modified", FieldFill.INSERT_UPDATE);        ArrayList<TableFill> tablefills = new ArrayList<>();        tablefills.add(gmtCreate);        tablefills.add(gmtModify);        strategy.setTableFillList(tablefills);        //乐观锁        strategy.setVersionFieldName("version");        strategy.setRestControllerStyle(true);        strategy.setControllerMappingHyphenStyle(true);//localhost:8080/hello_id_2,使用下划线命名        mpg.setStrategy(strategy);        mpg.execute();//执行    }}

fa0d1c6c986c8fe44f66895b0e086cc5.png

0daf10c54808fd0908febe56de765fcf.gif
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值