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
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
Ibatis+Spring的代码生成器,可以生成Ibatis+Spring的代码。 包括service,dao,entity,dto,xml。 其中模板需要修改成自己的基类。 数据库链接以mysql为标准,可以自行修改。 Generator.java为可执行文件。
资源详情
资源评论
资源推荐
收起资源包目录

























































共 43 条
- 1







格式:zip 资源大小:2.5MB























maco_liao
- 粉丝: 10
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- 大数据背景下的信息处理技术分析与研究.docx
- mssqlserver2000企业安装教程.doc
- 促进大数据发展行动纲要.doc
- 徐水职教中心计算机专业的教材建设及设计问题.docx
- 软件销售技巧销售话术.doc
- 软件测试技术基础CH.ppt
- 中小型餐厅无线监控网络一体化解决方案.doc
- 斜齿轮传动计算机辅助设计VB.doc
- 天津工程技术师范学院数控机床与编程试题库附答案.doc
- 基于百度文字识别 API 的身份证银行卡驾驶证行驶证快速识别工具
- 创新基金网络工作系统培训.docx
- 基于MATLAB的通信系统的方案设计书与仿真.doc
- 通信技术概论信号能量谱密度与功率谱密度.doc
- 大数据时代大学生思想政治教育探析.docx
- 计算机软件考试考生的报考动机研究.docx
- 电子商务(图书)微观环境研究分析.doc
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



安全验证
文档复制为VIP权益,开通VIP直接复制

评论10