根据数据库表自动生成基本代码
1.导入相关依赖
<!--mp 代码生成器-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.4.0</version>
</dependency>
<!--模板引擎选择一种即可,默认使用velocity模板引擎-->
<!--velocity模板引擎-->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.3</version>
</dependency>
<!--freemarker模板引擎-->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.30</version>
</dependency>
2.创建一个类,执行其中的main方法,即可生成对应的代码
public class MybatisPlusCodeGenerator {
// ================= 自定义配置 =================
//数据库类型
private static final DbType DB_TYPE = DbType.MYSQL;
//数据库连接地址
private static final String JDBC_URL = "jdbc:mysql://localhost:3306/user?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8";
//数据库驱动
private static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
//数据库用户名
private static final String JDBC_USERNAME = "root";
//数据库密码
private static final String JDBC_PASSWORD = "root";
/**
* 生成文件根目录及包名
*/
// private static final String ROOT_DIR =System.getProperty("user.dir")+ "/src/main/java";
// System.getProperty("user.dir") 当前的工程路径
private static final String ROOT_DIR = System.getProperty("user.dir")+"/cc_service/order-service/src/main/java";
//java文件夹下路径
//生成的父包名
private static final String PACKAGE_PARENT_NAME = "com.cc.mybatisplusCodegenerator";
private static final String PACKAGE_CONTROLLER_NAME = "controller";
private static final String PACKAGE_SERVICE_NAME = "service";
private static final String PACKAGE_SERVICEIMPL_NAME = "service.impl";
// entity
private static final String PACKAGE_ENTITY_NAME = "domain";
private static final String PACKAGE_MAPPER_NAME = "mapper";
// 换成自己的中文路径
private static final String RESOURCES_DIR = System.getProperty("user.dir")+"/cc_service/order-service/src/main/java";
private static final String MAPPER_XML_PATH = PACKAGE_PARENT_NAME.replace(".", "/") + "/mapper/xml";
/**
* 生成代码的@author
*/
private static final String AUTHOR = "爪哇cc";
/**
* 需要生成代码的数据库表名
*/
private static final String[] TABLES = new String[]{"ap_user"};
/**
* 数据库表名前缀
*/
private static final String[] TABLE_PREFIXS = new String[]{"ap_"};
public static void main(String[] args) {
AutoGenerator mpg = new AutoGenerator();
mpg.setDataSource(getDataSourceConfig());
mpg.setGlobalConfig(getGlobalConfig());
mpg.setStrategy(getStrategyConfig());
mpg.setPackageInfo(getPackageConfig());
mpg.setCfg(getInjectionConfig());
mpg.setTemplate(getTemplateConfig());
//设置模板引擎
//mpg.setTemplateEngine(new FreemarkerTemplateEngine());
// 执行生成
mpg.execute();
}
/**
* 数据源配置
*
* @return
*/
private static DataSourceConfig getDataSourceConfig() {
DataSourceConfig dsc = new DataSourceConfig();
dsc.setDbType(DB_TYPE);
dsc.setDriverName(JDBC_DRIVER);
dsc.setUrl(JDBC_URL);
dsc.setUsername(JDBC_USERNAME);
dsc.setPassword(JDBC_PASSWORD);
return dsc;
}
/**
* 全局配置
*
* @return
*/
private static GlobalConfig getGlobalConfig() {
GlobalConfig gc = new GlobalConfig();
//生成后是否打开资源管理器
gc.setOpen(false);
gc.setOutputDir(ROOT_DIR);
//重新生成时文件是否覆盖
gc.setFileOverride(true);
gc.setActiveRecord(true);
//主键策略
gc.setIdType(IdType.AUTO);
//定义生成的实体类中日期类型
gc.setDateType(DateType.ONLY_DATE);
//开启Swagger2模式
gc.setSwagger2(false);
// XML 二级缓存
gc.setEnableCache(false);
// XML ResultMap
gc.setBaseResultMap(true);
// XML columList
gc.setBaseColumnList(true);
gc.setAuthor(AUTHOR);
gc.setMapperName("%sMapper");
gc.setXmlName("%sMapper");
gc.setServiceName("%sService");
gc.setServiceImplName("%sServiceImpl");
gc.setControllerName("%sController");
return gc;
}
/**
* 生成策略配置
*
* @return
*/
private static StrategyConfig getStrategyConfig() {
StrategyConfig strategy = new StrategyConfig();
// 此处可以修改为您的表前缀
strategy.setTablePrefix(TABLE_PREFIXS);
// 表名生成策略
strategy.setNaming(NamingStrategy.underline_to_camel);
// 需要生成的表
strategy.setInclude(TABLES);
// lombok 模型 @Accessors(chain = true) setter链式操作
strategy.setEntityLombokModel(true);
//restful api风格控制器
strategy.setRestControllerStyle(true);
//url中驼峰转连字符
strategy.setControllerMappingHyphenStyle(true);
//生成TableFiled注解
strategy.setEntityTableFieldAnnotationEnable(true);
return strategy;
}
/**
* 生成包名设置
*
* @return
*/
private static PackageConfig getPackageConfig() {
// 4.生成文件所在包配置:
PackageConfig pc = new PackageConfig();
pc.setParent(PACKAGE_PARENT_NAME);
pc.setController(PACKAGE_CONTROLLER_NAME);
pc.setService(PACKAGE_SERVICE_NAME);
pc.setServiceImpl(PACKAGE_SERVICEIMPL_NAME);
pc.setEntity(PACKAGE_ENTITY_NAME);
pc.setMapper(PACKAGE_MAPPER_NAME);
return pc;
}
/**
* xml文件配置
*
* @return
*/
private static InjectionConfig getInjectionConfig() {
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-rb");
this.setMap(map);
}
};
//xml生成路径
List<FileOutConfig> focList = new ArrayList<>();
// 如果模板引擎是 freemarker
// String templatePath = "/templates/mapper.xml.ftl";
// 如果模板引擎是 velocity
// String templatePath = "/templates/mapper.xml.vm";
focList.add(new FileOutConfig("/templates/mapper.xml.vm") {
@Override
public String outputFile(TableInfo tableInfo) {
return RESOURCES_DIR + "/" + MAPPER_XML_PATH + "/" + tableInfo.getEntityName() + "Mapper.xml";
}
});
cfg.setFileOutConfigList(focList);
return cfg;
}
/**
* 关闭默认 xml 生成
*
* @return
*/
private static TemplateConfig getTemplateConfig() {
TemplateConfig tc = new TemplateConfig();
tc.setXml(null);
return tc;
}
}