使用mybatis plus代码生成器生成代码(自定义不同类型文件的生成路径)

mybatis plus在mybatis基础上进行了封装和加强,旨在提供方便的面向持久层的操作,类似Spring JPA。
配置方法和详细介绍见官网
mybatis plus提供代码生成器可以方便开发者生成xml、dao、service、controller、entity层代码,简化常规crud的代码和实体类映射编码。下面提供配置模板示例,供参考,其中包含自定义指定不同类型文件在项目中的路径。

配置mybatis plus代码生成器依赖

<dependency>
			<groupId>com.baomidou</groupId>
			<artifactId>mybatis-plus-generator</artifactId>
			<version>3.2.0</version>
		</dependency>
		<dependency>
			<groupId>org.apache.velocity</groupId>
			<artifactId>velocity-engine-core</artifactId> <!-- 使用velocity模板引擎(默认) -->
			<version>2.0</version>
		</dependency>

mysql数据源配置示例

public class MybatisAutoGenerator {

    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("南熏门前一只喵");
        gc.setOpen(false);
        gc.setFileOverride(true);//每次运行进行覆盖操作
        gc.setMapperName("%sDao");//java mapper类命名后缀
        gc.setDateType(DateType.ONLY_DATE);//mysql timestamp由java.util.Date类型映射

        // gc.setSwagger2(true); 实体属性 Swagger2 注解
        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://ip:port/databaseName?useUnicode=true&useSSL=false&characterEncoding=utf8");
        // dsc.setSchemaName("public");
        dsc.setDriverName("com.mysql.jdbc.Driver");
        dsc.setUsername("用户名");
        dsc.setPassword("密码");
        dsc.setSchemaName("数据库名");
        dsc.setDbType(DbType.MYSQL);
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        //设置文件的包名
        pc.setModuleName("manage");
        pc.setParent("com.czx.igateway");
        pc.setEntity("entity.mysql.plus");
        pc.setMapper("dao.mysql.plus");
        //设置不同类文件生成的路径
        HashMap<String, String> pathMap = new HashMap<>();
        pathMap.put(ConstVal.ENTITY,projectPath+"/src/main/java/com/czx/igateway/manage/entity/mysql/plus");
        pathMap.put(ConstVal.MAPPER,projectPath+"/src/main/java/com/czx/igateway/manage/dao/mysql/plus");
        pc.setPathInfo(pathMap);
        mpg.setPackageInfo(pc);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setInclude("deterioration_default_config,deterioration_config,deterioration_config_group,deterioration_group_config".split(","));//需要生成的表
        strategy.setTablePrefix("t_");//生成的文件去除表前缀

        // 配置模板
        TemplateConfig templateConfig = new TemplateConfig();
        //不生成如下类型模板
        templateConfig.setController(null);
        templateConfig.setService(null);
        templateConfig.setServiceImpl(null);
        templateConfig.setMapper(null);

        mpg.setTemplate(templateConfig);

         如果模板引擎是 freemarker
        //String templatePath = "/templates/mapper.xml.ftl";
        //如果模板引擎是 velocity
        String mapperXmlPath = "/templates/mapper.xml.vm";
        String mapperJavaPath = "/templates/mapper.java.vm";
        String entityPath= "/templates/entity.java.vm";


        // 自定义配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                // to do nothing
            }
        };

        // 自定义输出配置
        List<FileOutConfig> focList = new ArrayList<>();
        // 自定义配置优先于默认配置生效
        focList.add(new FileOutConfig(entityPath) {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义Entity文件名跟生成路径
                return projectPath + "/src/main/java/com/cmhi/igateway/manage/entity/mysql/plus/" + tableInfo.getEntityName() + StringPool.DOT_JAVA;
            }
        });

        focList.add(new FileOutConfig(mapperXmlPath) {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义xml 文件名和生成路径
                return projectPath + "/src/main/resources/mapping/plus/" + tableInfo.getEntityName() + StringPool.DOT_XML;
            }
        });

        focList.add(new FileOutConfig(mapperJavaPath) {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义Dao类文件名和生成路径
                return projectPath+"/src/main/java/com/cmhi/igateway/manage/dao/mysql/plus/"+tableInfo.getEntityName()+"Dao"+StringPool.DOT_JAVA;
            }
        });

        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);

        mpg.setStrategy(strategy);

        mpg.execute();
    }

}
  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值