Mybatis-Plus生成代码(超全注释配置)

背景

以前用的Tkmybatis,新入职的公司用的是Mybatis-Plus,琢磨了下Mybatis-Plus生成代码,感觉挺爽的(默认整合swagger,配合swagger妈妈再也不用担心项目一行注释都没有了),再此记录下生成代码的配置。

简介

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

配置

/**
 * @Author will
 * @Date 2019/10/30 0030 11:51
 **/
public class MybatisPlus {
    /**
     * 

* 读取控制台内容 *

*/ 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(); //MybatisPlus提供的工具类,做非空判断 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 = System.getProperty("user.dir"); // 指定文件生成路径 gc.setOutputDir(projectPath + "/src/main/java"); // 作者 gc.setAuthor("will"); gc.setOpen(false); //实体属性 Swagger2 注解 gc.setSwagger2(true); mpg.setGlobalConfig(gc); // 数据源配置 DataSourceConfig dsc = new DataSourceConfig(); dsc.setUrl("jdbc:mysql://127.0.0.1:3306/scm_XXX?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8"); // dsc.setSchemaName("public"); dsc.setDriverName("com.mysql.cj.jdbc.Driver"); dsc.setUsername("root"); dsc.setPassword("123"); mpg.setDataSource(dsc); // 包配置 PackageConfig pc = new PackageConfig(); pc.setModuleName(scanner("模块名")); // 父级包名 pc.setParent("com.cyberoller.cloud.base"); 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"; // 自定义输出配置 List 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.setFileCreate(new IFileCreate() { @Override public boolean isCreate(ConfigBuilder configBuilder, FileType fileType, String filePath) { // 判断自定义文件夹是否需要创建 checkDir("调用默认方法创建的目录"); return false; } }); */ 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(); //表名生成策略 underline_to_camel转驼峰命名,no_change默认的没变化 strategy.setNaming(NamingStrategy.underline_to_camel); //列名生成策略 underline_to_camel转驼峰命名,no_change默认的没变化 strategy.setColumnNaming(NamingStrategy.underline_to_camel); //strategy.setSuperEntityClass("com.baomidou.ant.common.BaseEntity"); // 是否使用Lombok优化代码 strategy.setEntityLombokModel(true); // controller类是否直接返回json strategy.setRestControllerStyle(true); // 公共父类 //strategy.setSuperControllerClass("com.baomidou.ant.common.BaseController"); // 写于父类中的公共字段 //strategy.setSuperEntityColumns("id"); //要生成的表名(控制台输入) strategy.setInclude(scanner("表名,多个英文逗号分割").split(",")); strategy.setControllerMappingHyphenStyle(true); //生成的类名去掉前缀 如yw_sys_user ---> SysUser strategy.setTablePrefix("t_"); mpg.setStrategy(strategy); mpg.setTemplateEngine(new FreemarkerTemplateEngine()); mpg.execute(); } }

附上一张生成实体的图

Mybatis-Plus生成代码(超全注释配置)_第1张图片

 

想更详细的了解Mybatis-Plus请移步上面的简介链接

你可能感兴趣的:(java)