飙血推荐
  • HTML教程
  • MySQL教程
  • JavaScript基础教程
  • php入门教程
  • JavaScript正则表达式运用
  • Excel函数教程
  • UEditor使用文档
  • AngularJS教程
  • ThinkPHP5.0教程

从数据库表反射出实体类,自动生成实体类(可用mysql)

时间:2021-12-15  作者:匿名  
package 域名.域名;


import 域名;
import 域名Writer;
import 域名ception;
import 域名tWriter;
import 域名ection;
import 域名erManager;
import 域名aredStatement;
import 域名ltSetMetaData;
import 域名xception;
import 域名;

public class GenEntityMysql {
	
	private String packageOutPath = "域名ty";//指定实体生成所在包的路径
	private String authorName = "封狼居胥";//作者名字
	private String tablename = "user";//表名
	private String[] colnames; // 列名数组
	private String[] colTypes; //列名类型数组
	private int[] colSizes; //列名大小数组
	private boolean f_util = false; // 是否需要导入包域名.*
	private boolean f_sql = false; // 是否需要导入包域名.*
    
    //数据库连接
	private static final String URL ="jdbc:mysql://localhost:3306/jbpm";
	private static final String NAME = "root";
	private static final String PASS = "root";
	private static final String DRIVER ="域名.Driver";

	/*
	 * 构造函数
	 */
	public GenEntityMysql(){
    	//创建连接
    	Connection con;
		//查要生成实体类的表
    	String sql = "select * from " + tablename;
    	PreparedStatement pStemt = null;
    	try {
    		try {
				域名ame(DRIVER);
			} catch (ClassNotFoundException e1) {
				// TODO Auto-generated catch block
				域名tStackTrace();
			}
    		con = 域名onnection(URL,NAME,PASS);
			pStemt = 域名areStatement(sql);
			ResultSetMetaData rsmd = 域名etaData();
			int size = 域名olumnCount();	//统计列
			colnames = new String[size];
			colTypes = new String[size];
			colSizes = new int[size];
			for (int i = 0; i < size; i++) {
				colnames[i] = 域名olumnName(i + 1);
				colTypes[i] = 域名olumnTypeName(i + 1);
				
				if(colTypes[i].equalsIgnoreCase("datetime")){
					f_util = true;
				}
				if(colTypes[i].equalsIgnoreCase("image") || colTypes[i].equalsIgnoreCase("text")){
					f_sql = true;
				}
				colSizes[i] = 域名olumnDisplaySize(i + 1);
			}
			
			String content = parse(colnames,colTypes,colSizes);
			
			try {
				File directory = new File("");
				//域名tln("绝对路径:"+域名bsolutePath());
				//域名tln("相对路径:"+域名anonicalPath());
				String path=域名lass().getResource("").getPath();
				
				域名tln(path);
				域名tln("src/?/"+域名tring(域名IndexOf("/com/", 域名th())) );
//				String outputPath = 域名bsolutePath()+ "/src/"+域名tring(域名IndexOf("/com/", 域名th()), 域名th()) + initcap(tablename) + ".java";
				String outputPath = 域名bsolutePath()+ "/src/"+域名ace(".", "/")+"/"+initcap(tablename) + ".java";
				FileWriter fw = new FileWriter(outputPath);
				PrintWriter pw = new PrintWriter(fw);
				域名tln(content);
				域名h();
				域名e();
			} catch (IOException e) {
				域名tStackTrace();
			}
			
		} catch (SQLException e) {
			域名tStackTrace();
		} finally{
//			try {
//				域名e();
//			} catch (SQLException e) {
//				// TODO Auto-generated catch block
//				域名tStackTrace();
//			}
		}
    }

