使用方法_注:1_2只适用于普通的web项目(绿色部分),_Maven只适用于maven项目(蓝色部分),使用建实体类的方式(黑色部分),在util包下创建,创建类后直接运行此类,使用此类前,要先与数据库进行连接
package club.lygangdai.a;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
/**
* @ClassName: GeneratingEntityClass
* @Description: TODO(根据数据库表名生成对应实体类,注意:同一路径下对已存在相同类名的类会被替换)
* @author Uncle liu
* @date 2018年6月11日 下午2:11:18
*
*/
public class GeneratingEntityClass_1_2 {
private String packageOutPath; //指定实体生成所在包的路径
private String authorName; //作者名字
private String tablename; //表名
private String explain; //类说明
private String[] colnames; // 列名数组
private String[] colTypes; //列名类型数组
private int[] colSizes; //列名大小数组
private boolean f_util = false; // 是否需要导入包java.util.*
private boolean f_sql = false; // 是否需要导入包java.sql.*
private Connection con = null;
private String outputPath = null;//生成路径
/**
*
* @Description: TODO(无参构造方法)
*/
public GeneratingEntityClass_1_2() {
}
/**
*
* @Description: TODO(有参构造函数/用于生成一个表实体类/生成一个多表联查)
* @param packageOutPath 指定实体生成所在包的路径 (如:com.ly.calculator.entity)
* @param tablename 表名
* @param authorName 作者名字
* @param explain 类说明
* @param Connection 传入数据库连接
* 根据数据库表名生成对应实体类,注意:同一路径下对已存在相同类名的类会被替换
*/
public GeneratingEntityClass_1_2(String packageOutPath,String tablename,String authorName,String explain,Connection con){
this.packageOutPath=packageOutPath;
this.tablename=tablename;
this.authorName=authorName;
this.explain=explain;
this.con=con;
}
/**
*
* @Title: ToPerformAll
* @Description: TODO(匹配无参构造方法一起使用,生成对应数据库所有表实体类)
* @param packageOutPath 指定实体生成所在包的路径 (如:com.ly.calculator.entity)
* @param authorName 作者名字
* @param Connection 传入数据库连接
*/
public void ToPerformAll(String packageOutPath,String authorName,Connection con) {
this.packageOutPath=packageOutPath;
this.authorName=authorName;
this.con=con;
Statement stmt = null;
try {
stmt = con.createStatement();
// 执行语句!
ResultSet rs = stmt.executeQuery("show tables; ");
// 使用ResultSet对象!
while (rs.next()) {
this.tablename=rs.getString(1);//表名
//生成实体类
generate();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
*
* @Title: generate
* @Description: TODO(生成实体类) void
*/
public void generate() {
try {
//查要生成实体类的表
String sql = "select * from " + tablename;
PreparedStatement pStemt = null;
pStemt = con.prepareStatement(sql);
ResultSetMetaData rsmd = pStemt.getMetaData();
int size = rsmd.getColumnCount(); //统计列
colnames = new String[size];
colTypes = new String[size];
colSizes = new int[size];
for (int i = 0; i < size; i++) {
colnames[i] = rsmd.getColumnName(i + 1);
colTypes[i] = rsmd.getColumnTypeName(i + 1);
if(colTypes[i].equalsIgnoreCase("datetime")){
f_util = true;
}
if(colTypes[i].equalsIgnoreCase("image") || colTypes[i].equalsIgnoreCase("text")){
f_sql = true;
}
colSizes[i] = rsmd.getColumnDisplaySize(i + 1);
}
String content = parse(colnames,colTypes,colSizes);
try {
File directory = new File("");
//String path=this.getClass().getResource("").getPath();
String outputPath = directory.getAbsolutePath()+ "\\src\\"+this.packageOutPath.replace(".", "\\")+"\\"+initcap(tablename) + ".java";
FileWriter fw = new FileWriter(outputPath);
PrintWriter pw = new PrintWriter(fw);
pw.println(content);
pw.flush();
pw.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (Exception e) {
// TODO: handle exception
}
}
/**
*
* @Title: generateSelect
* @Description: TODO(多表联查生成实体类)
* @param sql 查询sql
* @param alias 查询表的别名
*/
public void generateSelect(String sql) {
try {
PreparedStatement pStemt = null;
pStemt = con.prepareStatement(sql);
ResultSetMetaData rsmd = pStemt.getMetaData();
int size = rsmd.getColumnCount(); //统计列
colnames = new String[size];
colTypes = new String[size];
colSizes = new int[size];
for (int i = 0; i < size; i++) {
colnames[i] = rsmd.getColumnName(i + 1);
colTypes[i] = rsmd.getColumnTypeName(i + 1);
if(colTypes[i].equalsIgnoreCase("datetime")){
f_util = true;
}
if(colTypes[i].equalsIgnoreCase("image") || colTypes[i].equalsIgnoreCase("text")){
f_sql = true;
}
colSizes[i] = rsmd.getColumnDisplaySize(i + 1);
}
String content = parse(colnames,colTypes,colSizes);
try {
File directory = new File("");