代码生成器
步骤:
1.创建一个类,编写main方法,在其中编写代码,copy以下代码,做一些简易的更改即可(注释后加了***即表示重点要改的)
package com.yyjc.springboot.springboot_ch005;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
/**
* @author liucheng
* @version 1.0
* @date 2021-04-14 10:37
*/
public class MybatisGenertor {
public static void main(String[] args) {
//实例化代码生成器的实例
AutoGenerator autoGenerator = new AutoGenerator();
//配置数据源
DataSourceConfig dataSourceConfig = new DataSourceConfig();
//指定数据库的类型 ***
dataSourceConfig.setDbType(DbType.MYSQL);
//***
dataSourceConfig.setUrl("jdbc:mysql://localhost:3306/test_exercise?useUnicode=true&characterEncoding=UTF-8");
dataSourceConfig.setUsername("root");
dataSourceConfig.setPassword("root");
dataSourceConfig.setDriverName("com.mysql.jdbc.Driver");
autoGenerator.setDataSource(dataSourceConfig);
//配置 GlobalConfig
GlobalConfig globalConfig = new GlobalConfig();
//生成的代码存放路径 ***
globalConfig.setOutputDir(System.getProperty("user.dir") + "/springboot_ch005/src/main/java");
globalConfig.setAuthor("liucheng");
globalConfig.setOpen(false);
//接口的命名规则:IOrder.. or OrderServiceInter
//%sService:service接口名在其前不加I作为开始
globalConfig.setServiceName("%sService");
autoGenerator.setGlobalConfig(globalConfig);
//配置包的信息 ***
PackageConfig packageConfig = new PackageConfig();
//如果以下父包存在,则将ModuleName("generator")直接添加进去,不存在则会创建
packageConfig.setParent("com.yyjc.springboot");
packageConfig.setModuleName("generator");
packageConfig.setController("handler");
packageConfig.setService("service");
packageConfig.setServiceImpl("service.impl");
packageConfig.setMapper("mapper");
packageConfig.setEntity("entity");
autoGenerator.setPackageInfo(packageConfig);
//配置策略
StrategyConfig strategyConfig = new StrategyConfig();
//启用lombok的模式(应用@Data)
strategyConfig.setEntityLombokModel(true);
//pojo属性生成规则(table feild: xxx_xxx ==> pojo property: xxxXxx)
strategyConfig.setNaming(NamingStrategy.underline_to_camel);
strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
autoGenerator.setStrategy(strategyConfig);
//调用执行
autoGenerator.execute();
}
}