通过ORACEL的数据字典自动生成JAVA及FLEX实体类

package tool;

 

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileOutputStream;

import java.io.FileWriter;

import java.io.IOException;

import java.io.OutputStreamWriter;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

 

import domain.sys.OracelDict;

 

/**

 *

 * @ClassName: OracleTableToVO

 * @Description: 该类通过Oracle自带的数据字典自动生成java的domain及Flex的ValueObject类

 * @author 朱邦辉 zbh751225@163.com

 * @date May 21, 2012 9:08:16 AM

 *

 */

public class OracleTableToVO {

    private String javaClassName;// java类名

    private String as3ClassName;// flex类名

    private String javapackageName;// java包名

    private String as3packageName;// java包名

    private String tableName;// 数据库表名

 

    public List<OracelDict> oraceltab = new ArrayList<OracelDict>();// 用于存放oracel表名、列名、列注释

    public List<OracelDict> javatab = new ArrayList<OracelDict>();// 用于存放java表名、列名、列注释

    public List<OracelDict> as3tab = new ArrayList<OracelDict>();// 用于存放as3表名、列名、列注释

 

    public List<String> as3Property = new ArrayList<String>();

    public List<String> javaProperty = new ArrayList<String>();

    public List<String> as3Construction = new ArrayList<String>();

    public List<String> java3Construction = new ArrayList<String>();

 

    private static final String LINE = "\r\n";

    private static final String TAB = "\t";

 

    /**

     * @param javaClassName

     *            生成的JAVA类的文件名

     * @param as3ClassName

     *            生成Flex中OvalueObject类的文件名

     * @param javapackageName

     *            指定生成java类的包名

     * @param as3packageName

     *            指定生成Flex类的包名

     * @param javapath

     *            指定java文件存放路径

     * @param as3path

     *            指定flex文件存放路径

     * @param tableName

     *            指定Oracel中的表名

     * @throws IOException

     */

    public OracleTableToVO(String javaClassName, String as3ClassName,

           String javapackageName, String as3packageName, String javapath,

           String as3path, String tableName) throws IOException {

       super();

       this.javaClassName = javaClassName;

       this.as3ClassName = as3ClassName;

       this.javapackageName = javapackageName;

       this.as3packageName = as3packageName;

       this.tableName = tableName.toUpperCase();

       // ********************

       getOracelDict(tableName);// 从数据取出指定表结构

       // 根据oracel的表结构生成java和As3表结构

       getJava_As3DictField();

       // getGenAs3()

       // getGenJava()

       //01.out = new BufferedWriter(new OutputStreamWriter((new FileOutputStream(tempFile)),"utf-8"));

       File as3f= new File(as3path + "\\" + as3ClassName + "VO.as");

       //BufferedWriter as3bw = new BufferedWriter(new FileWriter(as3f));

       BufferedWriter as3bw=new BufferedWriter(new OutputStreamWriter((new FileOutputStream(as3f)),"utf-8"));

       as3bw.write(getGenAs3());

       as3bw.close();

 

       File javaf = new File(javapath + "\\" + javaClassName + ".java");

       //BufferedWriter javabw = new BufferedWriter(new FileWriter(javaf));

       BufferedWriter javabw = new BufferedWriter(new OutputStreamWriter((new FileOutputStream(javaf)),"utf-8"));

       javabw.write(getGenJava());

       javabw.close();

 

    }

 

    /**

     * @Title: getJavaType

     * @Description: 实出数据库类型向java类型的转换

     * @param

     * @param typeName

     *            oracel的字段类型

     * @param

     * @return java类型

     * @return String 返回类型

     */

    private static String getJavaType(String typeName) {

       if (typeName.equals("DATE")) {

           return "Date";

       } else if (typeName.equals("VARCHAR2") || typeName.equals("CHAR")) {

           return "String";

       } else if (typeName.equals("Number")) {

           return "Float";

       } else {

           return "*";// 其它类型的设置为未知类型

       }

    }

 

    /**

     * @Title: getAs3Type

     * @Description: 实出数据库类型向Flex类型的转换

     * @param

     * @param typeName

     *            oracel的字段类型

     * @param

     * @return Flex类型

     * @return String 返回类型

     */

    private static String getAs3Type(String typeName) {

       if (typeName.equals("DATE")) {

           return "Date";

       } else if (typeName.equals("FLOAT") || typeName.equals("Float")) {

           return "Number";

       } else if (typeName.equals("VARCHAR2") || typeName.equals("CHAR")) {

           return "String";

       } else {

           return "*";// 其它类型的设置为未知类型

       }

    }

 

