论坛首页 Java企业应用论坛

代码生成器组件部分(页面部分,待下回分解)

浏览 2793 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (11)
作者 正文
   发表时间:2008-10-15  

  既然提到xml,xml只是临时保存数据信息的,通常保存数据都是使用数据库,我想数据库大家肯定不陌生,而数据库中业务表的字段又和页面中的表单直接关联,当然不同的公司使用不同的框架,也就是利用不同的形式将表单和数据库进行关联。这里我就提供一个简单的读取数据表的结构的信息,供大家参考,也可以给我提一提您的宝贵的意见。

  代码如下:

package com.pkusoft.dbtest;

/**
 * 
 * @author HILL
 * @ 本类作为开发辅助作用
 */
public class ColumnInfo {
	private String[] strsAttribus = {"id","type","class","kind","fieldname","datatype" ,"state","maxlength"} ;
	private String id ;
	private String type ;
	private String strClass ;
	private String kind ;
	private String fieldname ;
	private String datatype ;
	private String state ;
	private String maxlength ;
	private String must ;
	private String comment ;
	private String columnName = null ;
	private String columnValue = null ;
	private String columnComment = null ;
	private String columnDatatype = null ;
	private int columnSize  = 0;
	private boolean colmunIsMust = false ;
	public ColumnInfo()
	{}
	

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}


	public boolean isColmunIsMust() {
		return colmunIsMust;
	}


	public void setColmunIsMust(boolean colmunIsMust) {
		this.colmunIsMust = colmunIsMust;
	}


	public String getColumnComment() {
		return columnComment;
	}


	public void setColumnComment(String columnComment) {
		this.columnComment = columnComment;
	}


	public String getColumnDatatype() {
		return columnDatatype;
	}


	public void setColumnDatatype(String columnDatatype) {
		this.columnDatatype = columnDatatype;
	}


	public String getColumnName() {
		return columnName;
	}


	public void setColumnName(String columnName) {
		this.columnName = columnName;
	}


	public int getColumnSize() {
		return columnSize;
	}


	public void setColumnSize(int columnSize) {
		this.columnSize = columnSize;
	}


	public String getColumnValue() {
		return columnValue;
	}


	public void setColumnValue(String columnValue) {
		this.columnValue = columnValue;
	}


	public String getDatatype() {
		return datatype;
	}


	public void setDatatype(String datatype) {
		this.datatype = datatype;
	}


	public String getFieldname() {
		return fieldname;
	}


	public void setFieldname(String fieldname) {
		this.fieldname = fieldname;
	}


	public String getId() {
		return id;
	}


	public void setId(String id) {
		this.id = id;
	}


	public String getKind() {
		return kind;
	}


	public void setKind(String kind) {
		this.kind = kind;
	}


	public String getMaxlength() {
		return maxlength;
	}


	public void setMaxlength(String maxlength) {
		this.maxlength = maxlength;
	}


	public String getState() {
		return state;
	}


	public void setState(String state) {
		this.state = state;
	}


	public String getStrClass() {
		return strClass;
	}


	public void setStrClass(String strClass) {
		this.strClass = strClass;
	}


	public String[] getStrsAttribus() {
		return strsAttribus;
	}


	public void setStrsAttribus(String[] strsAttribus) {
		this.strsAttribus = strsAttribus;
	}


	public String getType() {
		return type;
	}


	public void setType(String type) {
		this.type = type;
	}


	public String getMust() {
		return must;
	}


	public void setMust(String must) {
		this.must = must;
	}


	public String getComment() {
		return comment;
	}


	public void setComment(String comment) {
		this.comment = comment;
	}

}

 本类是基础的字段信息类。

package com.pkusoft.dbtest;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;
import java.util.ArrayList;
import java.sql.*;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import java.sql.Types;
import com.pkusoft.db.DBConnection;

/**
 * 
 * @author HILL
 *
 */
public class TableInfo {
	
	private static String xmlContent = "<?xml version='1.0'?><PKUDOC><DATAINFO></DATAINFO></PKUDOC>" ;
	
	private static String DATAINFO      = "DATAINFO" ;
	
	private static String COMMENT       = "comment" ;
	
