package generator.tools;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Map.Entry;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import generator.bean.KeyValueBean;
import generator.bean.TableInfoBean;
import generator.util.CommonUtil;
import generator.util.FileUtil;
import generator.util.PropertyFileReader;
public class SqlMapGenerator {
private SqlMapGenerator() {
}
public static void genaratorSqlMap(Configuration cfg, Entry <String, List <TableInfoBean>> tableInfo) throws IOException, TemplateException {
BufferedWriter writer = null;
try {
String path = CommonUtil.getSqlMapOutPutPath(tableInfo.getKey()) + "/" + getSqlMapName(tableInfo.getKey());
FileUtil.mkdir(path);
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path), "UTF-8"));
Map <String, Object> rootMap = new HashMap <String, Object>();
setRootMapForSQLMap(rootMap, tableInfo);
Template tpl = cfg.getTemplate("SqlMap.tpl");
tpl.setEncoding("UTF-8");
tpl.process(rootMap, writer);
} finally {
if (null != writer) {
writer.flush();
writer.close();
}
}
}
private static void setRootMapForSQLMap(Map <String, Object> rootMap, Entry <String, List <TableInfoBean>> tableInfo) {
Properties props = PropertyFileReader.getProperties("config.base");
List <String> commonColumns = Arrays.asList(props.getProperty("update.columns.nouse").split(","));
rootMap.put("tblName", CommonUtil.getOutputColumnName(tableInfo.getKey(), false));
rootMap.put("tblNameSql", tableInfo.getKey());
// rootMap.put("package", CommonUtil.getDomainPackage(tableInfo.getKey()));
rootMap.put("package", CommonUtil.getEntityPackage());
rootMap.put("daoPackage", CommonUtil.getDaoPackage(tableInfo.getKey()));
List <KeyValueBean> whereKeyValueList = new ArrayList <KeyValueBean>();
rootMap.put("whereConditionList", whereKeyValueList);
List <KeyValueBean> updateKeyValueList = new ArrayList <KeyValueBean>();
rootMap.put("updateColumnList", updateKeyValueList);
List <KeyValueBean> rsltSetKeyValueList = new ArrayList <KeyValueBean>();
rootMap.put("rsltSetColumList", rsltSetKeyValueList);
KeyValueBean keyValueBean = null;
String primaryKey = "";
String defaultSortKeys = "";
// add start for where key DEL_FLG='0'
List <String> allColums = new ArrayList <String>();
boolean isDelFlgPK = false;
// add end for where key DEL_FLG='0'
for (TableInfoBean tableInfoBean : tableInfo.getValue()) {
// add start for where key DEL_FLG='0'
allColums.add(tableInfoBean.getColumnName());
// add end for where key DEL_FLG='0'
keyValueBean = new KeyValueBean();
keyValueBean.setKey(tableInfoBean.getColumnName());
keyValueBean.setValue(CommonUtil.getOutputColumnName(tableInfoBean.getColumnName(), false));
whereKeyValueList.add(keyValueBean);
if (!commonColumns.contains(keyValueBean.getKey())) {
updateKeyValueList.add(keyValueBean);
}
rsltSetKeyValueList.add(keyValueBean);
if (!rootMap.containsKey("countColumn")) {
rootMap.put("countColumn", keyValueBean.getKey());
}
if (tableInfoBean.isPrimaryKey()) {
// add start for where key DEL_FLG='0'
if ("DEL_FLG".equals(keyValueBean.getKey())) {
isDelFlgPK = true;
}
// add end for where key DEL_FLG='0'
if ("".equals(primaryKey)) {
primaryKey += keyValueBean.getKey() + "=#" + keyValueBean.getValue() + "#";
defaultSortKeys += keyValueBean.getKey() + " asc";
} else {
primaryKey += " and " + keyValueBean.getKey() + "=#" + keyValueBean.getValue() + "#";
defaultSortKeys += ", " + keyValueBean.getKey() + " asc";
}
}
}
// add start for where key DEL_FLG='0'
if (!isDelFlgPK && allColums.contains("DEL_FLG")) {
primaryKey += " and DEL_FLG='0'";
}
// add end for where key DEL_FLG='0'
rootMap.put("primaryKeys", primaryKey);
rootMap.put("selectSql", getSelectSql(tableInfo));
rootMap.put("insertKeySql", getInsertKeySql(tableInfo));
rootMap.put("insertValueSql", getInsertValueSql(tableInfo));
rootMap.put("updateSql", getUpdateSql(tableInfo));
rootMap.put("conditionSql", getConditionSql(tableInfo));
// ソート順の設定
String sortKeys = props.getProperty(tableInfo.getKey() + ".sortKeys");
if (null == sortKeys || "".equals(sortKeys)) {
rootMap.put("sortKeys", defaultSortKeys);
} else {
rootMap.put("sortKeys", sortKeys);
}
}
private static String getSelectSql(Entry <String, List <TableInfoBean>> tableInfo) {
String selectSql = "select\r\n";
selectSql += "\t\t";
for (TableInfoBean tableInfoBean : tableInfo.getValue()) {
selectSql += tableInfoBean.getColumnName() + ",";
}
selectSql = selectSql.substring(0, selectSql.length() - 1);
selectSql += "\r\n";
selectSql += "\t\t";
selectSql += "from " + tableInfo.getKey();
return selectSql;
}
private static String getInsertKeySql(Entry <String, List <TableInfoBean>> tableInfo) {
String insertSql = "\t\tinsert into\r\n";
insertSql += "\t\t";
String keys = "";
// String values = "";
for (TableInfoBean tableInfoBean : tableInfo.getValue()) {
keys += tableInfoBean.getColumnName() + ",";
// values += "#"
// + CommonUtil.getOutputColumnName(tableInfoBean.getColumnName(), false)
// + "#,";
}
keys = keys.substring(0, keys.length() - 1);
// values = values.substring(0, values.length() - 1);
insertSql += tableInfo.getKey() + "(";
insertSql += keys + ")";
// insertSql += "\t\t";
// insertSql += "values(" + values + ")";
return insertSql;
}
private static String getInsertValueSql(Entry <String, List <TableInfoBean>> tableInfo) {
ArrayList <TableInfoBean> tableInfoList = new ArrayList <TableInfoBean>();
int columnCount = 0;
for (TableInfoBean tableInfoBean : tableInfo.getValue()) {
tableInfoList.add(columnCount, tableInfoBean);
columnCount++;
}
String updateSql = "\t\t<dynamic>\r\n\t\t(\r\n";
for (int i = 0; i < tableInfoList.size(); i++) {
TableInfoBean tableInfoBean = tableInfoList.get(i);
if ("java.math.BigDecimal".equals(CommonUtil.getJavaMappingType(tableInfoBean.getTypeName()))) {
updateSql += "\t\t<isEmpty property=\"" + CommonUtil.getOutputColumnName(tableInfoBean.getColumnName(), false) + "\">\r\n";
if (i < tableInfoList.size() - 1) {
if (tableInfoBean.isNullAble()) {
up
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
tableGenerator.zip (43个子文件)
tableGenerator
.settings
org.eclipse.core.resources.prefs 149B
src
config
base.properties 2KB
jdbc.properties 438B
tblcomment.properties 45B
tbldef.properties 123B
generator
Globles.java 488B
tools
EntityGenerator.java 4KB
ServiceGenerator.java 5KB
DaoGenerator.java 4KB
DomainGenerator.java 4KB
XmlGenerator.java 4KB
SqlMapGenerator.java 15KB
VoGenerator.java 4KB
ConditionDomainGenerator.java 4KB
bean
TableInfoBean.java 1KB
DomainDefineBean.java 1KB
KeyValueBean.java 410B
Generator.java 3KB
util
CommentUtil.java 1KB
DateUtil.java 359B
CommonUtil.java 9KB
FileUtil.java 916B
PropertyFileReader.java 790B
tblloader
TableInfoFetch.java 5KB
DBConnection.java 1KB
other
tools
SerialVersionUIDGen.java 6KB
template
DaoImpl.tpl 2KB
ServiceImpl.tpl 2KB
applicationContext.tpl 2KB
ConditionDomain.tpl 1KB
Vo.tpl 1KB
Entity.tpl 1KB
serial.tpl 109B
SqlMapConfig.tpl 553B
SqlMap.tpl 3KB
Domain.tpl 1KB
.checkstyle 301B
.project 390B
.classpath 659B
lib
commons-io-1.3.1.jar 82KB
mysql-connector-java-5.0.8-bin.jar 528KB
freemarker.jar 859KB
commons-lang.jar 237KB
共 43 条
- 1
dingxinggoodboy
- 粉丝: 15
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页