    /**

     * @Title: getOracelDict

     * @Description: 通过oracel数据字典返回指定表的数据结构并将返回结果存入oraceltab

     * @param

     * @param tablename

     *            表名

     * @return void 返回类型

     */

    private void getOracelDict(String tablename) {

       String sqlst = " select  rownum as id, a.TABLE_NAME,a.COLUMN_NAME,a.DATA_TYPE,b.COMMENTS from "

              + " user_tab_columns a left join user_col_comments b "

              + " on a.TABLE_NAME=b.TABLE_NAME and a.COLUMN_NAME=b.COLUMN_NAME "

              + " where a.TABLE_NAME=:tablename";

       Map<String, Object> map = new HashMap();

       map.put("tablename", tablename);

       oraceltab = (List<OracelDict>) HibernateTool.gets_SQL(OracelDict.class,

              sqlst, map);

    }

 

    /**

     * @Title: getPropertyName

     * @Description: 该方法主要用在生成java类的get和set方法中,目的是实现变量的首字母大写

     * @param

     * @param FieldName

     *            变量名

     * @param

     * @return 设定文件

     * @return String 返回类型

     */

    private String getPropertyName(String FieldName) {

       String lowString = FieldName.toLowerCase();

       String firstString = lowString.substring(0, 1).toUpperCase();

       String string = lowString.substring(1, lowString.length())

              .toLowerCase();

       return firstString + string;

    }

 

    /**

     * @Title: getJava_As3DictField

     * @Description: 1.把oraceltab的字段类型向java类型转变并存入javatab中<br>

     *               2.把oraceltab的字段类型向Flex类型转变并存入as3tab中

     * @param 设定文件

     * @return void 返回类型

     */

    private void getJava_As3DictField() {

       for (OracelDict oracelfiled : oraceltab) {

           OracelDict javaField = new OracelDict();

           javaField

                  .setColumn_name(oracelfiled.getColumn_name().toLowerCase());

           javaField.setData_type(getJavaType(oracelfiled.getData_type()

                  .toUpperCase()));

           javaField.setComments(oracelfiled.getComments());

           javaField.setTable_name(getPropertyName(oracelfiled

                  .getColumn_name()));// 将表名改为首字母大写的属性名

           javatab.add(javaField);

 

           OracelDict as3Field = new OracelDict();

           as3Field.setColumn_name("_"

                  + oracelfiled.getColumn_name().toLowerCase());

           as3Field.setData_type(getAs3Type(oracelfiled.getData_type()

                  .toUpperCase()));

           as3Field.setComments(oracelfiled.getComments());

           as3Field.setTable_name(oracelfiled.getColumn_name().toLowerCase());

           as3tab.add(as3Field);

       }

 

    }

 

    /**

     * @Title: getGenAs3

     * @Description: 根所as3tab中的内容生成flex所需的valueobject类

     * @param

     * @return 设定文件

     * @return String 生成的flex类

     */

    private String getGenAs3() {

       String pro = new String();

       String constructionfield = new String();// 构造字段

       String constructionbody = new String();// 构造字段

       String construction = new String();// 构造函数

       String getField = new String();

       String setField = new String();

       String as3File = new String();

       for (OracelDict filed : as3tab) {

           // as3属性

           pro = pro + TAB + "private var " + filed.getColumn_name() + ":"

                  + filed.getData_type() + ";//" + filed.getComments() + LINE;

           if (!filed.getTable_name().equals("id")) {

              constructionfield = constructionfield + " "

                     + filed.getTable_name() + ":" + filed.getData_type()

                     + ",";

              constructionbody = constructionbody + TAB + "this."

                     + filed.getColumn_name() + "=" + filed.getTable_name()

                     + ";" + LINE;

           }

           getField = getField + "public function get "

                  + filed.getTable_name() + "():" + filed.getData_type()

                  + "{" + LINE + TAB + "return " + filed.getColumn_name()

                  + ";}" + LINE;

           setField = setField + "public function set "

                  + filed.getTable_name() + "(" + filed.getTable_name() + ":"

                  + filed.getData_type() + "):void{" + LINE + TAB + "this."

                  + filed.getColumn_name() + "=" + filed.getTable_name()

                  + ";}" + LINE;

       }

       constructionfield = constructionfield.substring(0, constructionfield

              .length() - 1);// 去除多余的,

       construction = "public function " + as3ClassName + "(){}" + LINE

              + "public function " + as3ClassName + "(" + constructionfield

              + "){" + LINE + constructionbody + "}";

 

       as3File = "package " + as3packageName + "{" + LINE;

       as3File = as3File + TAB + "[Bindable]" + LINE;

       as3File = as3File + TAB + "[RemoteClass(alias=" + "\""

              + javapackageName + javaClassName + "\")]" + LINE;

       as3File = as3File + "public class " + as3ClassName + "{";

       as3File = as3File + LINE + pro + LINE + construction + LINE + getField

              + LINE + setField + "}" + LINE + "}";

       return as3File;

    }

 

