前言
在Java中经常在XML中通过SQL语句来实现一对多查询的(通过<collection></collection>
),对于自定义的实体类,肯定无法避免去在XML文件中去手写<resultMap>
里面的映射内容,通过下方工具类,可以一键生成指定类的所有属性以及对应的数据库字段映射(默认按小驼峰生成)
工具类分享
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;/*** @author zhu* @date 2024/9/24 17:05*/
public class GenerateResultMap {private static Pattern humpPattern = Pattern.compile("[A-Z]");//匹配大写字母的正则private static final Map<String, String> TYPE_MAP = new HashMap<>();//java类型和jdbc类型的对应关系static {//以下对应规则可根据自己的实际情况做修改TYPE_MAP.put("string", "VARCHAR");TYPE_MAP.put("boolean", "BIT");TYPE_MAP.put("byte", "TINYINT");TYPE_MAP.put("short", "SMALLINT");TYPE_MAP.put("integer", "INTEGER");TYPE_MAP.put("long", "BIGINT");TYPE_MAP.put("float", "REAL");TYPE_MAP.put("double", "DOUBLE");TYPE_MAP.put("date", "DATE");//TIMESTAMP,DATETIMETYPE_MAP.put("timestamp", "TIMESTAMP");TYPE_MAP.put("time", "TIME");TYPE_MAP.put("bigdecimal", "DECIMAL");}/*** 生成ResultMap** @param clazz 实体类的Class* @return String*/public static String generate(Class<?> clazz) {String pkgName = clazz.getName();String clazzName = clazz.getSimpleName();String resultMapId = clazzName + "Result";StringBuilder resultMap = new StringBuilder();resultMap.append("<resultMap type=\"");resultMap.append(pkgName);
// resultMap.append(clazzName);resultMap.append("\" id=\"");resultMap.append(resultMapId);resultMap.append("\">\n");Field[] fields = clazz.getDeclaredFields();for (Field f : fields) {String property = f.getName();String javaType = f.getType().getSimpleName();if ("serialVersionUID".equals(property)) {continue;//忽略掉这个属性}resultMap.append(" <result property=\"");resultMap.append(property);
// resultMap.append("\" jdbcType=\"");
// resultMap.append(javaType2jdbcType(javaType.toLowerCase()));resultMap.append("\" column=\"");resultMap.append(property2Column(property));resultMap.append("\" />\n");}resultMap.append("</resultMap>");return resultMap.toString();}//驼峰转下划线命名private static String property2Column(String property) {Matcher matcher = humpPattern.matcher(property);StringBuffer sb = new StringBuffer();while (matcher.find()) {matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());}matcher.appendTail(sb);return sb.toString();}//通过java类型获取jdbc类型private static String javaType2jdbcType(String javaType) {String jdbcType = TYPE_MAP.get(javaType);return jdbcType == null ? "UNKNOWN" : jdbcType;}public static void main(String[] args) {String resultMap = GenerateResultMap.generate(MemberResp.class);System.out.println(resultMap);}
在代码中main()
方法中指定要生成的类,运行main方法即可。
测试案例
需要生成的实体类
import com.alibaba.excel.annotation.ExcelProperty;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;/*** 群成员响应实体*/
@Data
public class MemberResp {@Schema(description = "成员名称", example = "张三")@ExcelProperty("成员名称")private String memberName;@Schema(description = "手机号", example = "张三")@ExcelProperty("手机号")private String phone;@Schema(description = "性别", example = "张三")@ExcelProperty("性别")private String sex;@Schema(description = "群成员id", example = "张三")@ExcelProperty("群成员id")private Long memberUserId;
}
生成后的结果
<resultMap type="com.xxx.MemberResp" id="MemberRespResult"><result property="memberName" column="member_name" /><result property="phone" column="phone" /><result property="sex" column="sex" /><result property="memberUserId" column="member_user_id" />
</resultMap>