mybatis plus3.x 生成entity mapper service 文件

  1. pom文件


    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.2.6.RELEASE
         
    
    com.example
    demo
    0.0.1-SNAPSHOT
    demo
    Demo project for Spring Boot

    
        1.8
    

    
        
            com.baomidou
            mybatis-plus-boot-starter
            3.3.0
        
        
        
            com.baomidou
            mybatis-plus-generator
            3.3.0
        
        
        
            org.freemarker
            freemarker
            2.3.28
        

        
            mysql
            mysql-connector-java
            runtime
            8.0.19
        
    



  1. java文件
package com.example.demo;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
/**
 * @desc 生成mybatis plus相关的代码工具类 具体配置请参考
 * http://mp.baomidou.com/guide/generator.html#%E6%B7%BB%E5%8A%A0%E4%BE%9D%E8%B5%96
 */
public class CodeGenerator {
    //要生成的表名
    public static String[] genTablesName = new String[]{
            "m_enumeration", "t_counseling_book", "t_video_history","t_video_category","t_counseling_message","t_counseling_schedule",
            "t_therapist_schedule","t_user_video","t_sys_role_permission","t_video","t_therapist","t_sys_user_role","t_sys_permission",
            "t_sys_role","t_rehabilitation_user"
    };

    public static void runMybatisPlusGenerator() {
        AutoGenerator autoGenerator = new AutoGenerator();
        // 全局配置
        GlobalConfig config = new GlobalConfig();
        // 项目目录
        config.setOutputDir("D:\\demo\\src\\main\\java");
        config.setFileOverride(true);
        config.setActiveRecord(true);
        config.setEnableCache(false);// XML 二级缓存
        config.setBaseResultMap(true);// XML ResultMap
        config.setBaseColumnList(false);// XML columList
        config.setAuthor("wen.yu");
        config.setOpen(false);
        // 自定义文件命名,注意 %s 会自动填充表实体属性!
        config.setMapperName("%sMapper");
        config.setXmlName("%sMapper");
        config.setServiceName("%sService");
        config.setServiceImplName("%sServiceImpl");
        config.setControllerName("%sController");
        autoGenerator.setGlobalConfig(config);
        // 数据源配置
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDbType(DbType.MYSQL);
        dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");
        dataSourceConfig.setUsername("root");
        dataSourceConfig.setPassword("123456");
        dataSourceConfig.setUrl("jdbc:mysql://10.12.41.144:3306/home_training_assist?characterEncoding=utf8");
        autoGenerator.setDataSource(dataSourceConfig);
        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setTablePrefix("t_");// 此处可以修改为您的表前缀
        strategy.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略
        strategy.setInclude(genTablesName); // 需要生成的表
        // strategy.setExclude(new String[]{"test"}); // 排除生成的表
        // 字段名生成策略
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        // 自定义实体父类
         strategy.setSuperEntityClass("BaseEntity");
//        // 自定义实体,公共字段
         strategy.setSuperEntityColumns(new String[] { "id", "createTime", "updateTime"});
        // 自定义 mapper 父类
        // strategy.setSuperMapperClass("com.maxbill.base.SuperMapper");
        // 自定义 service 父类
        // strategy.setSuperServiceClass("com.maxbill.base.SuperService");
        // 自定义 service 实现类父类
        // strategy.setSuperServiceImplClass("com.maxbill.base.SuperServiceImpl");
        // 自定义 controller 父类
        // strategy.setSuperControllerClass("com.maxbill.base.SuperController");
        // 【实体】是否生成字段常量(默认 false)
        // public static final String ID = "test_id";
        strategy.setEntityColumnConstant(false);
        // 【实体】是否为构建者模型(默认 false)
        // public User setName(String name) {this.name = name; return this;}
        strategy.setEntityBuilderModel(true);
        autoGenerator.setStrategy(strategy);
        // 包配置
        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setParent("com.training");
        packageConfig.setController("controller");
        packageConfig.setEntity("entity");
        packageConfig.setService("service");
        packageConfig.setServiceImpl("service.impl");
        packageConfig.setMapper("mapper");
        packageConfig.setXml("mapper");
        // packageConfig.setModuleName("base");
        autoGenerator.setPackageInfo(packageConfig);
        // 注入自定义配置,可以在 VM 中使用 cfg.abc 设置的值
		/*InjectionConfig injectionConfig = new InjectionConfig() {
			@Override
			public void initMap() {
				Map map = new HashMap();
				map.put("maxbill", this.getConfig().getGlobalConfig().getAuthor() + "-MybatisPlus");
				this.setMap(map);
			}
		};
		autoGenerator.setCfg(injectionConfig);*/
        // 执行生成
        autoGenerator.setTemplateEngine(new FreemarkerTemplateEngine());
        autoGenerator.execute();
        // 打印注入设置
        //System.err.println(autoGenerator.getCfg().getMap().get("maxbill"));
    }

    public static void main(String[] args) {
        runMybatisPlusGenerator();
        //System.out.println(LocalDateTime.now());
    }

}

你可能感兴趣的:(java学习)