您的位置:首页 > 健康 > 养生 > 武汉企业专属空间_360seo优化_营销软文代写_全媒体广告加盟

武汉企业专属空间_360seo优化_营销软文代写_全媒体广告加盟

2024/10/6 0:31:40 来源:https://blog.csdn.net/Syals/article/details/142489602  浏览:    关键词:武汉企业专属空间_360seo优化_营销软文代写_全媒体广告加盟
武汉企业专属空间_360seo优化_营销软文代写_全媒体广告加盟

前言

在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>

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com