    /**

     * @Title: getGenJava

     * @Description: 根所javatab中的内容生java所需的domain类

     * @param

     * @return 设定文件

     * @return String 生成的flex类

     * @throws

     */

    private String getGenJava() {

       String pro = new String();

       String constructionfield = new String();// 构造字段

       String constructionbody = new String();// 构造字段

       String construction = new String();// 构造函数

       String getField = new String();

       String setField = new String();

       String javaFile = new String();

       for (OracelDict filed : javatab) {

           pro = pro + TAB + "private " + filed.getData_type() + " "

                  + filed.getColumn_name() + ";//" + filed.getComments()

                  + LINE;

 

           constructionfield = constructionfield + filed.getData_type() + " "

                  + filed.getColumn_name() + ",";

           constructionbody = constructionbody + TAB + "this."

                  + filed.getColumn_name() + "=" + filed.getColumn_name()

                  + ";" + LINE;

           getField = getField + "public " + filed.getData_type() + " get"

                  + filed.getTable_name() + "(){" + LINE + TAB

                  + "return this." + filed.getColumn_name() + ";}" + LINE;

           setField = setField + "public void set" + filed.getColumn_name()

                  + "(" + filed.getData_type() + " " + filed.getColumn_name()

                  + "){" + LINE + TAB + "this." + filed.getColumn_name()

                  + "=" + filed.getColumn_name() + ";}" + LINE;

       }

 

       javaFile = "package " + javapackageName + ";" + LINE;

       javaFile = javaFile + "import javax.persistence.Entity;" + LINE;

       javaFile = javaFile + "import javax.persistence.Id;" + LINE;

       javaFile = javaFile + "import java.util.Date;" + LINE;

       javaFile = javaFile + "@Entity" + LINE;

       javaFile = javaFile + "public class " + javaClassName + "{" + LINE;

 

       // 构造函数

       constructionfield = constructionfield.substring(0, constructionfield

              .length() - 1);

       construction = "public " + javaClassName + "(" + constructionfield

              + "){" + LINE;

       construction = construction + constructionbody + "}" + LINE;

       construction = construction + "public " + javaClassName + "(){}";

 

       javaFile = javaFile + pro + LINE;

       javaFile = javaFile + construction + LINE;

       javaFile = javaFile + getField + LINE;

       javaFile = javaFile + setField + LINE;

 

       javaFile = javaFile + "}";

       return javaFile;

    }

 

    public static void main(String[] args) {

       String javaClassName = "Zymd";

       String as3ClassName = "Zymd";

       String javapackageName = "domain.zzb";

       String as3packageName = "domain.zzb";

       String javapath = "H:\\workspace_java\\zsglxt\\src\\domain\\zzb";

       String as3path = "H:\\workspace_flex\\zsglxt\\src\\valueobject";

       String tableName = "ZYMD";

       try {

           OracleTableToVO oracleTableToVO = new OracleTableToVO(

                  javaClassName, as3ClassName, javapackageName,

                  as3packageName, javapath, as3path, tableName);

       } catch (IOException e) {

           System.out.println(e.toString());

       }

 

    }

 

}

 //********************************

package domain.sys;

 

import javax.persistence.Entity;

import javax.persistence.GeneratedValue;

import javax.persistence.Id;

 

@Entity

public class OracelDict {

   private String id;

   private String table_name;//表名

   private String column_name;//列名

   private String data_type;//数据类型

   private String comments;//注释

  

 

@Id

@GeneratedValue

public String getId() {

       return id;

}

public void setId(String id) {

       this.id = id;

}

public String getTable_name() {

       return table_name;

}

public void setTable_name(String table_name) {

       this.table_name = table_name;

}

public String getColumn_name() {

       return column_name;

}

public void setColumn_name(String column_name) {

       this.column_name = column_name;

}

public String getData_type() {

       return data_type;

}

public void setData_type(String data_type) {

       this.data_type = data_type;

}

public String getComments() {

       return comments;

}

public void setComments(String comments) {

       this.comments = comments;

}

public OracelDict(String table_name, String column_name, String data_type,

              String comments) {

       super();

       this.table_name = table_name;

       this.column_name = column_name;

       this.data_type = data_type;

       this.comments = comments;

}

public OracelDict(){

      

}

 

}

posted on 2012-05-21 11:13  垦荒牛  阅读(458)  评论(0编辑  收藏  举报