package com.dbu;
import java.util.List;
import java.util.Map;
public class IbatisUtil {
private static String br = "\r\n";
private static String xmlTemp="<?XML version=\"1.0\" encoding=\"UTF-8\"?>"+br
+"<!DOCTYPE sqlMap PUBLIC \"-//ibatis.apache.org//DTD SQL Map 2.0//EN\" "+br
+"\"http://ibatis.apache.org/dtd/sql-map-2.dtd\">"+br
+"<sqlMap namespace=\"{namespace}\">"+br+br
+"<typeAlias alias=\"{alias}\" type=\"{javaBean}\"/>"+br+br
+"<cacheModel id=\"{cacheModelId}\" type=\"LRU\" readOnly=\"false\" serialize=\"true\">"+br
+"<flushInterval hours=\"24\" />"+br
+"<flushOnExecute statement=\"{insertId}\" />"+br
+"<flushOnExecute statement=\"{updateId}\" />"+br
+"<flushOnExecute statement=\"{deleteId}\" />"+br
+"<property name=\"cache-size\" value=\"20\" />"+br
+"</cacheModel>"+br+br
+"{parameterMap}"+br+br
+"{resultMap}"+br+br
//查询
+" <select id=\"{selectId}\" resultClass=\"{resultClass}\" cacheModel=\"{cacheModelId}\" "+br+" parameterClass=\"{selectParameterClass}\">" +br
+"{selectSql}" +br
+"</select>" +br+br
//查询(Map)
+" <select id=\"{selectId}Map\" resultClass=\"{resultClassMap}\" cacheModel=\"{cacheModelId}\" "+br+" parameterClass=\"{selectParameterClassMap}\">" +br
+"{selectSql}" +br
+"</select>" +br+br
//删除
+"<delete id=\"{deleteId}\" parameterClass=\"{deleteParameterClass}\">" +br
+"{deleteSql}"+br
+"</delete>" +br+br
//添加
+"<insert id=\"{insertId}\" parameterClass=\"{insertParameterClass}\">" +br
+"{insertSql}" +br
+"</insert>" +br+br
//修改
+"<update id=\"{updateId}\" parameterClass=\"{updateParameterClass}\">" +br
+"{updateSql}" +br
+"</update>" +br+br
+"</sqlMap>";
public static String getXML(Map tncPath, String tn,List colList){
String temp = xmlTemp;
String tnc = BaseDbUtil.topUpperCase(BaseDbUtil.replaceAll(tn, "_", ""));
temp = BaseDbUtil.replaceAll(temp,"{javaBean}",BaseDbUtil.mapGet(tncPath, "javaBean")+"."+tnc);
temp = BaseDbUtil.replaceAll(temp,"{namespace}",tnc);
temp = BaseDbUtil.replaceAll(temp,"{alias}",BaseDbUtil.topLowerCase(tnc));
String cacheModelId = BaseDbUtil.topLowerCase(tnc)+"-cache";
temp = BaseDbUtil.replaceAll(temp,"{cacheModelId}",cacheModelId);
String rms = "";
String rme = "";
String rmm = "";
String pms = "";
String pme = "";
String pmm = "";
rms = "<resultMap id=\"get"+tnc+"-result\" class=\"java.util.HashMap\">"+br;
pms = "<parameterMap id=\"get"+tnc+"-parameter\" class=\"java.util.HashMap\">"+br;
for(int i=0;i<colList.size();i++){
Map map = (Map)colList.get(i);
String name = String.valueOf(map.get("name"));
name = Character.toLowerCase(name.charAt(0))+name.substring(1);
String dtype = String.valueOf(map.get("dtype"));
if("CLOB".equals(dtype) || "NCLOB".equals(dtype)){
rmm += "<result property=\""+ name+"\" column=\""+ name +"\" jdbcType=\"CLOB\" javaType=\"java.lang.String\" />"+br;
}else{
rmm += "<result property=\""+ name+"\" column=\""+ name +"\"/>"+br;
}
if("1".equals(map.get("pk"))){
pmm += "<parameter property=\""+ name+"\"/>"+br;
}
}
rme = "</resultMap>";
pme = "</parameterMap>";
String resultMap = rms+rmm+rme;
String parameterMap = pms+pmm+pme;
temp = BaseDbUtil.replaceAll(temp,"{resultMap}",resultMap);
temp = BaseDbUtil.replaceAll(temp,"{parameterMap}",parameterMap);
String selectId = "get"+tnc;
temp = BaseDbUtil.replaceAll(temp,"{selectId}",selectId);
String resultClass = "get"+tnc+"-result";
temp = BaseDbUtil.replaceAll(temp,"{resultClass}",resultClass);
String selectParameterClass = "get"+tnc+"-parameter";
temp = BaseDbUtil.replaceAll(temp,"{selectParameterClass}",selectParameterClass);
String resultClassMap = BaseDbUtil.topLowerCase(tnc);
temp = BaseDbUtil.replaceAll(temp,"{resultClassMap}",resultClassMap);
String selectParameterClassMap = "get"+tnc+"-parameter";
temp = BaseDbUtil.replaceAll(temp,"{selectParameterClassMap}",selectParameterClassMap);
String selectSql = SqlUtil.getSelectSql(tn, colList);
temp = BaseDbUtil.replaceAll(temp,"{selectSql}",selectSql);
String insertId = "insert"+tnc;
temp = BaseDbUtil.replaceAll(temp,"{insertId}",insertId);
String insertParameterClass = BaseDbUtil.topLowerCase(tnc);
temp = BaseDbUtil.replaceAll(temp,"{insertParameterClass}",insertParameterClass);
String insertSql = SqlUtil.getInsertSql(tn, colList);
temp = BaseDbUtil.replaceAll(temp,"{insertSql}",insertSql);
String updateId = "update"+tnc;
temp = BaseDbUtil.replaceAll(temp,"{updateId}",updateId);
String updateParameterClass = BaseDbUtil.topLowerCase(tnc);
temp = BaseDbUtil.replaceAll(temp,"{updateParameterClass}",updateParameterClass);
String updateSql = SqlUtil.getUpdateSql(tn, colList);
temp = BaseDbUtil.replaceAll(temp,"{updateSql}",updateSql);
String deleteId = "delete"+tnc;
temp = BaseDbUtil.replaceAll(temp,"{deleteId}",deleteId);
String deleteParameterClass = "get"+tnc+"-parameter";
temp = BaseDbUtil.replaceAll(temp,"{deleteParameterClass}",deleteParameterClass);
String deleteSql = SqlUtil.getDeleteSql(tn, colList);
temp = BaseDbUtil.replaceAll(temp,"{deleteSql}",deleteSql);
return temp;
}
}