rapid-generator自動生成代碼步驟
最近找了個開源的代碼生成器rapid-generator,感覺挺好使的,所以這裡就分享下操作步驟。
1使用eclipse或idea創建一個maven工程
打開今日頭條,查看更多精彩圖片2配置依賴pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com</groupId>
<artifactId>myGenerator</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>myGenerator</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.googlecode.rapid-framework/rapid-generator -->
<dependency>
<groupId>com.googlecode.rapid-framework</groupId>
<artifactId>rapid-generator</artifactId>
<version>4.0.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.41</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.googlecode.rapid-framework/rapid-generator-template -->
<dependency>
<groupId>com.googlecode.rapid-framework</groupId>
<artifactId>rapid-generator-template</artifactId>
<version>4.0.6</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
3在源碼路徑下新建generator.xml文件,並做如下配置
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>
代碼生成器配置文件:
1.會為所有的property生成property_dir屬性,如pkg=com.company => pkg_dir=com/company
2.可以引用環境變數: ${env.JAVA_HOME} or System.properties: ${user.home},property之間也可以相互引用
</comment>
<entry key="basepackage">com.minarea</entry>
<!-- jsp namespace: web/${namespace}/${className}/list.jsp -->
<entry key="namespace">pages</entry>
<entry key="outRoot">generator-output</entry>
<!-- 需要移除的表名前綴,使用逗號進行分隔多個前綴,示例值: t_,v_ -->
<entry key="tableRemovePrefixes"></entry>
<entry key="jdbc_username">username</entry>
<entry key="jdbc_password">password</entry>
<!-- oracle需要指定jdbc.schema,其它資料庫忽略此項配置 -->
<entry key="jdbc_schema"></entry>
<entry key="jdbc_catalog"></entry>
<!-- 資料庫類型映射 -->
<entry key="java_typemapping.java.sql.Timestamp">java.util.Date</entry>
<entry key="java_typemapping.java.sql.Date">java.util.Date</entry>
<entry key="java_typemapping.java.sql.Time">java.util.Date</entry>
<entry key="java_typemapping.java.lang.Byte">Integer</entry>
<entry key="java_typemapping.java.lang.Short">Integer</entry>
<entry key="java_typemapping.java.math.BigDecimal">Long</entry>
<!-- H2 -->
<!-- <entry key="jdbc.url">jdbc:h2:tcp://localhost/test</entry>
<entry key="jdbc.driver">org.h2.Driver</entry>
-->
<entry key="jdbc_url">jdbc:mysql://**.**.***.**:3306/rms</entry>
<entry key="jdbc_driver">com.mysql.jdbc.Driver</entry>
<!-- Oracle jdbc:oracle:oci:@tnsname_devdb
<entry key="jdbc.url">jdbc:oracle:thin:@192.168.0.177:1521:[sid]</entry>
<entry key="jdbc.driver">oracle.jdbc.driver.OracleDriver</entry>
-->
<!-- SQLServer2000
<entry key="jdbc.url">jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=[database]</entry>
<entry key="jdbc.driver">com.microsoft.jdbc.sqlserver.SQLServerDriver</entry>
-->
<!-- SQLServer2005
<entry key="jdbc.url">jdbc:sqlserver://192.168.0.98:1433;DatabaseName=[database]</entry>
<entry key="jdbc.driver">com.microsoft.sqlserver.jdbc.SQLServerDriver</entry>
-->
<!-- JTDs for SQLServer
<entry key="jdbc.url">jdbc:jtds:sqlserver://192.168.0.102:1433/[database];tds=8.0;lastupdatecount=true</entry>
<entry key="jdbc.driver">net.sourceforge.jtds.jdbc.Driver</entry>
-->
<!-- PostgreSql
<entry key="jdbc.url">jdbc:postgresql://localhost/[database]</entry>
<entry key="jdbc.driver">org.postgresql.Driver</entry>
-->
<!-- Sybase
<entry key="jdbc.url">jdbc:sybase:Tds:localhost:5007/[database]</entry>
<entry key="jdbc.driver">com.sybase.jdbc.SybDriver</entry>
-->
<!-- DB2
<entry key="jdbc.url">jdbc:db2://localhost:5000/[database]</entry>
<entry key="jdbc.driver">com.ibm.db2.jdbc.app.DB2Driver</entry>
-->
<!-- HsqlDB
<entry key="jdbc.url">jdbc:hsqldb:mem:generatorDB</entry>
<entry key="jdbc.driver">org.hsqldb.jdbcDriver</entry>
-->
<!-- Derby
<entry key="jdbc.url">jdbc:derby://localhost/databaseName</entry>
<entry key="jdbc.driver">org.apache.derby.jdbc.ClientDriver</entry>
-->
</properties>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
4在源碼路徑下新建template目錄,並將rapid-generator-template-4.0.6generator emplate
apidshareasic路徑下的公共文件拷貝到該目錄下
5在template編寫待生成文件的模版文件${className}.java.ftl
<#include "/java_copyright.include">
<#assign className = table.className>
<#assign classNameLower = className?uncap_first>
package ${basepackage}.${table.classNameFirstLower};
<#include "/java_imports.include">
import com.linkage.agri.pojo.base.BaseEntity;
public class ${className} extends BaseEntity {
private static final long serialVersionUID = 5454155825314635342L;
<#list table.columns as column>
/**
* ${column.remarks}
*/
private ${column.simpleJavaType} ${column.columnNameLower};
</#list>
<@generateJavaColumns/>
<#macro generateJavaColumns>
<#list table.columns as column>
<#if column.isDateTimeColumn>
public String get${column.columnName}String() {
return DateConvertUtils.format(get${column.columnName}(), FORMAT_${column.constantName});
}
public void set${column.columnName}String(String ${column.columnNameLower}) {
set${column.columnName}(DateConvertUtils.parse(${column.columnNameLower}, FORMAT_${column.constantName},${column.simpleJavaType}.class));
}
</#if>
public void set${column.columnName}(${column.simpleJavaType} ${column.columnNameLower}) {
this.${column.columnNameLower} = ${column.columnNameLower};
}
public ${column.simpleJavaType} get${column.columnName}() {
return this.${column.columnNameLower};
}
</#list>
</#macro>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
6在main方法中敲幾行代碼執行生成文件
package com.myGenerator;
import cn.org.rapid_framework.generator.GeneratorFacade;
/**
* Hello world!
*
*/
public class App {
public static void main(String[] args) {
// 模板地址
try {
String templatePath = "C:\Users\yuxl\myproject\myGenerator\src\main\java\template";
GeneratorFacade g = new GeneratorFacade();
g.getGenerator().addTemplateRootDir(templatePath);
// 刪除生成器的輸出目錄//
// g.deleteOutRootDir();
// 通過資料庫表生成文件
g.generateByTable("user_info");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
7生成日誌及結果結果
"C:Program FilesJavajdk1.7.0_25jreinjava" "-javaagent:C:Program FilesJetBrainsIntelliJ IDEA 2017.1.5libidea_rt.jar=56073:C:Program FilesJetBrainsIntelliJ IDEA 2017.1.5in" -Dfile.encoding=GBK -classpath "C:Program FilesJavajdk1.7.0_25jrelibcharsets.jar;C:Program FilesJavajdk1.7.0_25jrelibdeploy.jar;C:Program FilesJavajdk1.7.0_25jrelibextaccess-bridge-32.jar;C:Program FilesJavajdk1.7.0_25jrelibextdnsns.jar;C:Program FilesJavajdk1.7.0_25jrelibextjaccess.jar;C:Program FilesJavajdk1.7.0_25jrelibextlocaledata.jar;C:Program FilesJavajdk1.7.0_25jrelibextsunec.jar;C:Program FilesJavajdk1.7.0_25jrelibextsunjce_provider.jar;C:Program FilesJavajdk1.7.0_25jrelibextsunmscapi.jar;C:Program FilesJavajdk1.7.0_25jrelibextsunpkcs11.jar;C:Program FilesJavajdk1.7.0_25jrelibextzipfs.jar;C:Program FilesJavajdk1.7.0_25jrelibjavaws.jar;C:Program FilesJavajdk1.7.0_25jrelibjce.jar;C:Program FilesJavajdk1.7.0_25jrelibjfr.jar;C:Program FilesJavajdk1.7.0_25jrelibjfxrt.jar;C:Program FilesJavajdk1.7.0_25jrelibjsse.jar;C:Program FilesJavajdk1.7.0_25jrelibmanagement-agent.jar;C:Program FilesJavajdk1.7.0_25jrelibplugin.jar;C:Program FilesJavajdk1.7.0_25jrelib
esources.jar;C:Program FilesJavajdk1.7.0_25jrelib
t.jar;C:UsersyuxlmyprojectmyGenerator argetclasses;C:Usersyuxl.m2
epositorycomgooglecode
apid-framework
apid-generator4.0.6
apid-generator-4.0.6.jar;C:Usersyuxl.m2
epositoryorgfreemarkerfreemarker2.3.16freemarker-2.3.16.jar;C:Usersyuxl.m2
epositorycomh2databaseh21.2.135h2-1.2.135.jar;C:Usersyuxl.m2
epositorymysqlmysql-connector-java5.1.41mysql-connector-java-5.1.41.jar" com.myGenerator.App
Start Load GeneratorPropeties from classpath:[generator.properties, generator.xml]
[Generator WARN] not found log4j.properties, cause:java.io.FileNotFoundException: classpath:log4j.properties
GeneratorPropeties Load Success,files:[/C:/Users/yuxl/myproject/myGenerator/target/classes/generator.xml]
Fri Nov 02 13:54:10 CST 2018 WARN: Establishing SSL connection without server"s identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn"t set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to "false". You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
***************************************************************
* BEGIN generate by user_info => UserInfo
***************************************************************
-------------------load template from templateRootDir = "C:UsersyuxlmyprojectmyGeneratorsrcmainjava emplate" outRootDir:C:UsersyuxlmyprojectmyGeneratorgenerator-output
[not generate] by gg.isOverride()=false and outputFile exist:C:UsersyuxlmyprojectmyGeneratorgenerator-outputUserInfo.java
[skip] endsWith ".include" template:custom.include
[skip] endsWith ".include" template:java_copyright.include
[skip] endsWith ".include" template:java_description.include
[skip] endsWith ".include" template:java_imports.include
[skip] endsWith ".include" template:macro.include
Process finished with exit code 0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
生成的java代碼UserInfo.java
/*
* Powered By [rapid-framework]
* Web Site: http://www.rapid-framework.org.cn
* Google Code: http://code.google.com/p/rapid-framework/
* Since 2008 - 2018
*/
package com.minarea.userInfo;
import java.io.*;
import java.net.*;
import java.util.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import javacommon.base.*;
import javacommon.util.*;
import cn.org.rapid_framework.util.*;
import cn.org.rapid_framework.web.util.*;
import cn.org.rapid_framework.page.*;
import cn.org.rapid_framework.page.impl.*;
import com.minarea.model.*;
import com.minarea.dao.*;
import com.minarea.service.*;
import com.minarea.query.*;
import com.linkage.agri.pojo.base.BaseEntity;
public class UserInfo extends BaseEntity {
private static final long serialVersionUID = 5454155825314635342L;
/**
*
*/
private Integer id;
/**
*
*/
private String userName;
/**
*
*/
private String passWord;
/**
*
*/
private String openId;
/**
*
*/
private String province;
/**
*
*/
private String city;
/**
*
*/
private String tel;
/**
*
*/
private String address;
/**
*
*/
private Integer gender;
/**
*
*/
private Date createTime;
/**
*
*/
private String sessionId;
/**
*
*/
private Date updateTime;
/**
*
*/
private String nickName;
/**
*
*/
private String mail;
/**
*
*/
private String country;
public void setId(Integer id) {
this.id = id;
}
public Integer getId() {
return this.id;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserName() {
return this.userName;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
public String getPassWord() {
return this.passWord;
}
public void setOpenId(String openId) {
this.openId = openId;
}
public String getOpenId() {
return this.openId;
}
public void setProvince(String province) {
this.province = province;
}
public String getProvince() {
return this.province;
}
public void setCity(String city) {
this.city = city;
}
public String getCity() {
return this.city;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getTel() {
return this.tel;
}
public void setAddress(String address) {
this.address = address;
}
public String getAddress() {
return this.address;
}
public void setGender(Integer gender) {
this.gender = gender;
}
public Integer getGender() {
return this.gender;
}
public String getCreateTimeString() {
return DateConvertUtils.format(getCreateTime(), FORMAT_CREATE_TIME);
}
public void setCreateTimeString(String createTime) {
setCreateTime(DateConvertUtils.parse(createTime, FORMAT_CREATE_TIME,Date.class));
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getCreateTime() {
return this.createTime;
}
public void setSessionId(String sessionId) {
this.sessionId = sessionId;
}
public String getSessionId() {
return this.sessionId;
}
public String getUpdateTimeString() {
return DateConvertUtils.format(getUpdateTime(), FORMAT_UPDATE_TIME);
}
public void setUpdateTimeString(String updateTime) {
setUpdateTime(DateConvertUtils.parse(updateTime, FORMAT_UPDATE_TIME,Date.class));
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
public Date getUpdateTime() {
return this.updateTime;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
public String getNickName() {
return this.nickName;
}
public void setMail(String mail) {
this.mail = mail;
}
public String getMail() {
return this.mail;
}
public void setCountry(String country) {
this.country = country;
}
public String getCountry() {
return this.country;
}
※淺談SDN架構下的運維
※push to origin/master was rejected錯誤解決方案
TAG:程序員小新人學習 |