	private static String ID            = "id" ;
	private static String TYPE          = "type" ;
	private static String CLASS         = "class" ;
	private static String KIND          = "kind" ;
	private static String FIELDNAME     = "fieldname" ;
	private static String DATATYPE      = "datatype" ;
	private static String STATE         = "state" ;
	private static String MAXLENGTH     = "maxlength" ;
	
	private String[]      strsAttribus  = {TableInfo.ID,TableInfo.TYPE,TableInfo.CLASS,TableInfo.KIND,TableInfo.FIELDNAME,TableInfo.DATATYPE ,TableInfo.STATE,TableInfo.MAXLENGTH} ;
	
	private Hashtable     hDataType     = new Hashtable() ;       // datatype 的字典翻译
	private Hashtable     hKind         = new Hashtable() ;       // kind     的字典翻译
	private Hashtable     hMust         = new Hashtable() ;       // must     的字典翻译	
	
	private String        tableName     = null ;                  // 表的名字
	private String        tableComment  = null ;                  // 表的注释
	private Hashtable     hashtable     = null ;                  // 列的详细信息
	private List          columns       = null ;                  // 列的列表
	
	private String        strXML        = null ;                  // 组织的一个xml
	
	public String getTableName()
	{
		return this.tableName ;
	}
	public String getTableComment()
	{
		return this.tableComment ;
	}
	public Hashtable getColumnInfo()
	{
		return this.hashtable ;
	}
	public List getColumns()
	{
		return this.columns ;
	}
	public String getXML() {
		return strXML;
	}
	
