package com.cn.util;
import java.lang.reflect.Field;
import org.apache.log4j.Logger;
import com.cn.model.credit.VehicleLicenseInfo;
/**
* 自动生成mybatis的XML文件
* */
public class AutoCreateXML {
//日志
private static final Logger LOGGER = Logger.getLogger(AutoCreateXML.class);
/**
* @param args
*/
public static void main(String[] args) {
//
printGenerate(new MsgInfo(),"");
printGenerate(new VehicleLicenseInfo(),"");
}
public static void printGenerate(Object model,String prefix){
LOGGER.debug(generteXmlQueryPage(model,prefix)+"\n");
LOGGER.debug(generateXmlFindById(model,prefix)+"\n");
LOGGER.debug(generateXmlSave(model,prefix)+"\n");
LOGGER.debug(generateXmlUpdate(model,prefix)+"\n");
LOGGER.debug(generateXmlDelete(model,prefix));
}
/**生成XML add*/
public static String generateXmlSave(Object model,String prefix){
String simpleName=model.getClass().getSimpleName();//类名
String upperSimpleName = simpleName.replaceFirst(simpleName.substring(0, 1), simpleName.substring(0, 1).toUpperCase());
String add="\n";
add+="
\n";
add+="INSERT INTO "+prefix+getColumnName(upperSimpleName).toUpperCase().substring(1)+"\n(\n";
String fields="";
String names="";
Field[] field = model.getClass().getDeclaredFields(); //获取实体类的所有属性,返回Field数组
for(int j=0 ; j
String name = field[j].getName(); //获取属性的名字
fields+=getColumnName(name);
if(name.contains("Date")||name.contains("Term")||name.contains("birthday")){
names+="TO_DATE(#{"+name+"},'yyyy-mm-dd')";
}else if(name.contains("Time")){
names+="TO_DATE(#{"+name+"},'yyyy-mm-dd hh24:mi:ss')";
}else {
names+="#{"+name+"}";
}
if(j
fields+=",\n";
names+=",\n";
}
}
add+=fields.toUpperCase()+"\n)\nVALUES\n(\n"+names+"\n)\n ";
return add;
}
/**生成XML update*/
public static String generateXmlUpdate(Object model,String prefix){
String simpleName=model.getClass().getSimpleName();
String upperSimpleName = simpleName.replaceFirst(simpleName.substring(0, 1), simpleName.substring(0, 1).toUpperCase());
String update="\n";
update+="
\n";
update+="UPDATE "+prefix+getColumnName(upperSimpleName).toUpperCase().substring(1)+" \nSET \n";
update+="\n";
Field[] field = model.getClass().getDeclaredFields(); //获取实体类的所有属性,返回Field数组
for(int j=1 ; j
String name = field[j].getName(); //获取属性的名字
update+="\n";
if(name.contains("Date")||name.contains("Term")||name.contains("birthday")){
update+=getColumnName(name).toUpperCase()+"=TO_DATE(#{"+name+"},'yyyy-mm-dd')";
}else if(name.contains("Time")){
update+=getColumnName(name).toUpperCase()+"=TO_DATE(#{"+name+"},'yyyy-mm-dd hh24:mi:ss')";
}else {
update+=getColumnName(name).toUpperCase()+"=#{"+name+"}";
}
if(j
update+=",";
}
update+="\n \n";
}
update+=" \n";
update+=" WHERE ID=#{id}\n ";
return update;
}
/**生成XML findById*/
public static String generateXmlFindById(Object model,String prefix){
String simpleName=model.getClass().getSimpleName();
String upperSimpleName = simpleName.replaceFirst(simpleName.substring(0, 1), simpleName.substring(0, 1).toUpperCase());
String findById="\n";
findById+="
\n";
findById+="SELECT \n";
Field[] field = model.getClass().getDeclaredFields(); //获取实体类的所有属性,返回Field数组
for(int j=0 ; j String name = field[j].getName(); //获取属性的名字
if(name.contains("Date")||name.contains("Term")||name.contains("birthday")){
findById+="TO_CHAR("+getColumnName(name).toUpperCase()+",'yyyy-mm-dd') AS "+name;
}else if(name.contains("Time")){
findById+="TO_CHAR("+getColumnName(name).toUpperCase()+",'yyyy-mm-dd hh24:mi:ss') AS "+name;
}else {
findById+=getColumnName(name).toUpperCase()+" AS "+name;
}
if(j
findById+=",";
}
findById+="\n";
}
findById+="FROM "+prefix+getColumnName(upperSimpleName).toUpperCase().substring(1);
findById+=" \nWHERE \n";
findById+="ID= #{id}\n";
findById+=" ";
return findById;
}
/**生成XML 根据条件分页查询*/
public static String generteXmlQueryPage(Object model,String prefix){
String simpleName=model.getClass().getSimpleName();
String upperSimpleName = simpleName.replaceFirst(simpleName.substring(0, 1), simpleName.substring(0, 1).toUpperCase());
String queryObjectPage="\n";
queryObjectPage+="
\n";
queryObjectPage+="SELECT \n";
Field[] field = model.getClass().getDeclaredFields(); //获取实体类的所有属性,返回Field数组
for(int j=0 ; j String name = field[j].getName(); //获取属性的名字
if(name.contains("Date")||name.contains("Term")||name.contains("birthday")){
queryObjectPage+="TO_CHAR("+getColumnName(name).toUpperCase()+",'yyyy-mm-dd') AS "+name;
}else if(name.contains("Time")){
queryObjectPage+="TO_CHAR("+getColumnName(name).toUpperCase()+",'yyyy-mm-dd hh24:mi:ss') AS "+name;
}else {
queryObjectPage+=getColumnName(name).toUpperCase()+" AS "+name;
}
if(j
queryObjectPage+=",";
}
queryObjectPage+="\n";
}
queryObjectPage+="FROM "+prefix+getColumnName(upperSimpleName).toUpperCase().substring(1);
queryObjectPage+="\n";
queryObjectPage+="\n
+".属性 != null and "+simpleName.replaceFirst(simpleName.substring(0, 1), simpleName.substring(0, 1).toLowerCase())
+".属性 != ''\">";
queryObjectPage+="\n字段 = #{"+simpleName.replaceFirst(simpleName.substring(0, 1), simpleName.substring(0, 1).toLowerCase())+".属性}";
queryObjectPage+="\n ";
queryObjectPage+="\n ";
queryObjectPage+="\n ";
return queryObjectPage;
}
/**生成XML delete*/
public static String generateXmlDelete(Object model,String prefix){
String simpleName=model.getClass().getSimpleName();
String upperSimpleName = simpleName.replaceFirst(simpleName.substring(0, 1), simpleName.substring(0, 1).toUpperCase());
String del="\n";
del+="
\n";
del+="DELETE FROM "+prefix+getColumnName(upperSimpleName).toUpperCase().substring(1)+" WHERE ID = #{id}\n";
del+=" ";
return del;
}
/**逆向转换字符串*/
public static String getColumnName(String field){
field=field.replaceAll("A", "_A");
field=field.replaceAll("B", "_B");
field=field.replaceAll("C", "_C");
field=field.replaceAll("D", "_D");
field=field.replaceAll("E", "_E");
field=field.replaceAll("F", "_F");
field=field.replaceAll("G", "_G");
field=field.replaceAll("H", "_H");
field=field.replaceAll("I", "_I");
field=field.replaceAll("J", "_J");
field=field.replaceAll("K", "_K");
field=field.replaceAll("L", "_L");
field=field.replaceAll("M", "_M");
field=field.replaceAll("N", "_N");
field=field.replaceAll("O", "_O");
field=field.replaceAll("P", "_P");
field=field.replaceAll("Q", "_Q");
field=field.replaceAll("R", "_R");
field=field.replaceAll("S", "_S");
field=field.replaceAll("T", "_T");
field=field.replaceAll("U", "_U");
field=field.replaceAll("V", "_V");
field=field.replaceAll("W", "_W");
field=field.replaceAll("X", "_X");
field=field.replaceAll("Y", "_Y");
field=field.replaceAll("Z", "_Z");
return field;
}
}
使用该方法生成xml文件时要注意变量以及数据库字段命名格式