通过javaBean反射转换成mybatis映射文件

package mysql.tool;

import java.util.List;

import mysql.model.LoanTransferDto;

public class MapperUtils {
	
	public static String genBaseResultMap(Class clazz){
		 List fieldList = BeanUtil.getBeanPropertyList(clazz);
		 StringBuffer sb = new StringBuffer();
		 sb.append("\r\n");
		 int index = 0;
		 for (String fields : fieldList) {
			  String[] fieldArr = fields.split("`");
			  String columnStr = toColumn(fieldArr[1]);
			  if(index==0){
				  sb.append("  \r\n");
			  }else{
				  String jdbcType = "varchar";
				  if(fieldArr[0].equals("int")||fieldArr[0].equals("Integer")){
					  jdbcType = "Integer";
				  }else if(fieldArr[0].equals("long")||fieldArr[0].equals("Long")){
					  jdbcType = "bigInt";
				  }else if(fieldArr[0].equals("Date")||fieldArr[1].contains("Time")||fieldArr[1].contains("Date")){
					  jdbcType = "TIMESTAMP";
				  }
				  sb.append("  \r\n");
			  }
			  index++;
		 }
		 sb.append("\r\n");
		 return sb.toString();
	}
	
	//生成删除语句
	public static String genDeleteMap(String tableNamePreFix,Class clazz){
		 List fieldList = BeanUtil.getBeanPropertyList(clazz);
		 String tableName =(tableNamePreFix+toColumn(clazz.getSimpleName())).toUpperCase();
		 StringBuffer sb = new StringBuffer();
		 sb.append("\r\n");
		 sb.append("   delete from "+tableName+"  where id = #{id,jdbcType=BIGINT}\r\n");
		 sb.append("\r\n");
		 return sb.toString();
	}
	
	//生成插入语句
	public static String genInsertMap(String tableNamePreFix,Class clazz){
		 List fieldList = BeanUtil.getBeanPropertyList(clazz);
		 String tableName =(tableNamePreFix+toColumn(clazz.getSimpleName())).toUpperCase();
		 StringBuffer sb = new StringBuffer();
		 sb.append("\r\n");
		 StringBuffer columnAppend = new StringBuffer();
		 StringBuffer valuesAppend = new StringBuffer();
		 int index = 0;
		 String idFields = "";
		 for (String fields : fieldList) {
			  String[] fieldArr = fields.split("`");
			  if(index>0){
				  String jdbcType = "varchar";
				  if(fieldArr[0].equals("int")||fieldArr[0].equals("Integer")){
					  jdbcType = "Integer";
				  }else if(fieldArr[0].equals("long")||fieldArr[0].equals("Long")){
					  jdbcType = "bigInt";
				  }else if(fieldArr[0].equals("Date")||fieldArr[1].contains("Time")||fieldArr[1].contains("Date")){
					  jdbcType = "TIMESTAMP";
				  }
				  columnAppend.append("    "+toColumn(fieldArr[1])+",\r\n");
				  valuesAppend.append("    #{"+fieldArr[1]+",jdbcType="+jdbcType.toUpperCase()+"},\r\n");
			  }else{
				  idFields = fieldArr[1];
			  }
			  index++;
		 }
		 columnAppend.deleteCharAt(columnAppend.lastIndexOf(","));
		 valuesAppend.deleteCharAt(valuesAppend.lastIndexOf(","));
		 sb.append("  insert into "+tableName+"("+columnAppend.toString()+") values("+valuesAppend+")\r\n");
		 sb.append("\r\n");
		 return sb.toString();
	}
	