	TableInfo(String strTalbeName){
		try {
			init(strTalbeName) ;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void init(String strTableName)throws Exception{
		strTableName = strTableName.toUpperCase() ;
		this.tableName = strTableName ;
		
		
		hDataType.put(String.valueOf(Types.VARCHAR) ,"0") ;        //VARCHAR2  12
		hDataType.put(String.valueOf(Types.CHAR)    ,"0") ;        //CHAR      1
		hDataType.put(String.valueOf(Types.NUMERIC) ,"1") ;        //NUMBER    2
		hDataType.put(String.valueOf(Types.DATE)    ,"3") ;        //DATE      91
		hDataType.put(String.valueOf(Types.BLOB)    ,"5") ;        //BLOB      2004
		
		hKind.put(String.valueOf(Types.VARCHAR)     ,"text") ;     //VARCHAR2  12
		hKind.put(String.valueOf(Types.CHAR)        ,"text") ;     //CHAR      1
		hKind.put(String.valueOf(Types.NUMERIC)     ,"int") ;      //NUMBER    2
		hKind.put(String.valueOf(Types.DATE)        ,"date") ;     //DATE      91
		hKind.put(String.valueOf(Types.BLOB)        ,"text") ;     //BLOB      2004
		
		hMust.put("0","true") ;
		hMust.put("1","false") ;
		
		DBConnection dbconn = null ;
		String strSql = null ;
		ResultSet rs = null ;
		ResultSetMetaData rsmd = null ;
		try {
			this.columns = new ArrayList() ;
			this.hashtable = new Hashtable() ;
			Hashtable commentHtable = this.getColumnCommonList(strTableName) ;
			strSql = "SELECT * FROM " + strTableName + " WHERE 1=2" ;
			dbconn = new DBConnection() ;
			rs  = dbconn.excuteQuery(strSql) ;
			rsmd = rs.getMetaData() ;
			int colLength = rsmd.getColumnCount() ;
			for(int i=1;i<=colLength;i++)
			{	
				ColumnInfo columnInfo = new ColumnInfo() ;
				String strName = rsmd.getColumnName(i) ;
				String strType = String.valueOf(rsmd.getColumnType(i)) ;
				
				columnInfo.setColumnDatatype(hDataType.get(strType).toString()) ;
				columnInfo.setColumnComment(commentHtable.get(strName).toString()) ;
				String isNullAble = String.valueOf(rsmd.isNullable(i)) ;
				if(isNullAble.equals("0"))
					columnInfo.setColmunIsMust(true) ;
				else
					columnInfo.setColmunIsMust(false) ;
				int colSize = rsmd.getColumnDisplaySize(i) ;
				columnInfo.setColumnSize(colSize) ;
				columnInfo.setColumnName(strName) ;
				Object[] arrObj = {strName.toLowerCase(),"text","Edit",hKind,strTableName +"/" + strName,hDataType,"0",String.valueOf(colSize)} ;
				for(int j=0;j<strsAttribus.length;j++)
				{
					Object o = arrObj[j] ;
					String strTempValue = "" ;
					if(o instanceof String)
						strTempValue = o.toString() ;
					else if(o instanceof Hashtable)
					{
						Hashtable ho = (Hashtable)o ;
						strTempValue =ho.get(strType).toString() ;
					}
					if(strsAttribus[j].equals(TableInfo.ID))
						columnInfo.setId(strName) ;
					else if(strsAttribus[j].equals(TableInfo.TYPE))
						columnInfo.setType(strTempValue) ;
					else if(strsAttribus[j].equals(TableInfo.CLASS))
						columnInfo.setStrClass(strTempValue) ;
					else if(strsAttribus[j].equals(TableInfo.KIND))
						columnInfo.setKind(strTempValue) ;
					else if(strsAttribus[j].equals(TableInfo.FIELDNAME))
						columnInfo.setFieldname(strTempValue) ;
					else if(strsAttribus[j].equals(TableInfo.DATATYPE))
						columnInfo.setDatatype(strTempValue) ;
					else if(strsAttribus[j].equals(TableInfo.STATE))
						columnInfo.setState(strTempValue) ;
					else if(strsAttribus[j].equals(TableInfo.MAXLENGTH))
						columnInfo.setMaxlength(strTempValue) ;
				}
				columnInfo.setMust(hMust.get(isNullAble).toString()) ;
				columnInfo.setComment(commentHtable.get(strName).toString()) ;
				this.hashtable.put(strName, columnInfo) ;
				this.columns.add(strName) ;
				this.strXML = this.organizationXML() ;
			}
			this.tableComment = commentHtable.get(strTableName).toString() ;
		} catch (Exception e) {
			e.printStackTrace() ;
		}
		finally
		{
			if(dbconn !=null)
				dbconn.freeConnection() ;
			if(rs!=null)
				rs.close() ;
		}
	}
	
	private String organizationXML()throws DocumentException{
		Document tempDoc = DocumentHelper.parseText(TableInfo.xmlContent) ;
		Element tabEle = DocumentHelper.createElement(this.getTableName()) ;
		
		Hashtable columnHashtable = this.getColumnInfo() ;
		Enumeration enu = columnHashtable.elements();
		while(enu.hasMoreElements())
		{
			ColumnInfo colInfo = (ColumnInfo)enu.nextElement() ; 
			Element colEle = DocumentHelper.createElement(colInfo.getColumnName()) ;
			
			colEle.addAttribute(TableInfo.COMMENT, colInfo.getColumnComment()) ;
			
			colEle.addAttribute(TableInfo.ID        , colInfo.getId().toLowerCase()) ;
			colEle.addAttribute(TableInfo.TYPE      , colInfo.getType()) ;
			colEle.addAttribute(TableInfo.CLASS     , colInfo.getStrClass()) ;
			colEle.addAttribute(TableInfo.KIND      , colInfo.getKind()) ;
			colEle.addAttribute(TableInfo.FIELDNAME , colInfo.getFieldname()) ;
			colEle.addAttribute(TableInfo.DATATYPE  , colInfo.getColumnDatatype()) ;
			colEle.addAttribute(TableInfo.STATE     , colInfo.getState()) ;
			colEle.addAttribute(TableInfo.MAXLENGTH , String.valueOf(colInfo.getColumnSize())) ;
			tabEle.add(colEle) ;
		}
		tempDoc.getRootElement().element(TableInfo.DATAINFO).add(tabEle) ;
		tempDoc.getRootElement().element(TableInfo.DATAINFO).addAttribute(TableInfo.COMMENT, this.getTableComment()) ;
		return tempDoc.asXML() ;
	}
	
	public java.util.Hashtable getColumnCommonList(String strTableName)throws Exception{
		Hashtable commentHashtable = new Hashtable() ;
		strTableName = strTableName.toUpperCase() ;
		String strSql = "SELECT * FROM USER_COL_COMMENTS WHERE TABLE_NAME='"+strTableName+"'" ;
		String strSql1 = "SELECT COMMENTS FROM USER_TAB_COMMENTS WHERE TABLE_NAME='"+strTableName+"'" ;
		DBConnection dbc = null ;
		ResultSet rs = null ;
		try{
			dbc = new DBConnection() ;
			rs = dbc.excuteQuery(strSql) ;
			while(rs.next()){
				String strComment = "" ;
				if(rs.getObject("COMMENTS") == null)
					strComment = "" ;
				else
					strComment = rs.getObject("COMMENTS").toString() ;
				commentHashtable.put(rs.getObject("COLUMN_NAME"), strComment) ;
			}
			dbc.freeConnection() ;
			rs.close() ;
			dbc = new DBConnection() ;
			rs = dbc.excuteQuery(strSql1) ;
			if(rs.next())
				commentHashtable.put(strTableName, rs.getString("COMMENTS")) ;
		}
		catch(Exception e){
			e.printStackTrace() ;
		}
		finally{
			if(dbc!=null)
				dbc.freeConnection() ;
			if(rs != null)
				rs.close() ;
		}
		return commentHashtable ;
	}	

}

 这是一个表的管理类,管理所有的字段,已经对应生成的xml的信息。

 

 

package com.pkusoft.dbtest;

import java.util.Hashtable;

public class TableFactory {
	
	public static TableFactory tableFactory = null ;
	
	private Hashtable tableList = null ;
	
	public static synchronized TableFactory getInstance(){
		if(tableFactory == null){}
			tableFactory = new TableFactory() ;
		return tableFactory ;
	}
	
	private TableFactory(){
		tableList = new Hashtable() ;
	}
	
	public TableInfo getTableInfo(String strTableName){
		String tempTableName = strTableName.toUpperCase() ;
		TableInfo tempTableInfo = null ;
		if(this.tableList.get(tempTableName) != null)
			tempTableInfo = (TableInfo)this.tableList.get(tempTableName) ;
		else{
			tempTableInfo = new TableInfo(tempTableName) ;
			this.tableList.put(tempTableName, tempTableInfo) ;
		}
			
		return tempTableInfo ;
	}
	
	public static void main(String[] args) {
		TableFactory tabFactory = TableFactory.getInstance() ;
		
		TableInfo tabeInfo = tabFactory.getTableInfo("caseinfo") ;
		System.out.println(tabeInfo.getXML());
	}

}

 

  这是一个工厂类,保存表的信息,读取一个表只需要一次访问数据库即可,第一次访问了就将数据信息保存到缓存中,第二次,直接从缓存中读取,这个的目的就是利用内存来换取速度。

 

      这个没有提供数据连接的类,如果想测试数据,可以自己写连接测试,以上近作参考。如有疑问,请给我留言。

 

这里是我生成的xml结构供大家参考——

 

<?xml version="1.0" encoding="UTF-8"?>
<PKUSOFT>
	<DATAINFO>
		<CASEINFO>
			<CTPANONYMOUS comment="匿名报警,对应DIC_YESORNO" id="CTPANONYMOUS" type="text" class="Edit" kind="int" fieldname="CASEINFO/CTPANONYMOUS" datatype="1" state="0" maxlength="22" />
			<REPORTMODE comment="报案方式,对应DIC_REPORT_TYPE" id="REPORTMODE" type="text" class="Edit" kind="text" fieldname="CASEINFO/REPORTMODE" datatype="0" state="0" maxlength="2" />
			<REPORTERGENDER comment="报案人性别,对应DIC_GENDER" id="REPORTERGENDER" type="text" class="Edit" kind="int" fieldname="CASEINFO/REPORTERGENDER" datatype="1" state="0" maxlength="22" />
			<LEGAL2 comment="法制办案人二" id="LEGAL2" type="text" class="Edit" kind="text" fieldname="CASEINFO/LEGAL2" datatype="0" state="0" maxlength="10" />
			<CSTATION comment="办案部门市局,对应MANAGEUNIT" id="CSTATION" type="text" class="Edit" kind="text" fieldname="CASEINFO/CSTATION" datatype="0" state="0" maxlength="16" />
			<LEGAL1 comment="法制办案人一" id="LEGAL1" type="text" class="Edit" kind="text" fieldname="CASEINFO/LEGAL1" datatype="0" state="0" maxlength="10" />
			<REMOVETIME comment="移交时间" id="REMOVETIME" type="text" class="Edit" kind="date" fieldname="CASEINFO/REMOVETIME" datatype="3" state="0" maxlength="7" />
			<CTP110 comment="110报警,对应DIC_YESORNO" id="CTP110" type="text" class="Edit" kind="int" fieldname="CASEINFO/CTP110" datatype="1" state="0" maxlength="22" />
			<PSTATION comment="办案部门省厅,对应MANAGEUNIT" id="PSTATION" type="text" class="Edit" kind="text" fieldname="CASEINFO/PSTATION" datatype="0" state="0" maxlength="16" />
			<LEGALNAME2 comment="法制办案人二姓名" id="LEGALNAME2" type="text" class="Edit" kind="text" fieldname="CASEINFO/LEGALNAME2" datatype="0" state="0" maxlength="60" />
			<LEGALNAME1 comment="法制办案人一姓名" id="LEGALNAME1" type="text" class="Edit" kind="text" fieldname="CASEINFO/LEGALNAME1" datatype="0" state="0" maxlength="60" />
			<CASEID comment="案件编号" id="CASEID" type="text" class="Edit" kind="text" fieldname="CASEINFO/CASEID" datatype="0" state="0" maxlength="50" />
			<REQSTATE comment="待办状态,对应DIC_CASESTATE_REQ" id="REQSTATE" type="text" class="Edit" kind="int" fieldname="CASEINFO/REQSTATE" datatype="1" state="0" maxlength="22" />
			<REPORTRADDR2 comment="报案人住址_详址" id="REPORTRADDR2" type="text" class="Edit" kind="text" fieldname="CASEINFO/REPORTRADDR2" datatype="0" state="0" maxlength="120" />
			<REPORTRADDR1 comment="报案人住址_门牌号" id="REPORTRADDR1" type="text" class="Edit" kind="text" fieldname="CASEINFO/REPORTRADDR1" datatype="0" state="0" maxlength="120" />
			<LEGALDEPT comment="法制办案部门" id="LEGALDEPT" type="text" class="Edit" kind="text" fieldname="CASEINFO/LEGALDEPT" datatype="0" state="0" maxlength="16" />
			<LEGALDEPTNAME comment="法制办案部门名称" id="LEGALDEPTNAME" type="text" class="Edit" kind="text" fieldname="CASEINFO/LEGALDEPTNAME" datatype="0" state="0" maxlength="90" />
			<REPORTRSECTIONID comment="报案人住址_行政区划" id="REPORTRSECTIONID" type="text" class="Edit" kind="text" fieldname="CASEINFO/REPORTRSECTIONID" datatype="0" state="0" maxlength="6" />
			<CASERESOURCEDES comment="案件来源描述" id="CASERESOURCEDES" type="text" class="Edit" kind="text" fieldname="CASEINFO/CASERESOURCEDES" datatype="0" state="0" maxlength="90" />
		</CASEINFO>
	</DATAINFO>
</PKUSOFT>

 

   发表时间:2008-10-16  
代码太长,没勇气看完~~~
0 请登录后投票
   发表时间:2008-10-16  
应该知道我是谁了吧???
0 请登录后投票
   发表时间:2008-10-16  
师兄是考研我们看代码的能力吗?一句注释都没有,还有看懂了80%,就是组织xml那个方法不是很清楚,下了个dom4j api,在看看吧,自己试一试!期待你在页面部分哦,(*^__^*) 嘻嘻……
0 请登录后投票
   发表时间:2008-10-16  
lucky520 写道

师兄是考研我们看代码的能力吗?一句注释都没有,还有看懂了80%,就是组织xml那个方法不是很清楚,下了个dom4j api,在看看吧,自己试一试!期待你在页面部分哦,(*^__^*) 嘻嘻……


页面部分我写的有点复杂了,n多,我还得整理一下。
是啊,从头到尾,我很少写注释哦,昨天晚上赶得急,也就没有写,我下次注意。
0 请登录后投票
   发表时间:2008-10-16  
yuxingege34 写道

应该知道我是谁了吧???


你是谁啊,怎么是谁给你取得名字,叫“yuxinggegege”很怪呢 ?
你就是班长呗!
0 请登录后投票
   发表时间:2008-10-16  
  本组件是在保证你对数据库连接没有问题的情况下可以实现,同时组织xml所使用到的jar包是dom4j,本连接的数据库是oracle。
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics