wiki.xsx.jg.core.FileGenerator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of JG-All Show documentation
Show all versions of JG-All Show documentation
a generator, from the database tables convert to the Java classes or from the Java classes convert to the database tables.
support mysql, oracle and sqlserver.
package wiki.xsx.jg.core;
import wiki.xsx.jg.log.Logger;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Set;
/**
* Java文件生成器
*/
public class FileGenerator {
/**
* 生成Java文件
* @param tableNames 表名,多个逗号分隔
* @param driverClassName 数据库驱动名称
* @param dbUrl 数据库地址
* @param userName 数据库用户名
* @param password 数据库密码
* @param rootPath 包的根路径
* @param packageName 完整包名,如com.xsx.test
* @throws Exception 异常信息
*/
public static void run(
String tableNames,
String driverClassName,
String dbUrl,
String userName,
String password,
String rootPath,
String packageName
) throws Exception{
DatabaseService service = DatabaseAdapter.getServiceInstance(driverClassName, dbUrl, userName, password, tableNames, true);
service.setTableInfo();
for (Table table : service.getDatabase().getTableSet()){
write(rootPath, packageName, table);
Logger.info("Create file: " + packageName + "." + titleCase(table.getName()) + ".java");
}
}
/**
* 生成Java文件
* @param rootPath 包的根路径
* @param packageName 包名
* @param table 数据表对象
* @return 返回完整类字符串
* @throws IOException 异常信息
*/
private static void write (
String rootPath,
String packageName,
Table table
) throws IOException{
StringBuilder path = new StringBuilder(createPackage(rootPath, packageName));
path.append(File.separator).append(titleCase(table.getName())).append(".java");
File file = new File(path.toString());
if (!file.exists()){
file.createNewFile();
}
try (PrintWriter writer = new PrintWriter(file)){
writer.write(classCase(packageName, table));
}catch (IOException e){
throw e;
}
}
/**
* 首字母大写
* @param str 待转换的字符串
* @return 返回首字母大写的字符串
*/
private static String titleCase(String str) {
if (str!=null&&str.trim().length()>0){
char[] charArray = str.toCharArray();
charArray[0] -= 32;
return String.valueOf(charArray);
}else{
return str;
}
}
/**
* 创建包
* @param filePath 文件路径(不包含包)
* @param packageName 完整包名
* @return 返回最终的完整路径
*/
private static String createPackage(String filePath, String packageName){
File dir = new File(filePath);
if (!dir.exists()&&!dir.isDirectory()) {
dir.mkdir();
}
StringBuilder pathBuilder = new StringBuilder(filePath);
String pathes[] = packageName.split("\\.");
for (String path : pathes){
pathBuilder.append(File.separator).append(path);
File file = new File(pathBuilder.toString());
if (!file.exists()&&!file.isDirectory()) {
file.mkdir();
}
}
return pathBuilder.toString();
}
/**
* 完整类字符串
* @param packageName 包名
* @param table 数据表对象
* @return 返回完整类字符串
*/
private static String classCase(String packageName, Table table) {
StringBuilder builder = new StringBuilder();
builder.append(packageCase(packageName));
if (table.getRemark().trim().length()>0){
builder.append("// ").append(table.getRemark()).append("\n");
}
builder.append("public class ").append(titleCase(table.getName())).append(" {\n");
Set columnSet = table.getColumnSet();
for (Column column : columnSet){
builder.append(fieldCase(column));
}
builder.append("\n");
for (Column column : columnSet){
builder.append(setterCase(column.getName(), column.getJavaType()));
builder.append(getterCase(column.getName(), column.getJavaType()));
}
builder.append("}");
return builder.toString();
}
/**
* 类中的包名字符串
* @param packageName 完整包名
* @return 返回包名字符串
*/
private static String packageCase(String packageName) {
StringBuilder builder = new StringBuilder();
builder.append("package ").append(packageName).append(";\n\n");
return builder.toString();
}
/**
* 类中的属性字符串
* @param column 列对象
* @return 返回属性字符串
*/
private static String fieldCase(Column column) {
StringBuilder builder = new StringBuilder();
if (column.getRemark().trim().length()>0){
builder.append("\t// ").append(column.getRemark());
if (column.getDefaultValue()!=null){
builder.append(", 默认值:").append(column.getDefaultValue());
}
}else{
if (column.getDefaultValue()!=null) {
builder.append("\t// ").append("默认值:").append(column.getDefaultValue());
}
}
builder.append("\n\tpublic ").append(getTypeString(column.getJavaType())).append(" ").append(column.getName()).append(";\n");
return builder.toString();
}
/**
* 类中的set方法字符串
* @param fieldName 字段名
* @param type 类型
* @return 返回set方法字符串
*/
private static String setterCase(String fieldName, Class type) {
StringBuilder builder = new StringBuilder();
builder.append("\tpublic void set").append(titleCase(fieldName))
.append("(").append(getTypeString(type)).append(" ").append(fieldName).append(") {\n")
.append("\t\tthis.").append(fieldName).append(" = ").append(fieldName).append(";\n")
.append("\t}\n");
return builder.toString();
}
/**
* 类中的get方法字符串
* @param fieldName 字段名
* @param type 类型
* @return 返回get方法字符串
*/
private static String getterCase(String fieldName, Class type) {
StringBuilder builder = new StringBuilder();
builder.append("\tpublic ").append(getTypeString(type)).append(" get").append(titleCase(fieldName))
.append("() {\n")
.append("\t\treturn this.").append(fieldName).append(";\n")
.append("\t}\n");
return builder.toString();
}
/**
* 获取对应的类型
* @param type 类型
* @return 返回对应类型字符串
*/
private static String getTypeString(Class type) {
StringBuilder builder = new StringBuilder();
builder.append(type.getSimpleName());
return builder.substring(builder.lastIndexOf(".")+1);
}
}