package mysql.tool; import java.util.List; import mysql.model.LoanTransferDto; public class MapperUtils { public static String genBaseResultMap(Class<?> clazz){ List<String> fieldList = BeanUtil.getBeanPropertyList(clazz); StringBuffer sb = new StringBuffer(); sb.append("<resultMap id=\"BaseResultMap\" type=\""+clazz.getSimpleName().toUpperCase()+"\">\r\n"); int index = 0; for (String fields : fieldList) { String[] fieldArr = fields.split("`"); String columnStr = toColumn(fieldArr[1]); if(index==0){ sb.append(" <id column=\""+columnStr+"\" jdbcType=\"BIGINT\" property=\""+fieldArr[1]+"\" />\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(" <result column=\""+columnStr+"\" jdbcType=\""+jdbcType.toUpperCase()+"\" property=\""+fieldArr[1]+"\" />\r\n"); } index++; } sb.append("</resultMap>\r\n"); return sb.toString(); } //生成删除语句 public static String genDeleteMap(String tableNamePreFix,Class<?> clazz){ List<String> fieldList = BeanUtil.getBeanPropertyList(clazz); String tableName =(tableNamePreFix+toColumn(clazz.getSimpleName())).toUpperCase(); StringBuffer sb = new StringBuffer(); sb.append("<delete id=\"deleteByPrimaryKey\" parameterType=\"java.lang.Long\">\r\n"); sb.append(" delete from "+tableName+" where id = #{id,jdbcType=BIGINT}\r\n"); sb.append("</delete>\r\n"); return sb.toString(); } //生成插入语句 public static String genInsertMap(String tableNamePreFix,Class<?> clazz){ List<String> fieldList = BeanUtil.getBeanPropertyList(clazz); String tableName =(tableNamePreFix+toColumn(clazz.getSimpleName())).toUpperCase(); StringBuffer sb = new StringBuffer(); sb.append("<insert id=\"insert\" parameterType=\""+clazz.getSimpleName().toUpperCase()+"\">\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("</insert>\r\n"); return sb.toString(); } //生成更新语句 public static String genUpdateMap(String tableNamePreFix,Class<?> clazz){ List<String> fieldList = BeanUtil.getBeanPropertyList(clazz); String tableName =(tableNamePreFix+toColumn(clazz.getSimpleName())).toUpperCase(); StringBuffer sb = new StringBuffer(); sb.append("<update id=\"updateByPrimaryKey\" parameterType=\""+clazz.getSimpleName().toUpperCase()+"\">\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("</update>\r\n"); return sb.toString(); } //生成更新语句 public static String genSelectByIdMap(String tableNamePreFix,Class<?> clazz){ List<String> fieldList = BeanUtil.getBeanPropertyList(clazz); String tableName =(tableNamePreFix+toColumn(clazz.getSimpleName())).toUpperCase(); StringBuffer sb = new StringBuffer(); sb.append("<select id=\"selectByPrimaryKey\" parameterType=\"java.lang.Long\" resultMap=\"BaseResultMap\">\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(" select "+columnAppend.toString()+" from "+tableName+" "); sb.append(" where "+toColumn(idFields)+" = #{"+idFields+",jdbcType=BIGINT}\r\n"); sb.append("</select>\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")); } }
生成后的效果:
<resultMap id="BaseResultMap" type="LOANTRANSFERDTO">
<id column="ltid" jdbcType="BIGINT" property="ltid" />
<result column="mid" jdbcType="BIGINT" property="mid" />
<result column="loan_no" jdbcType="VARCHAR" property="loanNo" />
<result column="title" jdbcType="VARCHAR" property="title" />
<result column="bid_count" jdbcType="BIGINT" property="bidCount" />
<result column="bid_amount" jdbcType="BIGINT" property="bidAmount" />
<result column="rate" jdbcType="BIGINT" property="rate" />
<result column="transfer_amount" jdbcType="BIGINT" property="transferAmount" />
<result column="loan_cycle" jdbcType="BIGINT" property="loanCycle" />
<result column="loan_cycle_attr" jdbcType="INTEGER" property="loanCycleAttr" />
<result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
<result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
</resultMap>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
delete from JGJ_LOAN_TRANSFER_DTO where id = #{id,jdbcType=BIGINT}
</delete>
<insert id="insert" parameterType="LOANTRANSFERDTO">
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}
)
</insert>
<update id="updateByPrimaryKey" parameterType="LOANTRANSFERDTO">
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}
</update>
<select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
select mid,
loan_no,
title,
bid_count,
bid_amount,
rate,
transfer_amount,
loan_cycle,
loan_cycle_attr,
update_time,
create_time
from JGJ_LOAN_TRANSFER_DTO where ltid = #{ltid,jdbcType=BIGINT}
</select>