	//生成更新语句
	public static String genUpdateMap(String tableNamePreFix,Class clazz){
		 List fieldList = BeanUtil.getBeanPropertyList(clazz);
		 String tableName =(tableNamePreFix+toColumn(clazz.getSimpleName())).toUpperCase();
		 StringBuffer sb = new StringBuffer();
		 sb.append("\r\n");
		 StringBuffer setAppend = new StringBuffer();
		 sb.append("  update "+tableName+" set\r\n");
		 int index = 0;
		 String idFields = "";
		 for (String fields : fieldList) {
			  String[] fieldArr = fields.split("`");
			  if(index>0){
				  String jdbcType = "varchar";
				  if(fieldArr[0].equals("int")||fieldArr[0].equals("Integer")){
					  jdbcType = "Integer";
				  }else if(fieldArr[0].equals("long")||fieldArr[0].equals("Long")){
					  jdbcType = "bigInt";
				  }else if(fieldArr[0].equals("Date")||fieldArr[1].contains("Time")||fieldArr[1].contains("Date")){
					  jdbcType = "TIMESTAMP";
				  }
				  String column = toColumn(fieldArr[1]);
				  setAppend.append("    "+column+" = #{"+fieldArr[1]+",jdbcType="+jdbcType.toUpperCase()+"},\r\n");
			  }else{
				  idFields = fieldArr[1];
			  }
		      index++;
		 }
		 setAppend.deleteCharAt(setAppend.lastIndexOf(","));
		 sb.append(setAppend.toString());
		 sb.append("  where "+toColumn(idFields)+" = #{"+idFields+",jdbcType=BIGINT}\r\n");
		 sb.append("\r\n");
		 return sb.toString();
	}
	
	//生成更新语句
	public static String genSelectByIdMap(String tableNamePreFix,Class clazz){
		 List fieldList = BeanUtil.getBeanPropertyList(clazz);
		 String tableName =(tableNamePreFix+toColumn(clazz.getSimpleName())).toUpperCase();
		 StringBuffer sb = new StringBuffer();
		 sb.append("\r\n");
		 return sb.toString();
	}
		
	
	public static String toColumn(String word) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < word.length(); i++) {
			char c = word.charAt(i);
			if (Character.isUpperCase(c)) {
				sb.append("_"+c);
			}else{
				sb.append(c);
			}
		}
		return sb.toString().toLowerCase();
	}
	
	public static void main(String[] args) {
		System.out.println(genBaseResultMap(LoanTransferDto.class));
		System.out.println(genDeleteMap("JGJ",LoanTransferDto.class));
		System.out.println(genInsertMap("JGJ",LoanTransferDto.class));
		System.out.println(genUpdateMap("JGJ",LoanTransferDto.class));
		System.out.println(genSelectByIdMap("JGJ",LoanTransferDto.class));
		//System.out.println(toColumn("loanType"));
	}

}

 

 

生成后的效果:

 


 
 
 
 
 
 
 
 
 
 
 
 

 


   delete from JGJ_LOAN_TRANSFER_DTO  where id = #{id,jdbcType=BIGINT}

 


  insert into JGJ_LOAN_TRANSFER_DTO(    mid,
    loan_no,
    title,
    bid_count,
    bid_amount,
    rate,
    transfer_amount,
    loan_cycle,
    loan_cycle_attr,
    update_time,
    create_time
) values(    #{mid,jdbcType=BIGINT},
    #{loanNo,jdbcType=VARCHAR},
    #{title,jdbcType=VARCHAR},
    #{bidCount,jdbcType=BIGINT},
    #{bidAmount,jdbcType=BIGINT},
    #{rate,jdbcType=BIGINT},
    #{transferAmount,jdbcType=BIGINT},
    #{loanCycle,jdbcType=BIGINT},
    #{loanCycleAttr,jdbcType=INTEGER},
    #{updateTime,jdbcType=TIMESTAMP},
    #{createTime,jdbcType=TIMESTAMP}
)

 


  update JGJ_LOAN_TRANSFER_DTO set
    mid = #{mid,jdbcType=BIGINT},
    loan_no = #{loanNo,jdbcType=VARCHAR},
    title = #{title,jdbcType=VARCHAR},
    bid_count = #{bidCount,jdbcType=BIGINT},
    bid_amount = #{bidAmount,jdbcType=BIGINT},
    rate = #{rate,jdbcType=BIGINT},
    transfer_amount = #{transferAmount,jdbcType=BIGINT},
    loan_cycle = #{loanCycle,jdbcType=BIGINT},
    loan_cycle_attr = #{loanCycleAttr,jdbcType=INTEGER},
    update_time = #{updateTime,jdbcType=TIMESTAMP},
    create_time = #{createTime,jdbcType=TIMESTAMP}
  where ltid = #{ltid,jdbcType=BIGINT}

 

 

你可能感兴趣的:(java工具类)