论坛首页 Java企业应用论坛

简易Etmvc-Ar自动生成工具

浏览 2471 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-11-14  

忙里偷闲挪出了点时间看看能不能弄个Etmvc自动生成代码工具,首先最烦的是AR部分,表也多,字段也多,码起来完全靠体力.

首先是要遍历数据库中的表咯,当然要连接数据库,我们就不必再创造数据库连接了,Etmvc-AR中已经有咯哦,

仔细看看代码可以用上 ConnectionHolder 这个类,在初始化时随便给个类就行了.例如

ConnectionHolder connectionHolder = new ConnectionHolder(ApplicationsController.class);

 可以这样

Connection conn = connectionHolder.getConnection();

 得到数据库连接对象,至此获取数据库表列表代码如下

	public JspView index() throws DataAccessException, SQLException{                
		JspView view = new JspView();
		ConnectionHolder connectionHolder = new ConnectionHolder(MyUser.class);
		Connection conn = connectionHolder.getConnection();
		DatabaseMetaData dbmd = conn.getMetaData();
		String[] types = { "TABLE" };
		ResultSet resultSet = dbmd.getTables(null, null, "%", types);
		view.setAttribute("tablelist", resultSet);
		return view;			
	}

 视图文件如下

<%@ page
	import="java.sql.ResultSet"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<form action="CreateAR" method="post" name="form1" >
<table>
<tr>
	<td>数据表</td>
	<td>
	<select id="tableName" name="tableName">
	<%
	ResultSet resultSet = (ResultSet)request.getAttribute("tablelist") ;
		while (resultSet.next()) {
	      String tableName = resultSet.getString(3);	    
	%>	
		<option value="<%=tableName %>"><%=tableName%></option>
		<%}%>
	</select>
	</td>
	<td> <input type="submit" value="确定" > </td>
</tr>
</table>
</form>
</body>
</html>

 接下来就是要遍历指定表的字段了,并生成相应的模板

遍历字段的方法

	public JspView CreateAR() throws SQLException, DataAccessException{	
		String table = request.getParameter("tableName");
        String sql = "select * from "+ table;        
        ResultSetMetaData rsmd = null;       
		ConnectionHolder connectionHolder = new ConnectionHolder(ApplicationsController.class);
        try{
            Connection conn = connectionHolder.getConnection();
            Statement stmt=conn.createStatement();
            ResultSet rs=stmt.executeQuery(sql);            
            rsmd = rs.getMetaData();               
        }
        finally{
            connectionHolder.close();            
        }	
        JspView view = new JspView();
        view.setAttribute("tables", table);
        view.setAttribute("tableinfo", rsmd);
        return view;
	}

 用jsp来作为模板,根据自身要求修改下.(还需要调整的)

<%@ page import="java.sql.ResultSetMetaData" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<%
		ResultSetMetaData rsmd = (ResultSetMetaData)request.getAttribute("tableinfo") ;	 
		String table = (String)request.getParameter("tableName") ;	
	%>	
<pre>

/* ***********************************************
 * author :  Anyhome
 * email  :  ayhome@gmail.com 
 * function: 
 * @date 2009-10-30
 * history:  created by Anyhome
 * ***********************************************/
 
package org.anyhome.models;

import com.et.ar.ActiveRecordBase;
import com.et.ar.annotations.Column;
import com.et.ar.annotations.Id;
import com.et.ar.annotations.Table;

@Table(name="<%=table%>")
public class <%=table%> extends ActiveRecordBase {
	<%
        for(int i=1;i<=rsmd.getColumnCount();i++)
        {
        	String s = rsmd.getColumnTypeName(i);
        	String sid = "Column";
        	if (s.trim().equalsIgnoreCase("int"))  
        		s = "Integer";
        	else if(s.trim().equalsIgnoreCase("int identity")) 
        	{
        		s = "Integer"; 
        		sid = "Id";
        	}
        	else
        		s = "String";
        	String outs = "@"+sid+" private " + s + " "
			+rsmd.getColumnName(i);
        	%>
        <%=outs %>
        	<%        	
        }
	 %>
	 
	<%
        for(int i=1;i<=rsmd.getColumnCount();i++)
        {
        	String s = rsmd.getColumnTypeName(i);
        	String sid = "Column";
        	if (s.trim().equalsIgnoreCase("int"))  
        		s = "Integer";
        	else if(s.trim().equalsIgnoreCase("int identity")) 
        	{
        		s = "Integer"; 
        		sid = "Id";
        	}
        	else
        		s = "String";
        	%>
	public void set<%=rsmd.getColumnName(i)%>(<%=s %> <%=rsmd.getColumnName(i).replaceFirst(String.valueOf(rsmd.getColumnName(i).charAt(0)), String.valueOf(rsmd.getColumnName(i).charAt(0)).toLowerCase()) %>) {
		<%=rsmd.getColumnName(i)%> = <%=rsmd.getColumnName(i).replaceFirst(String.valueOf(rsmd.getColumnName(i).charAt(0)), String.valueOf(rsmd.getColumnName(i).charAt(0)).toLowerCase()) %>;
	}        	
	public <%=s %> get<%=rsmd.getColumnName(i)%>() {
		return <%=rsmd.getColumnName(i)%>;
	}        	
        	<%
        }
	 %>	 
}
</pre>
</body>
</html>
 呵呵  很简单的吧 不过目前我只应用于我的项目,所以只知道mssql是通过了
论坛首页 Java企业应用版

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