自动生成Mapper代码



 1 public class BeanMapperTest {
 2     @Test
 3     public void build() throws Exception {
 4         Class clazz = RiskAccess.class;//把  RiskAccess替换成自己对应的model即可
 5         System.out.println("public final static RowMapper<#ClassName> #ClassName_MAPPER = (rs, rowNum) -> {".replace("#ClassName", clazz.getSimpleName()));
 6         System.out.println("#ClassName dto = new #ClassName();".replace("#ClassName", clazz.getSimpleName()));
 7         for (java.lang.reflect.Method method : clazz.getMethods()) {
 8             String methodName = method.getName();
 9             String fieldName = methodName.replace("set", "");
10             if (methodName.contains("set")) {
11                 Class paramType = method.getParameters()[0].getType();
12                 System.out.println("dto.#MethodName(rs.get#ParamType(\"#FieldName\"));"
13                         .replace("#MethodName", methodName)
14                         .replace("#ParamType", paramType.getSimpleName())
15                         .replace("#FieldName", fieldName));
16             }
17         }
18         System.out.println("return dto;");
19         System.out.println("};");
20     }
21 }

 注意:如果表的字段名称有下划线,而对应POJO的字段没有下划线,则需要手动加上下划线,并且mapper只支持getInt()方法 ,而不支持getInteger()方法

posted @ 2017-02-17 16:51  戴眼镜的蚂蚁  阅读(417)  评论(0编辑  收藏  举报