package com.nmore.smile.utils;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
public class GenEntity {
private static final String PATH = "";
private static String PACKAGE = "";
true
private static final String DATABASE = "imall";
private static final String URL ="jdbc:mysql://localhost:3306/"+ DATABASE +"?useUnicode=true&characterEncoding=UTF8&useSSL=false";
private static final String NAME = "root";
private static final String PASS = "123456";
private static final String DRIVER ="com.mysql.jdbc.Driver";
private String[] colnames;
private String[] colTypes;
private boolean f_util = false;
private Statement stm;
private ResultSet rs;
private Connection con;
public GenEntity(){
con = connectionDB();
try {
String[] arr = new String[]{"attr_name", "attr_value"};
for(String str : arr){
this.init(str, null);
}
} catch (Exception e){
e.printStackTrace();
} finally {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
System.out.println("Init..");
new GenEntity();
System.out.println("Init Complete..");
}
@SuppressWarnings("unused")
public void init(String tablename, String notes){
try {
String sql = "show full columns from `" + tablename + "`";
stm = con.createStatement();
rs = stm.executeQuery(sql);
rs.last();
int size = rs.getRow();
rs.beforeFirst();
colnames = new String[size];
colTypes = new String[size];
int i = 0;
while(rs.next()){
colnames[i] = rs.getString("Field");
colTypes[i] = rs.getString("Type").replaceAll("[(].*[)]", "");
if(colTypes[i].equalsIgnoreCase("datetime")) f_util = true;
i++;
}
if(notes == null){
notes = this.queryTableNotes(tablename).toString();
}
String content = parse(colnames, colTypes, tablename, notes);
try {
File directory = new File("");
String path = this.getClass().getResource("").getPath();
if(PATH == ""){
path = directory.getAbsolutePath() + "\\src\\main\\java\\" + path.substring(path.lastIndexOf("com/", path.length()), path.length()).replace("/", "\\");
}else{
path = PATH;
}
FileWriter fw = new FileWriter(path + initcap(tablename) + "Entity.java");
PrintWriter pw = new PrintWriter(fw);
pw.println(content);
pw.flush();
pw.close();
} catch (Exception ex){
ex.printStackTrace();
}
} catch (Exception e){
e.printStackTrace();
}
}
@SuppressWarnings("unchecked")
public void initAll(){
Map<String, String> table_notes = (Map<String, String>) queryTableNotes(null);
if(table_notes != null){
for (Map.Entry<String, String> entry : table_notes.entrySet()) {
this.init(entry.getKey(), entry.getValue());
}
}
}
true
private String parse(String[] colnames, String[] colTypes, String tablename, String notes) throws SQLException {
String entity = initcap(tablename);
StringBuffer sb = new StringBuffer();
String path = this.getClass().getResource("").getPath();
PACKAGE = PACKAGE != ""?PACKAGE:path.substring(path.lastIndexOf("com/", path.length()), path.length()-1).replace("/", ".");
sb.append("package "+ PACKAGE +";\r\n\r\n");
sb.append("import com.nmore.smile.base.BaseEntity;\r\n");
if(f_util){
sb.append("import java.util.Date;\r\n");
f_util = false;
}
sb.append("\r\n");
sb.append("/**\r\n");
sb.append("* @ClassName: "+ entity +" \r\n");
sb.append("* @Description: "+ notes +" \r\n");
sb.append("* @Author devfzm@gmail.com \r\n");
sb.append("* @Date "+ new Date() +" \r\n");
sb.append("*/\r\n");
sb.append("\r\n@SuppressWarnings(\"serial\")\r\npublic class " + initcap(tablename) + "Entity extends BaseEntity {\r\n");
processAllAttrs(sb);
processAllMethod(sb);
sb.append("}\r\n");
return sb.toString();
}
private void processAllAttrs(StringBuffer sb) throws SQLException {
int i = 0;
rs.beforeFirst();
while(rs.next()) {
sb.append("\t//" + rs.getString("Comment"));
sb.append("\n\tprivate " + sqlType2JavaType(colTypes[i]) + " " + colnames[i] + ";\r\n");
i++;
}
sb.append("\r\n");
rs.close();
stm.close();
}
private void processAllMethod(StringBuffer sb) {
for (int i = 0; i < colnames.length; i++) {
sb.append("\tpublic void set" + initcap(colnames[i]) + "(" + sqlType2JavaType(colTypes[i]) + " " +
colnames[i] + "){\r\n");
sb.append("\t\tthis." + colnames[i] + "=" + colnames[i] + ";\r\n");
sb.append("\t}\r\n");
sb.append("\tpublic " + sqlType2JavaType(colTypes[i]) + " get" + initcap(colnames[i]) + "(){\r\n");
sb.append("\t\treturn " + colnames[i] + ";\r\n");
sb.append("\t}\r\n");
}
}
private String sqlType2JavaType(String sqlType) {
if(sqlType.equalsIgnoreCase("bit")){
return "Boolean";
}else if(sqlType.equalsIgnoreCase("tinyint")){
return "byte";
}else if(sqlType.equalsIgnoreCase("smallint")){
return "short";
}else if(sqlType.equalsIgnoreCase("int")){
return "Integer";
}else if(sqlType.equalsIgnoreCase("bigint")){
return "long";
}else if(sqlType.equalsIgnoreCase("float")){
return "float";
}else if(sqlType.equalsIgnoreCase("decimal") || sqlType.equalsIgnoreCase("numeric")
|| sqlType.equalsIgnoreCase("real") || sqlType.equalsIgnoreCase("money")
|| sqlType.equalsIgnoreCase("smallmoney")){
return "Double";
}else if(sqlType.equalsIgnoreCase("varchar") || sqlType.equalsIgnoreCase("char")
|| sqlType.equalsIgnoreCase("nvarchar") || sqlType.equalsIgnoreCase("nchar")
|| sqlType.equalsIgnoreCase("text")){
return "String";
}else if(sqlType.equalsIgnoreCase("datetime")){
return "Date";
}else if(sqlType.equalsIgnoreCase("image")){
return "Blod";
}
return null;
}
private String initcap(String str) {
char[] ch = str.toCharArray();
if(ch[0] >= 'a' && ch[0] <= 'z'){
ch[0] = (char)(ch[0] - 32);
}
return new String(ch);
}
public Object queryTableNotes(String tableName){
try {
String sql = "select table_name,table_comment from information_schema.`TABLES` WHERE table_schema = '"+ DATABASE +"'";
if(tableName != null){
sql += " and table_name='"+ tableName +"'";
}
Statement sta = con.createStatement();
ResultSet rst = sta.executeQuery(sql);
if(tableName != null){
String str = "";
while(rst.next()){
str = rst.getString("table_comment");
}
sta.close();
rst.close();
return str;
}else{
Map<String, String> map = new HashMap<String, String>();
while(rst.next()){
map.put(rst.getString("table_name"), rst.getString("table_comment"));
}
sta.close();
rst.close();
return map;
}
} catch (Exception e){
e.printStackTrace();
return null;
}
}
public Connection connectionDB(){
try{
Class.forName(DRIVER);
Connection conn = DriverManager.getConnection(URL, NAME, PASS);
return conn;
}catch(Exception e){
e.printStackTrace();
return null;
}
}
}