JDBC查询Map转对象实现过程详解

网友投稿 246 2022-11-15


JDBC查询Map转对象实现过程详解

虽然项目中都夹杂了Hibernate的支持,但是团队开发中,很多人为了编写特殊查询的代码时都使用了JDBC进行查询。JDBC查询后返回的是一个List集合,List中组装的是Map,一个Map就是一个对应的对象。但是接口不能直接返回Map,都是返回的对象,以方便自己和其他人使用,为了转换这个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 para){

Object obj = this;

ObjectUtil.setValToObj(obj, para);

return obj;

}

公用方法:

public synchronized static void setValToObj(Object entityName, Map para){

try {

Class c = entityName.getClass();

// 获得对象属性

Field field[] = c.getDeclaredFields();

for (Fieldhttp:// 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 getProAndValMap(Object entityName) {

LinkedHashMap map = new LinkedHashMap();

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;

}

/**

* 获得对象属性的值

*EugshCrBU/

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);

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:JDBC PreparedStatement Like参数报错解决方案
下一篇:Oracle JDBC连接BUG解决方案
相关文章

 发表评论

暂时没有评论,来抢沙发吧~