使用ResultMap避免因使用@Results产生的过多的冗余代码。

@Select({"select id, name, class_id from my_student where id = #{id}"})
@ResultMap(value="studentMap")
Student selectById(integer id);

附带生成结果集代码工具类

package com.gxkj.util;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import com.gxkj.po.extended.ReturnRecordsOfConsumption;
 
/**
 * @author 石嘉懿
 * @version 0.1
 * Restmap生成工具类
 * 
 * 
 * **/
public class RestmapUtil {
	 
	public static void getRestMap(Class<? extends Object> class1) throws Exception {
		/*要生成的类*/
		System.out.println(getResultMapNew(class1));
		System.out.println("石嘉懿博客:www.shijiayi.top");
	}
	
	
	public static void main(String[] args) {
		ReturnRecordsOfConsumption rrr = new ReturnRecordsOfConsumption();
		try {
			System.out.println(getResultMapNew(rrr.getClass()));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	// 获取bean的属性 根据属性评价 resultMap
	private static String getResultMap(Class<?> cls) throws Exception {
		String str = "";
		// 每一行字符串 <result column="BID_SECTION_CODE" property="BID_SECTION_CODE"
		// jdbcType="VARCHAR" />
		String linestr = "";
		Field[] declaredFields = cls.getDeclaredFields();
		for (Field field : declaredFields) {
			if (field.getType().getName().equals("java.lang.String")) {
				linestr = "<result column=\"" + field.getName() + "\" property=\"" + field.getName()
						+ "\" jdbcType=\"VARCHAR\" />";
			} else {
				linestr = "<result column=\"" + field.getName() + "\" property=\"" + field.getName()
						+ "\" jdbcType=\"INTEGER\" />";
			}
			System.out.println(linestr);
		}
 
		return str;
	}
 
	// 获取bean的属性 根据属性评价 resultMap
	// 并将驼峰修改为'_'
	private static String getResultMapNew(Class<?> cls) throws Exception {
		String str = "";
		// 头部 <resultMap id="BaseResultMap" type="com.huajie.entity.sys.SysMenuinfo">
		str = "<resultMap id=\"" + cls.getSimpleName() + "ResultMap\" type=\"" + cls.getName() + "\"> \r\n";
		// 每一行字符串
		String linestr = "";
		Field[] declaredFields = cls.getDeclaredFields();
		for (Field field : declaredFields) {
			if (field.getType().getName().equals("java.lang.String")) {
				linestr = "<result column=\"" + getUpCaseReplace(field.getName()) + "\" property=\"" + field.getName()
						+ "\" jdbcType=\"VARCHAR\" />";
			} else {
				linestr = "<result column=\"" + getUpCaseReplace(field.getName()) + "\" property=\"" + field.getName()
						+ "\" jdbcType=\"INTEGER\" />";
			}
			linestr += "\r\n";
			str += linestr;
		}
		str+="</resultMap>";
		return str;
	}
 
	// 获取Base_Column_List sql语句字段
	private static String getColumnList(Class<?> cls) throws Exception {
		// 每一行字符串 <result column="BID_SECTION_CODE" property="BID_SECTION_CODE"
		// jdbcType="VARCHAR" />
		String linestr = "";
		Field[] declaredFields = cls.getDeclaredFields();
		for (Field field : declaredFields) {
			linestr = linestr + field.getName() + ",";
		}
		String str = linestr.substring(0, linestr.length() - 1);
		System.out.println(str);
		return str;
	}
 
	/**
	 * 将字符串中的驼峰写法替换成'_'
	 * 
	 * @param str
	 * @return
	 */
	private static String getUpCaseReplace(String str) {
		List<String> listChar = getUpCaseList(str);
		for (int i = 0; i < listChar.size(); i++) {
			str = str.replace(listChar.get(i), "_" + listChar.get(i).toLowerCase());
		}
		return str;
	}
 
	/**
	 * @Description: 输出字符串中的大写字母
	 * @param str
	 */
	private static List<String> getUpCaseList(String str) {
		List<String> listChar = new ArrayList<String>();
		// 转为char数组
		char[] ch = str.toCharArray();
		// 得到大写字母
		for (int i = 0; i < ch.length; i++) {
			if (ch[i] >= 'A' && ch[i] <= 'Z') {
				listChar.add(String.valueOf(ch[i]));
			}
		}
		return listChar;
	}
 
	/**
	 * @Description: 输出字符串中的大写字母
	 * @param str
	 */
	private static String getColumnListNew(Class<?> cls) throws Exception {
		// 每一行字符串 <result column="BID_SECTION_CODE" property="BID_SECTION_CODE"
		// jdbcType="VARCHAR" />
		String linestr = "";
		Field[] declaredFields = cls.getDeclaredFields();
		for (Field field : declaredFields) {
			linestr = linestr + getUpCaseReplace(field.getName()) + ",";
		}
		String str = linestr.substring(0, linestr.length() - 1);
		System.out.println(str);
		return str;
	}

}

 

最后修改于 2019-12-16 11:33:42
如果觉得我的文章对你有用,请随意赞赏
扫一扫支付
上一篇