本篇文章为大家展示了JDBC如何实现查询Map转对象,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
@SuppressWarnings("unchecked")
public static MS_Mont analyzeMapToMS_Mont(Map map){
MS_Mont obj = new MS_Mont();
if(null != map.get("montNo")) obj.setMontNo(Integer.parseInt(map.get("montNo").toString()));
if(null != map.get("montName")) obj.setMontName(map.get("montName").toString());
if(null != map.get("montType")) obj.setMontType(Integer.parseInt(map.get("montType").toString()));
if(null != map.get("montLength")) obj.setMontLength(Integer.parseInt(map.get("montLength").toString()));
if(null != map.get("montDesc")) obj.setMontDesc(map.get("montDesc").toString());
if(null != map.get("bigType")) obj.setBigType(Integer.parseInt(map.get("bigType").toString()));
if(null != map.get("bigTypeName")) obj.setBigTypeName(map.get("bigTypeName").toString());
if(null != map.get("littleType")) obj.setLittleType(Integer.parseInt(map.get("littleType").toString()));
if(null != map.get("littleTypeName")) obj.setLittleTypeName(map.get("littleTypeName").toString());
if(null != map.get("insertTime")) obj.setInsertTime(map.get("insertTime").toString());
if(null != map.get("updateTime")) obj.setUpdateTime(map.get("updateTime").toString());
if(null != map.get("userNoRe")) obj.setUserNoRe(Integer.parseInt(map.get("userNoRe").toString()));
if(null != map.get("userNoLast")) obj.setUserNoLast(Integer.parseInt(map.get("userNoLast").toString()));
return obj;
}
很麻烦,很多,很枯燥。
为了解决这个问题,我列出一个解决方法,写一个方法,传入要赋值的对象和Map,然后根据列的属性名称从Map中获得响应的值,然后赋值给这个对象的属性。
例如,这里写了一个简单的查询:
public CM_Line getObjectBean(int lineNo) {
try {
String sql = "select * from cm_line where lineNo=?";
Object[] obj = new Object[]{ lineNo };
List rows = jdbcTemplate.queryForList( sql, obj );
if(null != rows && rows.size() > 0) {
CM_Line line = new CM_Line();
return (CM_Line) line.analyzeMap((Map)rows.get(0));
} else {
return null;
}
} catch (Exception e) {
logger.error(e);
}
return null;
}
然后我们调用了他的analyzeMap方法,这个方法把当前对象当作要赋值的对象,然后调用公用方法进行组装:
public Object analyzeMap(Map<String, Object> para){
Object obj = this;
ObjectUtil.setValToObj(obj, para);
return obj;
}
公用方法:
public synchronized static void setValToObj(Object entityName, Map<String, Object> para){
try {
Class c = entityName.getClass();
// 获得对象属性
Field field[] = c.getDeclaredFields();
for (Field f : field) {
try {
PropertyDescriptor pd = new PropertyDescriptor(f.getName(), c);
Method writeMethod = pd.getWriteMethod();
if(!CommonCheck.isNullOrEmpty(para.get(f.getName())))
writeMethod.invoke(entityName, para.get(f.getName()));
} catch (Exception e) {
}
}
} catch (Exception e) {
}
}
下面就有人说了,那根据对象获得这个对象的Map怎么搞,这个之前已经写过了,不这里仍然把代码放一下:
/**
* 返回一个对象的属性和属性值
*/
public synchronized static LinkedHashMap<String,String> getProAndValMap(Object entityName) {
LinkedHashMap<String,String> map = new LinkedHashMap<String, String>();
try {
Class c = entityName.getClass();
// 获得对象属性
Field field[] = c.getDeclaredFields();
for (Field f : field) {
Object v = invokeMethod(entityName, f.getName(), null);
if(null != v) map.put(f.getName(), v.toString());
else map.put(f.getName(), "");
}
} catch (Exception e) {
map = null;
}
return map;
}
/**
* 获得对象属性的值
*/
private synchronized static Object invokeMethod(Object owner, String methodName,
Object[] args) throws Exception {
Class ownerClass = owner.getClass();
methodName = methodName.substring(0, 1).toUpperCase() + methodName.substring(1);
Method method = null;
try {
method = ownerClass.getMethod("get" + methodName);
} catch (Exception e) {
}
return method.invoke(owner);
}
上述内容就是JDBC如何实现查询Map转对象,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。