java模板代码生成器(还不是有手就行)
作者:互联网
简介
代码生成感觉非常合适我们这种勤奋的人,废话不多说,直接上代码(有手就行);
知识点:mybatis-plus框架,生成器根据mybatis-plus模板生成建议可以看官网:https://baomidou.com/
官网的生成器地址:https://baomidou.com/guide/generator.html
#pox.xml
<properties>
<java.version>1.8</java.version>
<mysql.version>8.0.22</mysql.version>
<plus.version>3.4.2</plus.version>
<swagger.version>2.9.2</swagger.version>
<swaggerio.version>1.5.21</swaggerio.version>
<commons.io.version>2.5</commons.io.version>
</properties>
<dependencies>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.31</version>
</dependency>
<dependency>
<groupId>com.ibeetl</groupId>
<artifactId>beetl</artifactId>
<version>3.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.0</version>
</dependency>
<!--io常用工具类 -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons.io.version}</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!-- Mybatis-plus 依赖 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>${plus.version}</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${plus.version}</version>
</dependency>
</dependencies>
PublicConstants(常量)
/**
* @ClassName PublicConstants
* @Author zwy
* @Data 15/4/2021 上午11:01
*/
public class PublicConstants {
final public static String DRIVER_NAME = "com.mysql.cj.jdbc.Driver";
final public static String URL = "jdbc:mysql://121.5.147.69:3306/material_system?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai";
final public static String USER_NAME = "root";
final public static String PASSWORD = "密码";
final public static String CONTROLLER = "templates/controller.java.vm";
final public static String SERVICE = "templates/service.java.vm";
final public static String SERVICE_IMPL = "templates/serviceImpl.java.vm";
final public static String MAPPER = "templates/mapper.java.vm";
final public static String MAPPER_XML = "templates/mapper.xml.vm";
final public static String ENTRY = "templates/entity.java.vm";
}
Test(测试)
@Test
public void test3(){
// 全局配置 说明白点,就是一些文件生成路径和创建类上面的一些注释,例如作者等
GlobalConfig globalConfig = new GlobalConfig();
globalConfig.setActiveRecord(true)
.setFileOverride(true)
.setOpen(false)
//作者
.setAuthor("lanys")
//生成文件
.setOutputDir("D:/lanys")
//指定生成的主键的ID类型
.setIdType(IdType.AUTO)
//service层的名称
.setServiceName("%sService")
.setBaseResultMap(true)
.setBaseColumnList(true);
//2.配置数据源 数据源,url,username,password
DataSourceConfig dataSourceConfig = new DataSourceConfig();
dataSourceConfig.setDriverName(PublicConstants.DRIVER_NAME);
dataSourceConfig.setUrl(PublicConstants.URL);
dataSourceConfig.setUsername(PublicConstants.USER_NAME);
dataSourceConfig.setPassword(PublicConstants.PASSWORD);
//3.包的策略配置 //packageConfig.setModuleName(mysql.getTableName());
PackageConfig packageConfig = new PackageConfig();
//配置包名
packageConfig.setParent("sys_user");
//实体包名
packageConfig.setEntity("model");
//4.模板 如果没有什么set的,就会使用默认的mybatis-plus默认模板
TemplateConfig templateConfig = new TemplateConfig();
templateConfig.setXml(null);
//下边是是定义模板
// templateConfig.setController(PublicConstants.CONTROLLER);
// templateConfig.setXml(PublicConstants.MAPPER_XML);
// templateConfig.setService(PublicConstants.SERVICE);
// templateConfig.setServiceImpl(PublicConstants.SERVICE_IMPL);
// templateConfig.setMapper(PublicConstants.MAPPER);
// templateConfig.setEntity(PublicConstants.ENTRY);
//5.配置策略模式
StrategyConfig strategyConfig = new StrategyConfig();
//驼峰命名
strategyConfig.setCapitalMode(true)
//驼峰命名
.setNaming(NamingStrategy.underline_to_camel)
/**
* 指定数据库表,如果不指定,就会生成数据库中的全部表
*/
.setInclude("sys_user");
//最主要的生成器
AutoGenerator autoGenerator = new AutoGenerator();
autoGenerator.setGlobalConfig(globalConfig)
.setDataSource(dataSourceConfig)
.setStrategy(strategyConfig)
.setTemplate(templateConfig)
.setPackageInfo(packageConfig);
//自定义生成
//autoGenerator.setCfg(injectionConfig());
autoGenerator.execute();
}
解析:因为大部分的代码已经写好注解,可以自己去看,如果想自己用来测试需要注意的有:
1.常量文件中的对应的mysql配置:ip,端口,数据库名称,账号,密码
final public static String URL = "jdbc:mysql://121.5.147.69:3306/material_system?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai";
final public static String USER_NAME = "root";
final public static String PASSWORD = "密码"
2.生成对应表,或生成全部表,下边已注释:
//5.配置策略模式
StrategyConfig strategyConfig = new StrategyConfig();
//驼峰命名
strategyConfig.setCapitalMode(true)
//驼峰命名
.setNaming(NamingStrategy.underline_to_camel)
/**
* 指定数据库表,如果不指定,就会生成数据库中的全部表
*/
.setInclude("sys_user");
注意:这是模板生成,如果想要更进一步提升,可以自己写一个生成器,不用太原始,比如你想生成数据库的某个表,他是如何获取的等,到你真正的去探索就会发现很有意思!
例如:1.获取数据库中的某个表:
select table_name, table_comment, create_time, update_time from
information_schema.tableswhere table_name NOT LIKE 'qrtz_%'
and table_name NOT LIKE 'gen_%' and table_schema = (select database())
and table_name='sys_notice'
2.获取数据库中的所有表:
select table_name,table_comment,create_time,update_time from
information_schema.tables where table_schema = (select database())
3.获取数据表中的字段:
select column_name,column_type from information_schema.columns
where table_schema = (select database()) and table_name = 'sys_user'
order by ordinal_position
声明:生成器简单入门,充当笔记,造轮子。
标签:代码生成,java,String,final,有手,static,table,public,PublicConstants 来源: https://blog.csdn.net/qq_44697754/article/details/115773733