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+="<insert id=\"add"+simpleName+"\" parameterType=\""+simpleName+"\">\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<field.length ; 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<field.length-1){
fields+=",\n";
names+=",\n";
}
}
add+=fields.toUpperCase()+"\n)\nVALUES\n(\n"+names+"\n)\n</insert>";
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+="<update id=\"update"+simpleName+"\" parameterType=\""+simpleName+"\">\n";
update+="UPDATE "+prefix+getColumnName(upperSimpleName).toUpperCase().substring(1)+" \nSET \n";
update+="<trim suffixOverrides=\",\" >\n";
Field[] field = model.getClass().getDeclaredFields(); //获取实体类的所有属性,返回Field数组
for(int j=1 ; j<field.length ; j++){ //遍历所有属性
String name = field[j].getName(); //获取属性的名字
update+="<if test=\""+name+" != null and "+name+" != ''\">\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<field.length-1){
update+=",";
}
update+="\n</if>\n";
}
update+="</trim>\n";
update+=" WHERE ID=#{id}\n</update>";
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="<!-- 根据ID查询 -->\n";
findById+="<select id=\"get"+simpleName+"ById\" resultType=\""+simpleName+"\">\n";
findById+="SELECT \n";
Field[] field = model.getClass().getDeclaredFields(); //获取实体类的所有属性,返回Field数组
for(int j=0 ; j<field.length ; 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<field.length-1){
findById+=",";
}
findById+="\n";
}
findById+="FROM "+prefix+getColumnName(upperSimpleName).toUpperCase().substring(1);
findById+=" \nWHERE \n";
findById+="ID= #{id}\n";
findById+="</select>";
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+="<select id=\"query"+simpleName+"s\" resultType=\""+simpleName+"\">\n";
queryObjectPage+="SELECT \n";
Field[] field = model.getClass().getDeclaredFields(); //获取实体类的所有属性,返回Field数组
for(int j=0 ; j<field.length ; 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<field.length-1){
queryObjectPage+=",";
}
queryObjectPage+="\n";
}
queryObjectPage+="FROM "+prefix+getColumnName(upperSimpleName).toUpperCase().substring(1);
queryObjectPage+="\n<trim prefix=\"WHERE\" prefixOverrides=\"AND|OR\">";
queryObjectPage+="\n<if test=\""+simpleName.replaceFirst(simpleName.substring(0, 1), simpleName.substring(0, 1).toLowerCase())
+".属性 != 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</if>";
queryObjectPage+="\n</trim>";
queryObjectPage+="\n</select>";
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+="<delete id=\"del"+simpleName+"\">\n";
del+="DELETE FROM "+prefix+getColumnName(upperSimpleName).toUpperCase().substring(1)+" WHERE ID = #{id}\n";
del+="</delete>";
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;
}
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+="<insert id=\"add"+simpleName+"\" parameterType=\""+simpleName+"\">\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<field.length ; 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<field.length-1){
fields+=",\n";
names+=",\n";
}
}
add+=fields.toUpperCase()+"\n)\nVALUES\n(\n"+names+"\n)\n</insert>";
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+="<update id=\"update"+simpleName+"\" parameterType=\""+simpleName+"\">\n";
update+="UPDATE "+prefix+getColumnName(upperSimpleName).toUpperCase().substring(1)+" \nSET \n";
update+="<trim suffixOverrides=\",\" >\n";
Field[] field = model.getClass().getDeclaredFields(); //获取实体类的所有属性,返回Field数组
for(int j=1 ; j<field.length ; j++){ //遍历所有属性
String name = field[j].getName(); //获取属性的名字
update+="<if test=\""+name+" != null and "+name+" != ''\">\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<field.length-1){
update+=",";
}
update+="\n</if>\n";
}
update+="</trim>\n";
update+=" WHERE ID=#{id}\n</update>";
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="<!-- 根据ID查询 -->\n";
findById+="<select id=\"get"+simpleName+"ById\" resultType=\""+simpleName+"\">\n";
findById+="SELECT \n";
Field[] field = model.getClass().getDeclaredFields(); //获取实体类的所有属性,返回Field数组
for(int j=0 ; j<field.length ; 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<field.length-1){
findById+=",";
}
findById+="\n";
}
findById+="FROM "+prefix+getColumnName(upperSimpleName).toUpperCase().substring(1);
findById+=" \nWHERE \n";
findById+="ID= #{id}\n";
findById+="</select>";
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+="<select id=\"query"+simpleName+"s\" resultType=\""+simpleName+"\">\n";
queryObjectPage+="SELECT \n";
Field[] field = model.getClass().getDeclaredFields(); //获取实体类的所有属性,返回Field数组
for(int j=0 ; j<field.length ; 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<field.length-1){
queryObjectPage+=",";
}
queryObjectPage+="\n";
}
queryObjectPage+="FROM "+prefix+getColumnName(upperSimpleName).toUpperCase().substring(1);
queryObjectPage+="\n<trim prefix=\"WHERE\" prefixOverrides=\"AND|OR\">";
queryObjectPage+="\n<if test=\""+simpleName.replaceFirst(simpleName.substring(0, 1), simpleName.substring(0, 1).toLowerCase())
+".属性 != 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</if>";
queryObjectPage+="\n</trim>";
queryObjectPage+="\n</select>";
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+="<delete id=\"del"+simpleName+"\">\n";
del+="DELETE FROM "+prefix+getColumnName(upperSimpleName).toUpperCase().substring(1)+" WHERE ID = #{id}\n";
del+="</delete>";
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文件时要注意变量以及数据库字段命名格式