创建代码生成器

1、创建parent父工程,在父工程的pom.xml文件中引入代码生成器的依赖

 <properties>
        <velocity.version>2.0</velocity.version>
   </properties>
<!-- velocity 模板引擎, Mybatis Plus 代码生成器需要 -->
   <dependencies>
            <dependency>
                <groupId>org.apache.velocity</groupId>
                <artifactId>velocity-engine-core</artifactId>
                <version>${velocity.version}</version>
            </dependency>
     </dependencies>

2、在parent父工程下创建service子模块,在service的pom.xml文件中引入代码生成器的依赖

<!-- velocity 模板引擎, Mybatis Plus 代码生成器需要 -->
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-engine-core</artifactId>
        </dependency>

3、在service子模块下创建service_edu模块
在这里插入图片描述
在CodeGenerator 中编写生成器的代码,右键选择Run CodeGenerator 运行代码即创建成功

public class CodeGenerator {

	@Test
	public void run() {
	
	    // 1、创建代码生成器
	    AutoGenerator mpg = new AutoGenerator();
	
	    // 2、全局配置
	    GlobalConfig gc = new GlobalConfig();
	    String projectPath = System.getProperty("user.dir");
	    gc.setOutputDir("D:\\学习资料\\项目\\parent\\service\\service_edu" + "/src/main/java");//项目存放路径名
	
	    gc.setAuthor("testjava");
	    gc.setOpen(false); //生成后是否打开资源管理器
	    gc.setFileOverride(false); //重新生成时文件是否覆盖
	    gc.setServiceName("%sService");	//去掉Service接口的首字母I
	
	
	    //ID_WORKER:实体类中的id类型为long类型用ID_WORKER    字符串类型(string)用ID_WORKER_STR
	    gc.setIdType(IdType.ID_WORKER_STR); //主键策略
	    gc.setDateType(DateType.ONLY_DATE);//定义生成的实体类中日期类型
	    gc.setSwagger2(true);//开启Swagger2模式
	
	    mpg.setGlobalConfig(gc);
	
	    // 3、数据源配置
	    DataSourceConfig dsc = new DataSourceConfig();
	    dsc.setUrl("jdbc:mysql://localhost:3306/guli?serverTimezone=GMT%2B8");//连接数据库
	    dsc.setDriverName("com.mysql.cj.jdbc.Driver");
	    dsc.setUsername("root");//数据库用户名
	    dsc.setPassword("root");//数据库密码
	    dsc.setDbType(DbType.MYSQL);
	    mpg.setDataSource(dsc);
	
	    // 4、包配置
	    PackageConfig pc = new PackageConfig();
	    pc.setParent("com.cn");
	    pc.setModuleName("eduservice"); //模块名
	    pc.setController("controller");
	    pc.setEntity("entity");
	    pc.setService("service");
	    pc.setMapper("mapper");
	    mpg.setPackageInfo(pc);
	
	    // 5、策略配置
	    StrategyConfig strategy = new StrategyConfig();
	
	    strategy.setInclude("edu_comment");//数据库表名
	    strategy.setNaming(NamingStrategy.underline_to_camel);//数据库表映射到实体的命名策略
	    strategy.setTablePrefix(pc.getModuleName() + "_"); //生成实体时去掉表前缀
	
	    strategy.setColumnNaming(NamingStrategy.underline_to_camel);//数据库表字段映射到实体的命名策略
	    strategy.setEntityLombokModel(true); // lombok 模型 @Accessors(chain = true) setter链式操作
	
	    strategy.setRestControllerStyle(true); //restful api风格控制器
	    strategy.setControllerMappingHyphenStyle(true); //url中驼峰转连字符
	
	    mpg.setStrategy(strategy);
	    // 6、执行
	    mpg.execute();
	    }
	}

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值