MyBatis-Plus 高阶用法

文档来源

官方

条件构造器

ge

        // 查询name不为空的用户,并且邮箱不为空的emp,年龄大于等于23
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        wrapper
                .isNotNull("last_name")
                .isNotNull("email")
                .ge("age",23);
        employeeMapper.selectList(wrapper).forEach(System.out::println);

eq

        // 查询name为Tom的用户
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        wrapper.eq("last_name","Tom");
        // 查询多个使用list或者map
        Employee employee = employeeMapper.selectOne(wrapper);
        System.out.println(employee);

between

        // 查询年龄在 18~24 之间的用户
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        wrapper.between("age",18,24);
        Integer count = employeeMapper.selectCount(wrapper);
        System.out.println(count);

like

        // 模糊查询
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        wrapper
                // 名字不是 %m%
                .notLike("last_name","m")
                // 左:%Aca  右:Aca%
                .likeRight("email","Aca");

        List<Map<String, Object>> maps = employeeMapper.selectMaps(wrapper);
        maps.forEach(System.out::println);

inSql(联表)

        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        // id在子查询中查出来
        wrapper.inSql("id","select id from employee where id < 4");
        List<Object> objects = employeeMapper.selectObjs(wrapper);
        objects.forEach(System.out::println);

orderby

        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        // 通过id进行排序
        wrapper.orderByDesc("id");
        List<Employee> employees = employeeMapper.selectList(wrapper);
        employees.forEach(System.out::println);

代码自动生成器

/**
 * @Author: XF-DD
 * @Date: 2020/7/9 15:07
 */
public class CodeGenerator {
    /**
     * 

* 读取控制台内容 *

*/
public static String scanner(String tip) { Scanner scanner = new Scanner(System.in); StringBuilder help = new StringBuilder(); help.append("请输入" + tip + ":"); System.out.println(help.toString()); if (scanner.hasNext()) { String ipt = scanner.next(); if (StringUtils.isNotEmpty(ipt)) { return ipt; } } throw new MybatisPlusException("请输入正确的" + tip + "!"); } public static void main(String[] args) { // 代码生成器 AutoGenerator mpg = new AutoGenerator(); // 全局配置 GlobalConfig gc = new GlobalConfig(); //生成的代码输出路径,自己根据需要修改 String projectPath = "D:\\projects\\Yeion\\m-business-service\\business-service"; // gc.setOutputDir(projectPath + "/src/main/java"); gc.setAuthor("BNMZY"); // gc.setAuthor("yuwei"); gc.setOpen(false); gc.setFileOverride(true); gc.setSwagger2(true); gc.setActiveRecord(true); gc.setBaseColumnList(true); gc.setBaseResultMap(true); gc.setIdType(IdType.ID_WORKER); mpg.setGlobalConfig(gc); // 数据源配置 DataSourceConfig dsc = new DataSourceConfig(); dsc.setUrl("jdbc:mysql://120.79.18.7:32553/yunpai?characterEncoding=utf-8"); // dsc.setSchemaName("public"); dsc.setDriverName("com.mysql.jdbc.Driver"); dsc.setUsername("sms"); dsc.setPassword("Yeion#187.top"); mpg.setDataSource(dsc); // 包配置 PackageConfig pc = new PackageConfig(); pc.setModuleName("member.business"); // pc.setModuleName(scanner("模块名")); pc.setParent("com.yeion.jd"); mpg.setPackageInfo(pc); // 自定义配置 InjectionConfig cfg = new InjectionConfig() { @Override public void initMap() { // to do nothing } }; // 如果模板引擎是 freemarker // String templatePath = "/templates/mapper.xml.ftl"; // 如果模板引擎是 velocity String templatePath = "/templates/mapper.xml.vm"; // 自定义输出配置 !!!!!!!!Here!! List<FileOutConfig> focList = new ArrayList<>(); // 自定义配置会被优先输出 focList.add(new FileOutConfig(templatePath) { @Override public String outputFile(TableInfo tableInfo) { // 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!! return projectPath + "/src/main/resources/mapper/" + pc.getModuleName() + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML; } }); cfg.setFileOutConfigList(focList); mpg.setCfg(cfg); // 配置模板 TemplateConfig templateConfig = new TemplateConfig(); // 配置自定义输出模板 //指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别 // templateConfig.setEntity("templates/entity2.java"); // templateConfig.setService(); // templateConfig.setController(); templateConfig.setXml(null); mpg.setTemplate(templateConfig); // 策略配置 StrategyConfig strategy = new StrategyConfig(); strategy.setNaming(NamingStrategy.underline_to_camel); strategy.setColumnNaming(NamingStrategy.underline_to_camel); strategy.setEntityTableFieldAnnotationEnable(true); strategy.setVersionFieldName("version"); //逻辑删除的字段 strategy.setLogicDeleteFieldName("deleted"); // strategy.setSuperEntityClass("com.baomidou.ant.common.BaseEntity"); strategy.setEntityLombokModel(true); strategy.setRestControllerStyle(true); strategy.setSuperControllerClass("com.yeion.jd.common.base.controller.BaseController"); // 自动填充配置 TableFill create = new TableFill("gmt_create", FieldFill.INSERT); TableFill update = new TableFill("gmt_update", FieldFill.INSERT_UPDATE); List<TableFill> list = Arrays.asList(create, update); strategy.setTableFillList(list); //少量表是表名从控制台读取 // strategy.setInclude(scanner("表名")); //多张表的时候直接在代码中写表名 strategy.setInclude("user_xf" ); strategy.setControllerMappingHyphenStyle(true); // !!!!!Here !!!! // strategy.setTablePrefix(scanner("表前缀") + "_"); mpg.setStrategy(strategy); mpg.setTemplateEngine(new VelocityTemplateEngine()); mpg.execute(); } }

AA

你可能感兴趣的:(框架)