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

作者:cuisuqiang 时间:2021-08-30 07:17:40 

虽然项目中都夹杂了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<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);
}

来源:https://www.iteye.com/blog/cuisuqiang-1926915

标签:JDBC,查询,Map,对象
0
投稿

猜你喜欢

  • Android开发实现查询远程服务器的工具类QueryUtils完整实例

    2021-11-06 14:57:10
  • Android自定义状态栏颜色与应用标题栏颜色一致

    2022-01-12 02:24:31
  • c# 使用模式匹配以及 is 和 as 运算符安全地进行强制转换

    2022-11-23 09:47:51
  • Java实例讲解多态数组的使用

    2021-08-30 19:34:46
  • android教程之把自己的应用加入到系统分享中

    2022-05-13 11:38:41
  • 本地jvm执行flink程序带web ui的操作

    2022-09-03 20:49:00
  • Android XML設置屏幕方向(android:screenOrientation)详解

    2021-09-08 09:46:35
  • 基于java实现斗地主代码实例解析

    2023-09-07 00:31:15
  • Android仿一点资讯收藏Toast动画效果

    2022-01-15 18:42:33
  • 如何使用HttpClient发送java对象到服务器

    2022-10-29 06:45:51
  • android studio 3.0 升级 项目遇到的问题及更改思路(问题小结)

    2021-06-14 17:19:16
  • Entity Framework主从表数据加载方式

    2022-03-10 21:44:14
  • 使用IntelliJ IDEA 15和Maven创建Java Web项目(图文)

    2023-08-26 07:10:15
  • 详解如何利用C#实现设置系统时间

    2023-09-04 13:55:05
  • Android自定义有限制区域图例角度自识别涂鸦工具类中篇

    2021-06-16 16:21:46
  • 一文详解Java中流程控制语句

    2023-11-26 11:39:49
  • Java @Deprecated注解的作用及传递性

    2023-08-11 12:55:05
  • 详细解读Java Spring AOP

    2022-10-09 11:06:06
  • Android运动健康睡眠自定义控件的实现

    2021-07-17 22:52:35
  • Java Calendar类使用案例详解

    2023-07-09 14:03:22
  • asp之家 软件编程 m.aspxhome.com