自己利用spring
開發思路
其實就是將數據庫中的表取出來
我這裡只舉例說明如何生成對應的model文件
首先定義一個對象SqlColumnData包含兩個屬性columnName(列名稱)
private String columnName;
private String columnType;
public String getColumnName()
{
return columnName;
}
public void setColumnName(String columnName)
{
lumnName = columnName;
}
public String getColumnType()
{
return columnType;
}
public void setColumnType(String columnType)
{
lumnType = columnType;
}
下面三個方法作用如下
//用來獲取指定表的所有列名及類型 public List getColumnDatas(String tableName)//將列名生成對應的field 和 methodpublic String getBeanField(String tableName) throws SQLException//將數據庫類型轉換成對應的JAVA類型publicString getType(String type)
* 獲取指定表的所有列名
*
* @param tableName
* @return
* @throws SQLException
*/
public List getColumnDatas(String tableName)
throws SQLException
{
String sqlColumns =
+ tableName +
Connection conn = null;
PreparedStatement pst = null;
ResultSet rs = null;
List columnList = new ArrayList()
try
{
conn = sqlDialect
pst = conn
rs = pst
while (rs
{
String name = rs
String type = rs
type = this
SqlColumnData cd = new SqlColumnData()
cd
cd
columnList
}
}
catch ( Exception e )
{
e
}
finally
{
try
{
if (conn != null) conn
if (pst != null) pst
if (rs != null) rs
}
catch ( SQLException e )
{
e
}
}
return columnList;
}
/**
* 將列名生成對應的field 和 method
*
* @param tableName
* @return
* @throws SQLException
*/
public String getBeanField(String tableName) throws SQLException
{
List dataList = getColumnDatas(tableName)
StringBuffer str = new StringBuffer()
StringBuffer getset = new StringBuffer()
for (SqlColumnData d : dataList)
{
String name = d
String type = d
String maxChar = name
str
name)
String method = maxChar + name
getset
getset
getset
getset
}
argv = str
method = getset
return argv + method;
}
private String argv;
private String method;
/**
* 將數據庫類型轉換成對應的JAVA類型
*
* @param type
* @return
*/
public String getType(String type)
{
type = type
if (
||
{
return
}
else if (
{
return
}
else if (
{
return
}
else if (
||
{
return
}
else if (
{
return
}
else if (
{
return
}
else if (
{
return
}
return null;
}
/**
* 將表名轉成class名稱
*
* @param tableName
* @return
*/
public String getTableNameToClassName(String tableName)
{
String[] splits = tableName
if (splits
{
StringBuffer className = new StringBuffer()
for (String split : splits)
{
String tempTableName = split
+ split
className
}
return className
}
else
{
String className = splits[
+ splits[
return className;
}
}
SQL方面就准備的差不多了
我這裡使用的freemarker
${feilds?default(
用freemarker通過模版創建文件
* 創建靜態文件
*
* @param templateFileName
*
模板文件名
* @param propMap
*
用於處理模板的屬性object映射
* @param htmlFilePath
*
要生成的靜態文件的路徑
* @param htmlFileName
*
要生成的文件名
* @param templateFilePath
*
模版路徑
*/
@SuppressWarnings(
{
public static void createHtmlFile(String templateFileName
String htmlFilePath
{
try
{
Template t = getFreemarkerCFG(templateFilePath)
templateFileName)
//createDirs(htmlFilePath)
File file = null;
if(StringTools
else file = new File(htmlFilePath +
if(!file
else file
Writer out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(file)
t
out
out
(
}
catch ( IOException e )
{
e
}
catch ( TemplateException e )
{
e
}
}
現在該准備的都准備好了
* 生成JAVAMODULE文件
* @param tableName
*/
public static void createJavaModuleFile(String tableName)
{
String className = sqlutil
// 生成到指定的目錄下
String modelPath =
Map context = new HashMap()
context
context
context
/****************************** 生成bean字段 *********************************/
try
{
context
(
}
catch ( Exception e )
{
e
}
//
CreateHtml
}
大致代碼就是這麼多
因為我項目中對SPRING的管理都是用注解完成
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26400.html