Commit 762d5473 authored by Quxl's avatar Quxl

x

parent dcc3df25
package com.egolm.common.jdbc.dialect;
import java.io.File;
import java.io.IOException;
import java.util.List;
import com.egolm.common.FileUtil;
import com.egolm.common.StringUtil;
/**
*
* @Description 生成Bean的工具类
* @author 曲欣亮
* @date 2016年4月22日
* @since 2016年4月22日
*/
public class ReverseUtil {
public static void main(String[] args) {
String regex = "^[a-z][A-Z].*$";
System.out.println("nQd".matches(regex));
}
/**
* 功能:生成实体类主体代码
*
* @param columnNames
* @param colTypes
* @return
* @throws IOException
*/
public static void parseEntity(String pkg_name, String author, String root, String name, String comment, List<String> paramyKeys, List<String> columnNames, List<String> colTypes, List<String> colComments) throws IOException {
StringBuffer sb = new StringBuffer();
sb.append("package " + pkg_name + ";" + "\n\n");
sb.append("import javax.persistence.Entity;" + "\n\n");
sb.append("import javax.persistence.Column;" + "\n\n");
if(paramyKeys.size() > 0) {
sb.append("import javax.persistence.Id;" + "\n\n");
}
boolean is_date = false;
boolean is_decimal = false;
for(String columnType : colTypes) {
if ((columnType.equalsIgnoreCase("datetime") || columnType.equalsIgnoreCase("date")) && !is_date) {
sb.append("import java.util.Date;" + "\n\n");
is_date = true;
} else if ((columnType.contains("decimal") || columnType.contains("numeric")) && !is_decimal) {
sb.append("import java.math.BigDecimal;" + "\n\n");
is_decimal = true;
}
}
// sb.append("/**" + "\n");
// sb.append(" * @author " + author + "\n");
// sb.append(" * @since " + DateUtil.format(new Date(), DateUtil.FMT_DATE) + "\n");
// sb.append(" * @version v-1.0" + "\n");
// sb.append(" */" + "\n");
sb.append("@Entity(name=\"" + name + "\")" + "\n");
sb.append("public class " + StringUtil.upperFirst(name) + " implements java.io.Serializable {" + "\n\n" + "\tprivate static final long serialVersionUID = 1L;" + "\n\n");
processAllAttrs(paramyKeys, columnNames, colTypes, colComments, sb);
// processAllConstructor(StringUtil.upperFirst(name), comment, columnNames, colTypes, colComments, sb);
processAllMethod(columnNames, colTypes, colComments, sb);
sb.append("}" + "\n");
String outputPath = root + File.separator + pkg_name.replace(".", File.separator) + File.separator + StringUtil.upperFirst(name) + ".java";
File file = new File(outputPath).getParentFile();
if (!file.exists()) {
file.mkdirs();
}
FileUtil.stringToFile(outputPath, sb.toString());
System.out.println("Create: " + outputPath);
}
/**
* 功能:生成所有属性
*
* @param sbuffer
*/
public static void processAllAttrs(List<String> paramyKeys, List<String> columnNames, List<String> colTypes, List<String> columnComments, StringBuffer sbuffer) {
for (int i = 0; i < columnNames.size(); i++) {
String columnName = columnNames.get(i);
if(StringUtil.contains(paramyKeys.toArray(new String[paramyKeys.size()]), columnName)) {
sbuffer.append("\t@Id" + "\n");
}
sbuffer.append("\t@Column(columnDefinition=\"" + colTypes.get(i) + (StringUtil.isNotBlank(columnComments.get(i)) ? (" COMMENT '" + columnComments.get(i).replaceAll("\\s+", " ") + "'") : "") + "\")\n");
sbuffer.append("\tprivate " + sqlType2JavaType(colTypes.get(i)) + " " + columnName + ";" + "\n\n");
}
sbuffer.append("\n");
}
/**
* 功能:生成构造方法
* @param sb
*/
public static void processAllConstructor(String name, String comment, List<String> colnames, List<String> colTypes, List<String> colComments, StringBuffer sb) {
sb.append("\t/**" + "\n");
sb.append("\t * " + "无参数构造方法" + "\n");
sb.append("\t */" + "\n");
sb.append("\tpublic " + name + " () {" + "\n" + "\t\tsuper();" + "\n" + "\t}" + "\n\n");
sb.append("\t/**" + "\n");
sb.append("\t * " + "全参数构造方法" + "\n");
for (int i = 0; i < colnames.size(); i++) {
sb.append("\t * @param " + colnames.get(i) + " " + (StringUtil.isNotBlank(colComments.get(i)) ? colComments.get(i) : "") + "\n");
}
sb.append("\t */" + "\n");
sb.append("\tpublic " + name + "(");
for (int i = 0; i < colnames.size(); i++) {
sb.append(sqlType2JavaType(colTypes.get(i)) + " " + colnames.get(i));
if(i < colnames.size() - 1) {
sb.append(", ");
}
}
sb.append(") {" + "\n");
for (int i = 0; i < colnames.size(); i++) {
sb.append("\t\tthis." + colnames.get(i) + " = " + colnames.get(i) + ";" + "\n");
}
sb.append("\t}" + "\n\n");
}
/**
* 功能:生成所有方法
* @param sb
*/
public static void processAllMethod(List<String> colnames, List<String> colTypes, List<String> colComments, StringBuffer sb) {
String regex = "^[a-z][A-Z].*$";
for (int i = 0; i < colnames.size(); i++) {
String columnName = colnames.get(i);
String name = (columnName.matches(regex) ? columnName : StringUtil.upperFirst(columnName));
sb.append("\tpublic void set" + name + "(" + sqlType2JavaType(colTypes.get(i)) + " " + colnames.get(i) + ") {" + "\n");
sb.append("\t\tthis." + colnames.get(i) + " = " + colnames.get(i) + ";" + "\n");
sb.append("\t}" + "\n\n");
sb.append("\tpublic " + sqlType2JavaType(colTypes.get(i)) + " get" + name + "() {" + "\n");
sb.append("\t\treturn " + colnames.get(i) + ";" + "\n");
sb.append("\t}" + "\n\n");
}
}
/**
* 功能:获得列的数据类型
* @param sqlType
* @return
*/
public static String sqlType2JavaType(String sqlType) {
if(sqlType.startsWith("bit")){
return "Boolean";
} else if(sqlType.startsWith("int") || sqlType.startsWith("integer") || sqlType.startsWith("tinyint") || sqlType.startsWith("smallint")){
return "Integer";
} else if(sqlType.startsWith("bigint")){
return "Long";
} else if(sqlType.startsWith("float")){
return "Float";
} else if(sqlType.startsWith("decimal") || sqlType.startsWith("numeric")) {
return "BigDecimal";
} else if(sqlType.startsWith("double") || sqlType.startsWith("real") || sqlType.startsWith("money") || sqlType.startsWith("smallmoney")){
return "Double";
} else if(sqlType.startsWith("varchar") || sqlType.startsWith("char") || sqlType.startsWith("nvarchar") || sqlType.startsWith("nchar") || sqlType.startsWith("text") || sqlType.startsWith("longtext")){
return "String";
} else if(sqlType.startsWith("datetime") || sqlType.startsWith("date") || sqlType.startsWith("timestamp")){
return "Date";
} else if(sqlType.startsWith("image")){
return "Byte[]";
} else {
return "";
}
// throw new RuntimeException("Unknown Type: " + sqlType);
}
}
package com.egolm.common.jdbc.dialect;
import java.io.File;
import java.io.IOException;
import java.util.List;
import com.egolm.common.FileUtil;
import com.egolm.common.StringUtil;
/**
*
* @Description 生成Bean的工具类
* @author 曲欣亮
* @date 2016年4月22日
* @since 2016年4月22日
*/
public class ReverseUtil {
public static void main(String[] args) {
String regex = "^[a-z][A-Z].*$";
System.out.println("nQd".matches(regex));
}
/**
* 功能:生成实体类主体代码
*
* @param columnNames
* @param colTypes
* @return
* @throws IOException
*/
public static void parseEntity(String pkg_name, String author, String root, String name, String comment, List<String> paramyKeys, List<String> columnNames, List<String> colTypes, List<String> colComments) throws IOException {
StringBuffer sb = new StringBuffer();
sb.append("package " + pkg_name + ";" + "\n\n");
sb.append("import javax.persistence.Entity;" + "\n\n");
sb.append("import javax.persistence.Column;" + "\n\n");
if(paramyKeys.size() > 0) {
sb.append("import javax.persistence.Id;" + "\n\n");
}
boolean is_date = false;
boolean is_decimal = false;
for(String columnType : colTypes) {
if ((columnType.equalsIgnoreCase("datetime") || columnType.equalsIgnoreCase("date")) && !is_date) {
sb.append("import java.util.Date;" + "\n\n");
is_date = true;
} else if ((columnType.contains("decimal") || columnType.contains("numeric")) && !is_decimal) {
sb.append("import java.math.BigDecimal;" + "\n\n");
is_decimal = true;
}
}
// sb.append("/**" + "\n");
// sb.append(" * @author " + author + "\n");
// sb.append(" * @since " + DateUtil.format(new Date(), DateUtil.FMT_DATE) + "\n");
// sb.append(" * @version v-1.0" + "\n");
// sb.append(" */" + "\n");
sb.append("@Entity(name=\"" + name + "\")" + "\n");
sb.append("public class " + StringUtil.upperFirst(name) + " implements java.io.Serializable {" + "\n\n" + "\tprivate static final long serialVersionUID = 1L;" + "\n\n");
processAllAttrs(paramyKeys, columnNames, colTypes, colComments, sb);
// processAllConstructor(StringUtil.upperFirst(name), comment, columnNames, colTypes, colComments, sb);
processAllMethod(columnNames, colTypes, colComments, sb);
sb.append("}" + "\n");
String outputPath = root + File.separator + pkg_name.replace(".", File.separator) + File.separator + StringUtil.upperFirst(name) + ".java";
File file = new File(outputPath).getParentFile();
if (!file.exists()) {
file.mkdirs();
}
FileUtil.stringToFile(outputPath, sb.toString());
System.out.println("Create: " + outputPath);
}
/**
* 功能:生成所有属性
*
* @param sbuffer
*/
public static void processAllAttrs(List<String> paramyKeys, List<String> columnNames, List<String> colTypes, List<String> columnComments, StringBuffer sbuffer) {
for (int i = 0; i < columnNames.size(); i++) {
String columnName = columnNames.get(i);
if(StringUtil.contains(paramyKeys.toArray(new String[paramyKeys.size()]), columnName)) {
sbuffer.append("\t@Id" + "\n");
}
sbuffer.append("\t@Column(columnDefinition=\"" + colTypes.get(i) + (StringUtil.isNotBlank(columnComments.get(i)) ? (" COMMENT '" + columnComments.get(i).replaceAll("\\s+", " ") + "'") : "") + "\")\n");
sbuffer.append("\tprivate " + sqlType2JavaType(colTypes.get(i)) + " " + columnName + ";" + "\n\n");
}
sbuffer.append("\n");
}
/**
* 功能:生成构造方法
* @param sb
*/
public static void processAllConstructor(String name, String comment, List<String> colnames, List<String> colTypes, List<String> colComments, StringBuffer sb) {
sb.append("\t/**" + "\n");
sb.append("\t * " + "无参数构造方法" + "\n");
sb.append("\t */" + "\n");
sb.append("\tpublic " + name + " () {" + "\n" + "\t\tsuper();" + "\n" + "\t}" + "\n\n");
sb.append("\t/**" + "\n");
sb.append("\t * " + "全参数构造方法" + "\n");
for (int i = 0; i < colnames.size(); i++) {
sb.append("\t * @param " + colnames.get(i) + " " + (StringUtil.isNotBlank(colComments.get(i)) ? colComments.get(i) : "") + "\n");
}
sb.append("\t */" + "\n");
sb.append("\tpublic " + name + "(");
for (int i = 0; i < colnames.size(); i++) {
sb.append(sqlType2JavaType(colTypes.get(i)) + " " + colnames.get(i));
if(i < colnames.size() - 1) {
sb.append(", ");
}
}
sb.append(") {" + "\n");
for (int i = 0; i < colnames.size(); i++) {
sb.append("\t\tthis." + colnames.get(i) + " = " + colnames.get(i) + ";" + "\n");
}
sb.append("\t}" + "\n\n");
}
/**
* 功能:生成所有方法
* @param sb
*/
public static void processAllMethod(List<String> colnames, List<String> colTypes, List<String> colComments, StringBuffer sb) {
String regex = "^[a-z][A-Z].*$";
for (int i = 0; i < colnames.size(); i++) {
String columnName = colnames.get(i);
String name = (columnName.matches(regex) ? columnName : StringUtil.upperFirst(columnName));
sb.append("\tpublic void set" + name + "(" + sqlType2JavaType(colTypes.get(i)) + " " + colnames.get(i) + ") {" + "\n");
sb.append("\t\tthis." + colnames.get(i) + " = " + colnames.get(i) + ";" + "\n");
sb.append("\t}" + "\n\n");
sb.append("\tpublic " + sqlType2JavaType(colTypes.get(i)) + " get" + name + "() {" + "\n");
sb.append("\t\treturn " + colnames.get(i) + ";" + "\n");
sb.append("\t}" + "\n\n");
}
}
/**
* 功能:获得列的数据类型
* @param sqlType
* @return
*/
public static String sqlType2JavaType(String sqlType) {
if(sqlType.startsWith("bit")){
return "Boolean";
} else if(sqlType.startsWith("int") || sqlType.startsWith("integer") || sqlType.startsWith("tinyint") || sqlType.startsWith("smallint")){
return "Integer";
} else if(sqlType.startsWith("bigint")){
return "Long";
} else if(sqlType.startsWith("float")){
return "Float";
} else if(sqlType.startsWith("decimal") || sqlType.startsWith("numeric")) {
return "BigDecimal";
} else if(sqlType.startsWith("double") || sqlType.startsWith("real") || sqlType.startsWith("money") || sqlType.startsWith("smallmoney")){
return "Double";
} else if(sqlType.startsWith("varchar") || sqlType.startsWith("char") || sqlType.startsWith("nvarchar") || sqlType.startsWith("nchar") || sqlType.startsWith("text") || sqlType.startsWith("longtext") || sqlType.startsWith("mediumtext")){
return "String";
} else if(sqlType.startsWith("datetime") || sqlType.startsWith("date") || sqlType.startsWith("timestamp")){
return "Date";
} else if(sqlType.startsWith("image")){
return "Byte[]";
} else {
return "";
}
// throw new RuntimeException("Unknown Type: " + sqlType);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment