南来北往的客

是对还是错,问询南来北往的客。

根据库表结构自动生成java实体类(生成规范的变量及get/set方法).

/**
 * java根据库表结构自动生成java实体,在网上找了相关资料,已经有很多大侠实现了这功能。
 * 但是并没有一个使我自己满意的,要么生成的变量不规范,要么就是格式很乱。
 * 所以我参照网上一些已有的方法,自己动手改进了下,实现了可以生成规范的字段名及
 * get/set方法,只不过这里有个前提,就是在新建表字段时如果字段名较长,那么必须遵守用"_"隔开。
 * 其实只有这样才能规范,毕竟电脑也不是万能的,不可能根据你想要的格式自动生成规范的变量名及get/set方法。
 */

 

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;

public class GenEntityOracle {

 private String packageOutPath = "com.user.entity";//指定实体生成所在包的路径
 private String authorName = "张三";//作者名字
 private String tablename = "RECORD";//表名
 private String[] colnames; // 列名数组
 private String[] colTypes; //列名类型数组
 private int[] colSizes; //列名大小数组
 private boolean f_util = false; // 是否需要导入包java.util.*
 private boolean f_sql = false; // 是否需要导入包java.sql.*
    
    //数据库连接
  private static final String URL ="jdbc:oracle:thin:@192.168.1.113:1521:eport";
  private static final String NAME = "test";
  private static final String PASS = "test";
  private static final String DRIVER ="oracle.jdbc.driver.OracleDriver";

 /*
  * 构造函数
  */
 public GenEntityOracle(){
     //创建连接
     Connection con;
  //查要生成实体类的表
     String sql = "select * from " + tablename;
     Statement pStemt = null;
     try {
      try {
    Class.forName(DRIVER);
   } catch (ClassNotFoundException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
   }
      con = DriverManager.getConnection(URL,NAME,PASS);
   pStemt = (Statement) con.createStatement();
   ResultSet rs = pStemt.executeQuery(sql);
   ResultSetMetaData rsmd = rs.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("date") || colTypes[i].equalsIgnoreCase("timestamp")){
     f_util = true;
    }
    if(colTypes[i].equalsIgnoreCase("blob") || colTypes[i].equalsIgnoreCase("char")){
     f_sql = true;
    }
    colSizes[i] = rsmd.getColumnDisplaySize(i + 1);
   }
   
   String content = parse(colnames,colTypes,colSizes);
   
   try {
    File directory = new File("");
    //System.out.println("绝对路径:"+directory.getAbsolutePath());
    //System.out.println("相对路径:"+directory.getCanonicalPath());
    String path=this.getClass().getResource("").getPath();
    
    System.out.println(path);
    //System.out.println("src/?/"+path.substring(path.lastIndexOf("/com/", path.length())) );
//    String outputPath = directory.getAbsolutePath()+ "/src/"+path.substring(path.lastIndexOf("/com/", path.length()), path.length()) + initcap(tablename) + ".java";
    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 (SQLException e) {
   e.printStackTrace();
  } finally{
//   try {
//    con.close();
//   } catch (SQLException e) {
//    // TODO Auto-generated catch block
//    e.printStackTrace();
//   }
  }
    }

 /**
  * 功能:生成实体类主体代码
  * @param colnames
  * @param colTypes
  * @param colSizes
  * @return
  */
 private String parse(String[] colnames, String[] colTypes, int[] colSizes) {
  StringBuffer sb = new StringBuffer();
  
  sb.append("package " + this.packageOutPath + ";\r\n");
  //判断是否导入工具包
  if(f_util){
   sb.append("import java.util.Date;\r\n");
  }
  if(f_sql){
   sb.append("import java.sql.*;\r\n");
  }
  sb.append("\r\n");
  //注释部分
  sb.append("   /**\r\n");
  sb.append("    * "+initcap(tablename)+" 实体类\r\n");
  sb.append("    * "+new Date()+" "+this.authorName+"\r\n");
  sb.append("    */ \r\n");
  //实体部分
  sb.append("\r\n\r\npublic class " + initcap(tablename) + "{\r\n");
  processAllAttrs(sb);//属性
  processAllMethod(sb);//get set方法
  sb.append("}\r\n");
  
     //System.out.println("999999 = " +sb.toString());
  return sb.toString();
 }
 
