package util;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
*
*/
/**
* @author Gaojingbo
*华为昆明数字经济学院-JAVA课程-高靖博 @Copy
*/
@SuppressWarnings({"rawtypes", "unchecked"})
public class Mybatis {
/**
* 域文件内容
* @param className
* @param list
* @param domainPackage
* @return
* @throws Exception
*/
public static String getDomainFile(String className, String table, List<Map> list, String domainPackage)throws Exception {
StringBuffer sbFile = new StringBuffer();
StringBuffer sbImport = new StringBuffer();
StringBuffer sbPerp = new StringBuffer();
StringBuffer sbGet = new StringBuffer();
StringBuffer sbSet = new StringBuffer();
Map mapImport = new HashMap();
for(Map map : list){
String colName = (String)map.get("COLUMN_NAME");
String[] dataType = getPropertyType(map);
sbPerp.append(" private ");
sbGet.append(" public ");
sbSet.append(" public void set"+ colName.substring(0,1).toUpperCase() + colName.substring(1) +"(");
if(dataType[0].equalsIgnoreCase("Date")){
mapImport.put("import java.util.Date;", "1");
sbPerp.append("Date");
sbGet.append("Date");
sbSet.append("Date");
}
else if(dataType[0].equalsIgnoreCase("Integer")){
sbPerp.append("Integer");
sbGet.append("Integer");
sbSet.append("Integer");
}
else if(dataType[0].equalsIgnoreCase("BigDecimal")){
mapImport.put("import java.math.BigDecimal;", "1");
sbPerp.append("BigDecimal");
sbGet.append("BigDecimal");
sbSet.append("BigDecimal");
}
else {
sbPerp.append("String");
sbGet.append("String");
sbSet.append("String");
}
sbPerp.append(" "+ colName +";\n");
sbGet.append(" get"+ colName.substring(0,1).toUpperCase() + colName.substring(1) +"() {\n");
sbGet.append(" return "+ colName +";\n");
sbGet.append(" }\n");
sbSet.append(" "+ colName +") {\n");
sbSet.append(" this."+ colName +" = "+ colName +";\n");
sbSet.append(" }\n");
}
Iterator it = mapImport.keySet().iterator();
while (it.hasNext()) {
String key = (String) it.next();
sbImport.append(key +"\n");
}
sbImport.append("import java.io.Serializable;\n");
sbFile.append("/**\n");
sbFile.append(" * @Copyright: 华为昆明数字经济学院-JAVA课程-高靖博 @Copy All rights reserved.\n");
sbFile.append(" */\n");
sbFile.append("package "+ domainPackage +";\n\n");
sbFile.append(sbImport);
sbFile.append("\n");
sbFile.append("import lombok.Data;\n");
//sbFile.append("import com.alibaba.druid.support.monitor.annotation.MTable;\n");
sbFile.append("\n");
//sbFile.append("@MTable(name = \""+table+"\")\n");
sbFile.append("@Data\n");
sbFile.append("public class "+ className +" implements Serializable {\n\n");
sbFile.append(" private static final long serialVersionUID = 1L;\n\n");
sbFile.append(sbPerp);
sbFile.append("\n");
sbFile.append(sbGet);
sbFile.append("\n");
sbFile.append(sbSet);
sbFile.append("}");
return sbFile.toString();
}
/**
* 域Example文件内容
* @param table
* @param list
* @param domainPackage
* @return
* @throws Exception
*/
public static String getDomainExampleFile(String table, List<Map> list, String domainPackage)throws Exception {
String className = getClassName(table);
Map mapImport = new HashMap();
StringBuffer sbFile = new StringBuffer();
StringBuffer sbCondition = new StringBuffer();
for(Map map : list){
// 生成条件语句
sbCondition.append(getConditions(map, " ", mapImport));
}
sbFile.append("/**\n");
sbFile.append(" * @Copyright: 华为昆明数字经济学院-JAVA课程-高靖博 @Copy All rights reserved.\n");
sbFile.append(" */\n");
sbFile.append("package "+ domainPackage +";\n\n");
sbFile.append("import java.util.ArrayList;\n");
sbFile.append("import java.util.List;\n");
sbFile.append("import java.util.Iterator;\n");
sbFile.append("import java.util.Date;\n");
Iterator it = mapImport.keySet().iterator();
while (it.hasNext()) {
String key = (String) it.next();
sbFile.append(key +"\n");
}
sbFile.append("\n");
sbFile.append("public class " + className + "Example {\n\n");
sbFile.append(" protected String orderByClause;\n\n");
sbFile.append(" protected boolean distinct;\n\n");
sbFile.append(" protected List<Criteria> oredCriteria;\n\n");
sbFile.append(" public " + className + "Example() {\n");
sbFile.append(" oredCriteria = new ArrayList<Criteria>();\n");
sbFile.append(" }\n\n");
sbFile.append(" public void setOrderByClause(String orderByClause) {\n");
sbFile.append(" this.orderByClause = orderByClause;\n");
sbFile.append(" }\n\n");
sbFile.append(" public String getOrderByClause() {\n");
sbFile.append(" return orderByClause;\n");
sbFile.append(" }\n\n");
sbFile.append(" public void setDistinct(boolean distinct) {\n");
sbFile.append(" this.distinct = distinct;\n");
sbFile.append(" }\n\n");
sbFile.append(" public boolean isDistinct() {\n");
sbFile.append(" return distinct;\n");
sbFile.append(" }\n");
sbFile.append(" public List<Criteria> getOredCriteria() {\n");
sbFile.append(" return oredCriteria;\n");
sbFile.append(" }\n\n");
sbFile.append(" public void or(Criteria criteria) {\n");
sbFile.append(" oredCriteria.add(criteria);\n");
sbFile.append(" }\n\n");
sbFile.append(" public Criteria or() {\n");
sbFile.append(" Criteria criteria = createCriteriaInternal();\n");
sbFile.append(" oredCriteria.add(criteria);\n");
sbFile.append(" return criteria;\n");
sbFile.append(" }\n\n");
sbFile.append(" public Criteria createCriteria() {\n");
sbFile.append(" Criteria criteria = createCriteriaInternal();\n");
sbFile.append(" if (oredCriteria.size() == 0) {\n");
sbFile.append(" oredCriteria.add(criteria);\n");
sbFile.append(" }\n");
sbFile.append(" return criteria;\n");
sbFile.append(" }\n\n");
sbFile.append(" protected Criteria createCriteriaInternal() {\n");
sbFile.append(" Criteria criteria = new Criteria();\n");
sbFile.append(" return criteria;\n");
sbFile.append(" }\n\n");
sbFile.append(" public void clear() {\n");
sbFile.append(" oredCriteria.clear();\n");
sbFile.append(" orderByClause = null;\n");
sbFile.append(" distinct = false;\n");
sbFile.append(" }\n\n");
sbFile.append(" protected abstract static class GeneratedCriteria {\n");
sbFile.append(" protected List<Criterion> cr