由于长期写代码, 所以在很多工具之前, 就写了自己的代码生成器.
前面的文章讲了生成 java bean 这个更直接 一把全部生成5个文件
比如 INTERFACE_ID_MAPPING 表生成:
InterfaceIdMapping.java 文件
IInterfaceIdMappingService.java 服务层接口
InterfaceIdMappingServicePojoImpl 服务层接口实现
IInterfaceIdMappingDao.java 层接口
InterfaceIdMappingDaoJdbcImpl 层接口实现
有些人问为什么要生成这么多层? Service层主要是用来发布微服务,与dao层隔离, dao层有些内容不方便公开.
所以 Service层与 dao层是有必要的 (现在很多开源工具也能做到这点, 但是有些事件还是自己做行靠谱)
先来看工具代码
TableToSerivceByArchUtils.java:
package com.test;
import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.sql.DataSource;
import org.springframework.jdbc.datasource.DataSourceUtils;
import com.test.ServiceLocator;
/**
* 根据数据库表生成[档案服务代码].
* 1表转换成Bean, 2生成Serivce层接口,3生成Serivce层实现, 4生成Dao层接口, 5生成Dao层实现.
* 说明:可以按表结构生成原来的大写字段bean, 也可以转成按java规则要求的bean.
* 是否需要转换,设置isConvertColName参数.
* @author guishuanglin 2013-11-26
* update 2015-06-24
*/
public class TableToSerivceByArchUtils {
//BYTE,SHORT对象用INTEGE,不再区别处理.
//FLOAT,DOUBLE对象用BIGDECIMAL,不再区别处理.
private final int jINTEGER = 1;
private final int jLONG = 2;
private final int jDATE = 3;
private final int jSTRING = 4;
private final int jBIGDECIMAL = 5;
private final int jOBJECT = 6;
private final String sINTEGER ="Integer";
private final String sLONG = "Long";
private final String sDATE = "Date";
private final String sSTRING = "String";
private final String sBIGDECIMAL = "BigDecimal";
private final String sOBJECT = "Object";
//资料
private int dataBaseType =-1;
private String tablename = "";
private String beanName = "";
private int colCount;//列数
private String[] benFieldName;
private String[] benClassName;
private int[] benClassType;
private String[] colnames;
private String[] colTypes;//列类型名字.
private int[] colSizes; // 列字符大小
private int[] colScale; // 列小数精度
//引用包
private boolean importUtil = false; // 导入包java.util.*
private boolean importSql = false; // 包java.sql.*
private boolean importMath = false; // 包java.math.*
//控制变量
private boolean isConvertColName =true;//是否把列名转成java规范的名字.
private boolean onlyCreateBean =true; //只生成Bean.
private boolean isCreateBatchMethod =false;//是否创建批处理方法.
//类附加注释
private SimpleDateFormat DateFormat = new SimpleDateFormat("yyyy-MM-dd");
private String strDate=null;
private String author="table tool";
/** 入口 */
public void execute(String dbTableName) {
if(dbTableName ==null) return;
if(strDate ==null)
strDate = DateFormat.format(new Date());
tablename = dbTableName;
beanName = this.firstUpperCase(this.toJavaName(dbTableName.toLowerCase()));
boolean b = processTableMetaData(dbTableName);
if(b){
//初始 bean字段数组.
benFieldName = new String[colCount];
benClassName = new String[colCount];
benClassType = new int[colCount];
//处理每列对应的bean属性与java类型
this.processClassnames();
//生成实体 bean
b =tableToEntity(beanName);
if(b && !onlyCreateBean){
this.builderServiceCode();
this.builderDaoCode();
}
}
}
/** 处理数表媒体数据, spring数据源名称: dataSource0,dataSource1,dataSource3,dataSource4,dataSource5. */
private boolean processTableMetaData(String tName) {
boolean b =false;
DataSource ds = (DataSource) ServiceLocator.getService("dataSource0");
Connection conn = DataSourceUtils.getConnection(ds); // 得到数据库连接
String strsql = "SELECT * FROM " + tablename;//+" WHERE ROWNUM=1";
try {
System.out.println("================ "+tName+" ================");
System.out.println(strsql);
this.dataBaseType =this.getDbType(conn);
PreparedStatement pstmt = conn.prepareStatement(strsql);
pstmt.executeQuery();
ResultSetMetaData rsmd = pstmt.getMetaData();
int size = rsmd.getColumnCount(); // 共有多少列
colCount = size;
colnames = new String[size];
colTypes = new String[size];
colSizes = new int[size];
colScale = new int[size];
for (int i = 0; i < rsmd.getColumnCount(); i++) {
colnames[i] = rsmd.getColumnName(i + 1).toLowerCase();
colTypes[i] = rsmd.getColumnTypeName(i + 1).toLowerCase();
colSizes[i] = rsmd.getPrecision(i + 1);
colScale[i] = rsmd.getScale(i + 1);
System.out.println(i+",DBType: "+rsmd.getColumnTypeName(i + 1)+", javaType: "+rsmd.getColumnClassName(i + 1));
}
b =true;
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DataSourceUtils.releaseConnection(conn, ds);
}
return b;
}
/**
* 获取主流数据库类型.
* -1-未知,0-ORACLE,1-SYBASE,2-SQLSERVER,3-DB2,4-MYSQL
* @throws SQLException
*/
private int getDbType(Connection conn) throws SQLException{
if(conn==null) return -1;
int dbType =-1;
DatabaseMetaData dbMeta =conn.getMetaData();
String dbProductName =dbMeta.getDatabaseProductName();
String url = dbMeta.getURL().toLowerCase();
if (url != null){
if(url.indexOf("oracle") >=0 ){
dbType = 0;
}else if(url.indexOf("sybase") >=0){
dbType = 1;
}else if(url.indexOf("sqlserver") >=0 ){
dbType = 2;
}else if(url.indexOf("ibm") >=0 ){
dbType = 3;
}else if(url.indexOf("mysql") >=0 ){
dbType = 4;
}else{
System.out.println("警告: [不确定的数据库类型]: "+url+","+dbProductName);
}
}else{
System.out.println("警告: [不确定的数据库类型]: "+url+","+dbProductName);
}
dbMeta =null;
return dbType;
}
//================================生成实体部分======================================
/**
* 根据文件名,代码内容生成java代码;
*/
private boolean fileWriter(String name, String javaCodeContent) {
boolean b =false;
try {//编码根据项目本身要求的字符编码修改.
FileOutputStream out = new FileOutputStream(name + ".java");
byte[] bytes = new byte[20480];//20k
byte[] inbytes = javaCodeContent.getBytes("utf-8");
ByteArrayInputStream in = new ByteArrayInputStream(inbytes);
int c;
while ((c = in.read(bytes)) != -1) {
out.write(bytes, 0, c);
}
in.close();
in = null;
bytes =null;
inbytes =null;
b =true;
System.out.println("生成================ "+name+" ================完成");
} catch (IOException e) {
e.printStackTrace();
System.out.println("生成================ "+name+" ================异常");
} finally {
javaCodeContent =null;
}
return b;
}
/**
* 生成实体类文件
* @param args
*/
private boolean tableToEntity(String tName) {
boolean b =false;
String content = buildEntityCode();
b=fileWriter(tName, content);
return b;
}
/**
* 1, 处理列名,把空格下划线'_'去掉,同时把下划线后的首字母大写
* 要是整个列在3个字符及以内,则去掉'_'后,不把"_"后首字母大写.
* 同时把数据库列名,列类型写到注释中以便查看,
* 2, 处理bean字段的java类型
* -1-未知,0-ORACLE,1-SYBASE,2-SQLSERVER,3-DB2,4-MYSQL
* 3, java类型对应的数字常量.
*/
private void processClassnames() {
for (int i = 0; i < colnames.length; i++) {
//1
if(this.isConvertColName){
benFieldName[i] = this.toJavaName(colnames[i]);
}else{
benFieldName[i] = colnames[i].toUpperCase();
}
//2
if(this.dataBaseType ==0){
benClassName[i] = this.oracleTypeToJavaType(colTypes[i], colSizes[i], colScale[i]);
}else if(this.dataBaseType ==1){
benClassName[i] = this.sybaseTypeToJavaType(colTypes[i], colSizes[i], colScale[i]);
}else if(this.dataBaseType ==2){//暂用oralce类型
benClassName[i] = this.oracleTypeToJavaType(colTypes[i], colSizes[i], colScale[i]);
}else if(this.dataBaseType ==3){//暂用oralce类型
benClassName[i] = this.oracleTypeToJavaType(colTypes[i], colSizes[i], colScale[i]);
}else if(this.dataBaseType ==4){//暂用oralce类型
benClassName[i] = this.oracleTypeToJavaType(colTypes[i], colSizes[i], colScale[i]);
}else{//暂用oralce类型
benClassName[i] = this.oracleTypeToJavaType(colTypes[i], colSizes[i], colScale[i]);
}
//3 转换类型对应的常量值,方便以后判断处理.
if(benClassName[i].equals(this.sINTEGER)){
benClassType[i]=this.jINTEGER;
}else if(benClassName[i].equals(this.sLONG)){
benClassType[i]=this.jLONG;
}else if(benClassName[i].equals(this.sBIGDECIMAL)){
//bean是否用到Bigdecimal类型的包
importMath = true;
benClassType[i]=this.jBIGDECIMAL;
}else if(benClassName[i].equals(this.sSTRING)){
benClassType[i]=this.jSTRING;
}else if(benClassName[i].equals(this.sDATE)){
//bean是否用到Date类型的包
importUtil = true;
benClassType[i]=this.jDATE;
}else if(benClassName[i].equals(this.sOBJECT)){
benClassType[i]=this.jOBJECT;
}else{
benClassType[i]=this.jOBJECT;
}
}
}
/**
* 解析处理(生成实体类主体代码)
*/
private String buildEntityCode() {
StringBuffer sb = new StringBuffer();
sb.append("\r\nimport java.io.Serializable;\r\n");
if (importUtil) {
sb.append("import java.util.Date;\r\n");
}
if (importSql) {
sb.append("import java.sql.*;\r\n\r\n");
}
if(importMath){
sb.append("import java.math.*;\r\n\r\n");
}
//表注释
processNote(sb);
sb.append("public class " + this.beanName + " implements Serializable {\r\n");
processAllAttrs(sb);
processAllMethod(sb);
sb.append("}\r\n");
System.out.println(sb.toString());
return sb.toString();
}
/**
* 把数据库列名,列类型,长度,精度写到注释中以便查看.
* (目前字段文本注释暂不能处理)
* @param sb
*/
private void processNote(StringBuffer sb) {
//可增加类说明
sb.append("\r\n/** \r\n");
sb.append(" * "+tablename+":\r\n");
String colsiz="";
String colsca="";
for (int i = 0; i < colnames.length; i++) {
colsiz = colSizes[i]<=0? "" : (colScale[i]<=0? "("+colSizes[i]+")" : "("+colSizes[i]+","+colScale[i]+")");
sb.append("\t" + colnames[i].toUpperCase() +" "+colTypes[i].toUpperCase()+ colsiz+"\r\n");
}
//增加日期
sb.append(" * @author "+author+" "+this.strDate+"\r\n");
sb.append(" */\r\n");
}
/**
* 生成所有的方法
* @param sb
*/
private void processAllMethod(StringBuffer sb) {
for (int i = 0; i < colnames.length; i++) {
sb.append("\tpublic void set" + firstUpperCase(benFieldName[i]) + "("
+ benClassName[i] + " " + benFieldName[i]
+ "){\r\n");
sb.append("\t\tthis." + benFieldName[i] + "=" + benFieldName[i] + ";\r\n");
sb.append("\t}\r\n");
//
sb.append("\tpublic " + benClassName[i] + " get"
+ firstUpperCase(benFieldName[i]) + "(){\r\n");
sb.append("\t\treturn " + benFieldName[i] + ";\r\n");
sb.append("\t}\r\n");
}
}
/**
* 解析输出属性
*
* @return
*/
private void processAllAttrs(StringBuffer sb) {
sb.append("\tprivate static final long serialVersionUID = 1L;\r\n");
for (int i = 0; i < colnames.length; i++) {
sb.append("\tprivate " + benClassName[i] + " "
+ benFieldName[i] + ";\r\n");
}
sb.append("\r\n");
}
/**
* 把输入字符串的首字母改成大写,用作Bean的类名,方法名等.
*/
private String firstUpperCase(String name) {
char[] ch = name.toCharArray();
if (ch[0] >= 'a' && ch[0] <= 'z') {
ch[0] = (char) (ch[0] - 32);
}
return new String(ch);
}
/**
* 数据库列名转换成java规范的名字(当然也可以不转).
* 处理列名,把空格下划线'_'去掉,同时把下划线后的首字母大写
* 要是整个列在3个字符及以内,则去掉'_'后,不把"_"后首字母大写.
*/
private String toJavaName(String name) {
char[] ch = name.toCharArray();
char c ='a';
if(ch.length>3){
for(int j=0;j = 'a' && ch[j+1] <= 'z') {
ch[j+1]=(char) (ch[j+1]-32);
}
}
}
}
String str = new String(ch);
str = str.replaceAll("_", "");
return str;
}
/**
* Sybase
*/
private String sybaseTypeToJavaType(String sqlType, int size, int scale) {
if (sqlType.equals("int")
|| sqlType.equals("bit")
|| sqlType.equals("tinyint")
|| sqlType.equals("smallint")
) {
return this.sINTEGER;
} else if (sqlType.equals("bigint")) {
return this.sLONG;
} else if (sqlType.equals("float")
|| sqlType.equals("float precision")
|| sqlType.equals("double")
|| sqlType.equals("double precision")
|| sqlType.equals("money")
|| sqlType.equals("smallmoney")) {
return this.sBIGDECIMAL;
}else if (sqlType.equals("number")
||sqlType.equals("decimal")
|| sqlType.equals("numeric")
|| sqlType.equals("real")) {
return scale==0? (size<10? this.sINTEGER : this.sLONG) : this.sBIGDECIMAL;
}else if (sqlType.equals("varchar")
|| sqlType.equals("varchar2")
|| sqlType.equals("char")
|| sqlType.equals("nvarchar")
|| sqlType.equals("nchar")) {
return this.sSTRING;
} else if (sqlType.equals("datetime")
|| sqlType.equals("date")) {
return this.sDATE;
}else {
return this.sOBJECT;
}
}
/**
* Oracle
*/
private String oracleTypeToJavaType(String sqlType, int size, int scale) {
if (sqlType.equals("integer")) {
return this.sINTEGER;
} else if (sqlType.equals("long")) {
return this.sLONG;
} else if (sqlType.equals("float")
|| sqlType.equals("float precision")
|| sqlType.equals("double")
|| sqlType.equals("double precision")) {
return this.sBIGDECIMAL;
}else if (sqlType.equals("number")
||sqlType.equals("decimal")
|| sqlType.equals("numeric")
|| sqlType.equals("real")) {
return scale==0? (size<10? this.sINTEGER : this.sLONG) : this.sBIGDECIMAL;
}else if (sqlType.equals("varchar")
|| sqlType.equals("varchar2")
|| sqlType.equals("char")
|| sqlType.equals("nvarchar")
|| sqlType.equals("nchar")) {
return this.sSTRING;
} else if (sqlType.equals("datetime")
|| sqlType.equals("date")
|| sqlType.startsWith("timestamp")) {
return this.sDATE;
}else {
return this.sOBJECT;
}
}
//================================生成Serivce部分======================================
/**
* 解析处理(生成Service接口代码)
*/
private void builderServiceCode() {
String faceName = "I"+this.beanName+"Service";
String implName = this.beanName+"ServicePojoImpl";
String content = builderServiceInterface(faceName, implName);
this.fileWriter(faceName, content);
//
content = builderServicePojoImpl(faceName, implName);
this.fileWriter(implName, content);
}
/**
* 生成Service接口代码)
*/
private String builderServiceInterface(String interfaceName, String implName) {
StringBuffer sb = new StringBuffer();
sb.append("\r\nimport java.io.Serializable;\r\n");
sb.append("import java.util.*;\r\n");
sb.append("\r\nimport com.wasion.commons.dto.SupDto;\r\n");
sb.append("import com.wasion.framework.service.IBaseService;\r\n");
if(this.isCreateBatchMethod){
sb.append("import com.wasion.framework.service.IBatchService;\r\n");
}
//增加类说明
sb.append("\r\n/** \r\n");
sb.append(" * "+tablename+"\r\n");
sb.append(" * @author "+author+" "+this.strDate+"\r\n");
sb.append(" */\r\n");
//类声明
if(this.isCreateBatchMethod){
sb.append("public interface " + interfaceName + " extends IBaseService<"+this.beanName+", SupDto>"+ ", IBatchService<"+this.beanName+", SupDto> {\r\n");
}else{
sb.append("public interface " + interfaceName + " extends IBaseService<"+this.beanName+", SupDto> {\r\n");
}
sb.append("\r\n");
sb.append("\t//demo: public abstract List<"+this.beanName+"> findAll(String sqlExpression, SupDto p);");
sb.append("\r\n");
sb.append("}\r\n");
//System.out.println(sb.toString());
return sb.toString();
}
/**
* 生成Service的Pojo实现代码)
*/
private String builderServicePojoImpl(String interfaceName, String implName) {
StringBuffer sb = new StringBuffer();
sb.append("\r\nimport java.io.Serializable;\r\n");
sb.append("import java.util.*;\r\n");
sb.append("\r\nimport com.wasion.commons.dto.SupDto;\r\n");
sb.append("\r\n");
//增加类说明
sb.append("\r\n/** \r\n");
sb.append(" * "+tablename+"\r\n");
sb.append(" * @author "+author+" "+this.strDate+"\r\n");
sb.append(" */\r\n");
//类声明
sb.append("public class " + implName + " implements "+interfaceName+" {\r\n");
builderServicePojoFieldAndMethod(sb);
//
sb.append("}\r\n");
//System.out.println(sb.toString());
return sb.toString();
}
/**
* 组装Service接口Pojo实现类属性与方法代码
* @return
*/
private void builderServicePojoFieldAndMethod(StringBuffer sb) {
String faceName = "I"+this.beanName+"Dao";
sb.append("\r\n");
sb.append("\tprivate "+faceName+" dao;\r\n");
sb.append("\r\n");
sb.append("\t/** spring in */\r\n");
sb.append("\tpublic "+faceName+" getDao() {\r\n\t\treturn dao;\r\n\t}\r\n");
sb.append("\tpublic void setDao("+faceName+" dao) {\r\n\t\tthis.dao = dao;\r\n\t}\r\n");
sb.append("\r\n");
//方法,基本操作方法 create,delete,update,isExist,findAll(条件)
sb.append("\tpublic "+beanName+" create("+beanName+" entity, SupDto p) {\r\n\t\treturn this.dao.create(entity, p);\r\n\t}\r\n");
sb.append("\tpublic boolean delete("+beanName+" entity, SupDto p) {\r\n\t\treturn this.dao.delete(entity, p);\r\n\t}\r\n");
sb.append("\tpublic "+beanName+" read("+beanName+" entity, SupDto p) {\r\n\t\treturn this.dao.read(entity, p);\r\n\t}\r\n");
sb.append("\tpublic boolean update("+beanName+" entity, SupDto p) {\r\n\t\treturn this.dao.update(entity, p);\r\n\t}\r\n");
sb.append("\tpublic List<"+beanName+"> findAll(String sqlExpression, SupDto p) {\r\n\t\treturn this.dao.findAll(sqlExpression, p);\r\n\t}\r\n");
if(this.isCreateBatchMethod){
sb.append("\r\n");
sb.append("\tpublic boolean createBatch(List<"+beanName+"> entities, SupDto p) {\r\n\t\treturn this.dao.createBatch(entities, p);\r\n\t}\r\n");
sb.append("\tpublic boolean deleteBatch(List<"+beanName+"> entities, SupDto p) {\r\n\t\treturn this.dao.deleteBatch(entities, p);\r\n\t}\r\n");
sb.append("\tpublic boolean updateBatch(List<"+beanName+"> entities, SupDto p) {\r\n\t\treturn this.dao.updateBatch(entities, p);\r\n\t}\r\n");
sb.append("\tpublic boolean updateOrInsertBatch(List<"+beanName+"> entities, SupDto p) {\r\n\t\treturn this.dao.updateOrInsertBatch(entities, p);\r\n\t}\r\n");
}
}
//================================生成 Dao 部分======================================
/**
* 解析处理(生成 Dao代码)
*/
private void builderDaoCode() {
String faceName = "I"+this.beanName+"Dao";
String implName = this.beanName+"DaoJdbcImpl";
String content = builderDaoInterface(faceName, implName);
this.fileWriter(faceName, content);
//
content = builderDaoPojoImpl(faceName, implName);
this.fileWriter(implName, content);
}
/**
* 生成Dao接口代码)
*/
private String builderDaoInterface(String interfaceName, String implName) {
StringBuffer sb = new StringBuffer();
sb.append("\r\nimport java.io.Serializable;\r\n");
sb.append("import java.util.*;\r\n");
sb.append("\r\nimport com.wasion.commons.dto.SupDto;\r\n");
sb.append("import com.wasion.framework.dao.IBaseDao;\r\n");
if(this.isCreateBatchMethod){
sb.append("import com.wasion.framework.dao.IBatchDao;\r\n");
}
//增加类说明
sb.append("\r\n/** \r\n");
sb.append(" * "+tablename+"\r\n");
sb.append(" * @author "+author+" "+this.strDate+"\r\n");
sb.append(" */\r\n");
//类声明
if(this.isCreateBatchMethod){
sb.append("public interface " + interfaceName + " extends IBaseDao<"+this.beanName+", SupDto>" + ", IBatchDao<"+this.beanName+", SupDto> {\r\n");
}else{
sb.append("public interface " + interfaceName + " extends IBaseDao<"+this.beanName+", SupDto> {\r\n");
}
sb.append("\r\n");
sb.append("\t//demo: public abstract List<"+this.beanName+"> findAll(String sqlExpression, SupDto p);");
sb.append("\r\n");
sb.append("}\r\n");
//System.out.println(sb.toString());
return sb.toString();
}
/**
* 生成Dao的Jdbc实现代码)
*/
private String builderDaoPojoImpl(String interfaceName, String implName) {
StringBuffer sb = new StringBuffer();
//增加引用
sb.append("\r\nimport java.util.*;\r\n");
sb.append("import java.math.*;\r\n");
sb.append("\r\n");
sb.append("import org.apache.commons.logging.Log;\r\n");
sb.append("import org.apache.commons.logging.LogFactory;\r\n");
sb.append("\r\n");
sb.append("import com.wasion.commons.dto.SupDto;\r\n");
sb.append("import com.wasion.commons.util.JdbcDataUtils;\r\n");
sb.append("import com.wasion.commons.util.SqlCacheUnits;\r\n");
sb.append("import com.wasion.commons.util.SqlStringUnits;\r\n");
sb.append("\r\n");
sb.append("import com.wasion.framework.dao.BaseMdbDaoSupport;\r\n");
sb.append("\r\n");
//增加类说明
sb.append("\r\n/** \r\n");
sb.append(" * "+tablename+"\r\n");
sb.append(" * @author "+author+" "+this.strDate+"\r\n");
sb.append(" */\r\n");
//类声明
sb.append("public class " + implName + " extends BaseMdbDaoSupport implements "+interfaceName+" {\r\n");
sb.append("\tprivate Log logger = LogFactory.getLog(this.getClass());\r\n");
sb.append("\tprivate SqlStringUnits stool = new SqlStringUnits();\r\n");
sb.append("\tprivate JdbcDataUtils jtool = new JdbcDataUtils();\r\n");
sb.append("\tprivate String table = \""+tablename+"\";\r\n");
sb.append("\tprivate String name = \""+tablename+"\";\r\n");
sb.append("\tprivate final int BATCH_SIZE=500;\r\n");
daoMethodFindAll_1(sb);
daoMethodRead(sb);
daoMethodInsert(sb);
daoMethodDelete_1(sb);
daoMethodUpdate(sb);
if(isCreateBatchMethod){
this.daoMethodInsertBatch(sb);
this.daoMethodDeleteBatch(sb);
this.daoMethodUpdateBatch(sb);
this.daoMethodUpdateOrInsertBatch(sb);
}
sb.append("\r\n\r\n\t//-------------------------Parameters CODE------------------------- \r\n\r\n");
daoMethodProcessInsertParm(sb);
daoMethodProcessDeleteParm(sb);
daoMethodProcessUpdateParm(sb);
daoMethodProcessUpdateOrInsertParm(sb);
daoMethodGetColumnString(sb);
sb.append("\r\n");
sb.append("}\r\n");
return sb.toString();
}
//----------生成 Dao Template 部分----------
/**
* 生成Dao的Jdbc实现: findAll 1对象.
*/
private void daoMethodFindAll_1(StringBuffer sb) {
sb.append("\r\n");
sb.append("\t/** findAll row */\r\n");
sb.append("\tpublic List<"+beanName+"> findAll(String sqlExpression, SupDto p) {\r\n");
//e2->>
sb.append("\t\tif(sqlExpression == null) return null;\r\n");
sb.append("\t\tString tablename = this.getTableName(p, this.table);\r\n");
sb.append("\t\tString sqlNote = this.getSqlNote(p, this.name);\r\n");
sb.append("\t\tString strSql = null;\r\n");
sb.append("\t\tstrSql =\" SELECT \"+this.getColumnString()+\" FROM \"+ tablename+\" \"+sqlExpression;\r\n");
//e1->>
sb.append("\t\treturn this.getJdbcDaoSupport(p).query(strSql, "+beanName+".class, sqlNote);\r\n\t}\r\n");
}
/**
* 生成Dao的Jdbc实现: Read 对象.
*/
private void daoMethodRead(StringBuffer sb) {
sb.append("\r\n");
sb.append("\t/** read row */\r\n");
sb.append("\tpublic "+beanName+" read("+beanName+" entity, SupDto p) {\r\n");
//e2->>
sb.append("\t\tif(entity == null) return null;\r\n");
sb.append("\t\tString tablename = this.getTableName(p, this.table);\r\n");
sb.append("\t\tString sqlNote = this.getSqlNote(p, this.name);\r\n");
sb.append("\t\tString strSql = null;\r\n");
sb.append("\t\tstrSql =\" SELECT \"+this.getColumnString()+\" FROM \"+tablename+\" WHERE ID =1.....\";\r\n");
sb.append("\t\t//TODO ADD WHERE CODE......\r\n");
//e1->>
sb.append("\t\treturn this.getJdbcDaoSupport(p).querySingle(strSql, "+beanName+".class, sqlNote);\r\n\t}\r\n");
}
/**
* 生成Dao的Jdbc实现: insertEntity 插入对象.
*/
private void daoMethodInsert(StringBuffer sb) {
sb.append("\r\n");
sb.append("\t/** create row */\r\n");
sb.append("\tpublic "+beanName+" create("+beanName+" entity, SupDto p) {\r\n");
//e2->>
sb.append("\t\tif(entity == null) return null;\r\n");
sb.append("\t\tString tablename = this.getTableName(p, this.table);\r\n");
sb.append("\t\tString sqlNote = this.getSqlNote(p, this.name);\r\n");
sb.append("\t\tString strSql = null;\r\n");
sb.append("\t\tstrSql =\" INSERT INTO \"+ tablename +\" (\"+this.getColumnString()+\") \"+\r\n");
//e3->>
sb.append("\t\t\t\" VALUES(");
String s="";
for (int i = 0; i < colnames.length; i++) {
if(i>
sb.append("\t\tint rs = this.getJdbcDaoSupport(p).update(strSql, this.processInsertParm(entity), sqlNote);\r\n");
//e1->>
sb.append("\t\treturn rs >0 ? entity : null;\r\n\t}\r\n");
}
/**
* 生成Dao的Jdbc实现: deleteEntityTemplate 删除对象模板.
*/
private void daoMethodDelete_1(StringBuffer sb) {
sb.append("\r\n");
sb.append("\t/** delete row */\r\n");
sb.append("\tpublic boolean delete("+beanName+" entity, SupDto p) {\r\n");
//e2->>
sb.append("\t\tif(entity == null) return false;\r\n");
sb.append("\t\tString tablename = this.getTableName(p, this.table);\r\n");
sb.append("\t\tString sqlNote = this.getSqlNote(p, this.name);\r\n");
sb.append("\t\tString strSql = null;\r\n");
sb.append("\t\tstrSql =\" DELETE \"+ tablename +\r\n");
//e3->>
sb.append("\t\t\t\" WHERE ID =1..... \";\r\n");
sb.append("\t\t//TODO ADD WHERE CODE......\r\n");
//e2->>
sb.append("\t\tint rs = this.getJdbcDaoSupport(p).update(strSql, sqlNote);\r\n");
//e1->>
sb.append("\t\treturn rs >= 0 ? true : false;\r\n\t}\r\n");
}
/**
* 生成Dao的Jdbc实现: updateEntityTemplate 修改对象模板.
*/
private void daoMethodUpdate(StringBuffer sb) {
sb.append("\r\n");
sb.append("\t/** update row */\r\n");
sb.append("\tpublic boolean update("+beanName+" entity, SupDto p) {\r\n");
//e2->>
sb.append("\t\tif(entity == null) return false;\r\n");
sb.append("\t\tString tablename = this.getTableName(p, this.table);\r\n");
sb.append("\t\tString sqlNote = this.getSqlNote(p, this.name);\r\n");
sb.append("\t\tString strSql = null;\r\n");
sb.append("\t\tstrSql =\" UPDATE \"+ tablename +\" SET\"+\r\n");
//e3->>
sb.append("\t\t\t\" ");
String s="";
for (int i = 0; i < colnames.length; i++) {
if(i>
sb.append("\t\t//TODO ADD WHERE CODE......\r\n");
sb.append("\t\tint rs = this.getJdbcDaoSupport(p).update(strSql, this.processUpdateParm(entity), sqlNote);\r\n");
//e1->>
sb.append("\t\treturn rs >= 0 ? true : false;\r\n\t}\r\n");
}
/**
* 生成Dao的Jdbc实现: insertBatch 插入对象.
*/
private void daoMethodInsertBatch(StringBuffer sb) {
sb.append("\r\n");
sb.append("\t/** createBatch row */\r\n");
sb.append("\tpublic boolean createBatch(List<"+beanName+"> entities, SupDto p) {\r\n");
//e2->>
sb.append("\t\tif(entities == null || entities.size()==0) return true;\r\n");
sb.append("\t\tString tablename = this.getTableName(p, this.table);\r\n");
sb.append("\t\tString sqlNote = this.getSqlNote(p, this.name);\r\n");
sb.append("\t\tString strSql = null;\r\n");
sb.append("\t\tstrSql =\" INSERT INTO \"+ tablename +\" (\"+this.getColumnString()+\") \"+\r\n");
//e3->>
sb.append("\t\t\t\" VALUES(");
String s="";
for (int i = 0; i < colnames.length; i++) {
if(i>batch
sb.append("\t\tboolean bResult = false;\r\n");
sb.append("\t\tList
代码太长了, 生成的类由看下一节
关于数据源的问题, 请自己处理, 这个不是核心代码.