需求:当我们使用mybatis的时候可能要手动生成很多map去接收数据库查出来的值,大量重复的操作,浪费时间!!!
此处写了个自动生成mapperMap的代码,供大家分享!
public class MapperBuildUtil {
public static void main(String[] args) {
/**
* 调用这个方法传入 对象的class对象,生成对应的mapperMap
*/
buildMapper(User.class);
}
public static void buildMapper(Class classz) {
String name = classz.getSimpleName();
name = name.substring(0, 1).toLowerCase() + name.substring(1, name.length());
StringBuffer buffer = new StringBuffer();
Field[] fields = classz.getDeclaredFields();
buffer.append("<resultMap id=" + "\"" + name + "Map\"" + " type=" + "\"" + name + "\"" + ">").append("\n")
.append("\t");
for (Field field : fields) {
String typeName = field.getType().getTypeName();
String filename = field.getName();
if ("java.lang.String".equals(typeName)) {
buffer.append("<result column=" + "\"" + filename + "\"" + " property=" + "\"" + filename + "\""
+ " jdbcType=" + "\"" + "VARCHAR" + "\"" + " />").append("\n").append("\t");
}
if ("java.math.BigDecimal".equals(typeName)) {
buffer.append("<result column=" + "\"" + filename + "\"" + " property=" + "\"" + filename + "\""
+ " jdbcType=" + "\"" + "NUMERIC" + "\"" + " />").append("\n").append("\t");
}
if ("java.lang.Boolean".equals(typeName) || "boolean".equals(typeName)) {
buffer.append("<result column=" + "\"" + filename + "\"" + " property=" + "\"" + filename + "\""
+ " jdbcType=" + "\"" + "BOOLEAN" + "\"" + " />").append("\n").append("\t");
}
if ("java.lang.Long".equals(typeName)) {
buffer.append("<result column=" + "\"" + filename + "\"" + " property=" + "\"" + filename + "\""
+ " jdbcType=" + "\"" + "BIGINT" + "\"" + " />").append("\n").append("\t");
}
if ("java.lang.Float".equals(typeName) || "float".equals(typeName)) {
buffer.append("<result column=" + "\"" + filename + "\"" + " property=" + "\"" + filename + "\""
+ " jdbcType=" + "\"" + "FLOAT" + "\"" + " />").append("\n").append("\t");
}
if ("java.lang.Double".equals(typeName) || "double".equals(typeName)) {
buffer.append("<result column=" + "\"" + filename + "\"" + " property=" + "\"" + filename + "\""
+ " jdbcType=" + "\"" + "DOUBLE" + "\"" + " />").append("\n").append("\t");
}
if ("java.sql.Date".equals(typeName)) {
buffer.append("<result column=" + "\"" + filename + "\"" + " property=" + "\"" + filename + "\""
+ " jdbcType=" + "\"" + "DATE" + "\"" + " />").append("\n").append("\t");
}
if ("java.sql.Timestamp".equals(typeName)) {
buffer.append("<result column=" + "\"" + filename + "\"" + " property=" + "\"" + filename + "\""
+ " jdbcType=" + "\"" + "TIMESTAMP" + "\"" + " />").append("\n").append("\t");
}
if ("java.sql.Clob".equals(typeName)) {
buffer.append("<result column=" + "\"" + filename + "\"" + " property=" + "\"" + filename + "\""
+ " jdbcType=" + "\"" + "CLOB" + "\"" + " />").append("\n").append("\t");
}
if ("java.lang.Integer".equals(typeName) || "int".equals(typeName)) {
buffer.append("<result column=" + "\"" + filename + "\"" + " property=" + "\"" + filename + "\""
+ " jdbcType=" + "\"" + "INTEGER" + "\"" + " />").append("\n").append("\t");
}
// 未完待续......
}
buffer.append("</resultMap>");
System.out.println(buffer.toString());
}
}
用法:很简单
只需要传入要生成的实体的class对象,点击运行,即可生成mapperMap