 /**
  * 功能:生成所有属性
  * @param sb
  */
 private void processAllAttrs(StringBuffer sb) {
  
  for (int i = 0; i < colnames.length; i++) {
   sb.append("\tprivate " + sqlType2JavaType(colTypes[i]) + " " + defineVar(colnames[i]) + ";\r\n");
  }
  
 }

 /**
  * 功能:生成所有方法
  * @param sb
  */
 private void processAllMethod(StringBuffer sb) {
  
  for (int i = 0; i < colnames.length; i++) {
   sb.append("\n\tpublic void set" + transVar(colnames[i]) + "(" + sqlType2JavaType(colTypes[i]) + " " +
     defineVar(colnames[i]) + "){\r\n");
   sb.append("\t\tthis." + this.defineVar(colnames[i]) + "=" + this.defineVar(colnames[i]) + ";\r\n");
   sb.append("\t}\r\n");
   sb.append("\n\tpublic " + sqlType2JavaType(colTypes[i]) + " get" + transVar(colnames[i]) + "(){\r\n");
   sb.append("\t\treturn " + defineVar(colnames[i]) + ";\r\n");
   sb.append("\t}\r\n");
  }
  
 }
 
 /**
  * 功能:将输入字符串的首字母改成大写
  * @param str
  * @return
  */
 private String initcap(String str) {
  
  char[] ch = str.toLowerCase().toCharArray();
  System.out.println("6666 str = "+str.toString());
  if(ch[0] >= 'a' && ch[0] <= 'z'){
   ch[0] = (char)(ch[0] - 32);
  }
  
  return new String(ch);
 }
 
 /**
  * 用于生成get/set方法时
  * 功能:先将变量字母全变为小写,将第一个字母变为大写,将紧跟“_”后面一个字母大写,并去掉“_”.
  * @param str
  * @return
  */
 private String transVar(String str){
  System.out.println("111"+str);
  int index = 0;
  if(str.indexOf("_")!=-1){
   index = str.indexOf("_");
   str = str.replace("_", "");
  }
  System.out.println("222"+str);
  char[] ch = str.toLowerCase().toCharArray();
  if(ch[0] >= 'a' && ch[0] <= 'z'){
   ch[0] = (char)(ch[0] - 32);
   if(index!=0){
    ch[index] = (char)(ch[index] - 32);
   }
  }
  str =  new String(ch);
  System.out.println("333"+str);
  return str;
 }
 
 /**
  * 用于定义变量名
  * 功能:先将变量字母全变为小写,将紧跟“_”后面一个字母大写,并去掉“_”.
  * @param str
  * @return
  */
 private String defineVar(String str){
  int index = 0;
  if(str.indexOf("_")!=-1){
   index = str.indexOf("_");
   str = str.replace("_", "");
  }
  char[] ch = str.toLowerCase().toCharArray();
  if(ch[0] >= 'a' && ch[0] <= 'z'&&index!=0){
   ch[index] = (char)(ch[index] - 32);
  }
  str =  new String(ch);
  return str;
 }

 /**
  * 功能:获得列的数据类型
  * @param sqlType
  * @return
  */
 private String sqlType2JavaType(String sqlType) {
  
  if(sqlType.equalsIgnoreCase("binary_double")){
   return "double";
  }else if(sqlType.equalsIgnoreCase("binary_float")){
   return "float";
  }else if(sqlType.equalsIgnoreCase("blob")){
   return "byte[]";
  }else if(sqlType.equalsIgnoreCase("blob")){
   return "byte[]";
  }else if(sqlType.equalsIgnoreCase("char") || sqlType.equalsIgnoreCase("nvarchar2")
    || sqlType.equalsIgnoreCase("varchar2")){
   return "String";
  }else if(sqlType.equalsIgnoreCase("date") || sqlType.equalsIgnoreCase("timestamp")
     || sqlType.equalsIgnoreCase("timestamp with local time zone") 
     || sqlType.equalsIgnoreCase("timestamp with time zone")){
   return "Date";
  }else if(sqlType.equalsIgnoreCase("number")){
   return "Long";
  }
  
  return "String";
 }
 
 /**
  * 出口
  * TODO
  * @param args
  */
 public static void main(String[] args) {
  
  new GenEntityOracle();
  
 }

}



 

阅读更多
个人分类: java
上一篇eclipse/myeclipse一些常用的快捷键
下一篇Oracle sql日期比较(to_date).
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