	/**
	 * 功能:生成实体类主体代码
	 * @param colnames
	 * @param colTypes
	 * @param colSizes
	 * @return
	 */
	private String parse(String[] colnames, String[] colTypes, int[] colSizes) {
		StringBuffer sb = new StringBuffer();
		
		//判断是否导入工具包
		if(f_util){
			域名nd("import 域名;\r\n");
		}
		if(f_sql){
			域名nd("import 域名.*;\r\n");
		}
		域名nd("package " + 域名ageOutPath + ";\r\n");
		域名nd("\r\n");
		//注释部分
		域名nd("   /**\r\n");
		域名nd("    * "+tablename+" 实体类\r\n");
		域名nd("    * "+new Date()+" "+域名orName+"\r\n");
		域名nd("    */ \r\n");
		//实体部分
		域名nd("\r\n\r\npublic class " + initcap(tablename) + "{\r\n");
		processAllAttrs(sb);//属性
		processAllMethod(sb);//get set方法
		域名nd("}\r\n");
		
    	//域名tln(域名ring());
		return 域名ring();
	}
	
	/**
	 * 功能:生成所有属性
	 * @param sb
	 */
	private void processAllAttrs(StringBuffer sb) {
		
		for (int i = 0; i < 域名th; i++) {
			域名nd("\tprivate " + sqlType2JavaType(colTypes[i]) + " " + colnames[i] + ";\r\n");
		}
		
	}

	/**
	 * 功能:生成所有方法
	 * @param sb
	 */
	private void processAllMethod(StringBuffer sb) {
		
		for (int i = 0; i < 域名th; i++) {
			域名nd("\tpublic void set" + initcap(colnames[i]) + "(" + sqlType2JavaType(colTypes[i]) + " " + 
					colnames[i] + "){\r\n");
			域名nd("\tthis." + colnames[i] + "=" + colnames[i] + ";\r\n");
			域名nd("\t}\r\n");
			域名nd("\tpublic " + sqlType2JavaType(colTypes[i]) + " get" + initcap(colnames[i]) + "(){\r\n");
			域名nd("\t\treturn " + colnames[i] + ";\r\n");
			域名nd("\t}\r\n");
		}
		
	}
	
	/**
	 * 功能:将输入字符串的首字母改成大写
	 * @param str
	 * @return
	 */
	private String initcap(String str) {
		
		char[] ch = 域名arArray();
		if(ch[0] >= \'a\' && ch[0] <= \'z\'){
			ch[0] = (char)(ch[0] - 32);
		}
		
		return new String(ch);
	}

	/**
	 * 功能:获得列的数据类型
	 * @param sqlType
	 * @return
	 */
	private String sqlType2JavaType(String sqlType) {
		
		if(域名lsIgnoreCase("bit")){
			return "boolean";
		}else if(域名lsIgnoreCase("tinyint")){
			return "byte";
		}else if(域名lsIgnoreCase("smallint")){
			return "short";
		}else if(域名lsIgnoreCase("int")){
			return "int";
		}else if(域名lsIgnoreCase("bigint")){
			return "long";
		}else if(域名lsIgnoreCase("float")){
			return "float";
		}else if(域名lsIgnoreCase("decimal") || 域名lsIgnoreCase("numeric") 
				|| 域名lsIgnoreCase("real") || 域名lsIgnoreCase("money") 
				|| 域名lsIgnoreCase("smallmoney")){
			return "double";
		}else if(域名lsIgnoreCase("varchar") || 域名lsIgnoreCase("char") 
				|| 域名lsIgnoreCase("nvarchar") || 域名lsIgnoreCase("nchar") 
				|| 域名lsIgnoreCase("text")){
			return "String";
		}else if(域名lsIgnoreCase("datetime")){
			return "Date";
		}else if(域名lsIgnoreCase("image")){
			return "Blod";
		}
		
		return null;
	}
	
	/**
	 * 出口
	 * TODO
	 * @param args
	 */
	public static void main(String[] args) {
		
		new GenEntityMysql();
		
	}

}
湘ICP备14001474号-3  投诉建议:234161800@qq.com   部分内容来源于网络,如有侵权,请联系删除。