我的漫漫程序之旅

          專注于JavaWeb開發
          隨筆 - 39, 文章 - 310, 評論 - 411, 引用 - 0
          數據加載中……

          Spring jdbc 對象Mapper的簡單封裝

          一般查詢實體的時候,都需要這么使用:
          /**
               * 根據id查詢
               * 
               * 
          @return
               
          */

              
          public Emp queryEmpById(Integer id)
              
          {
                  String sql 
          = "select * from emp where empno = ?";
                  ParameterizedRowMapper
          <Emp> mapper = new ParameterizedRowMapper<Emp>()
                  
          {

                      
          public Emp mapRow(ResultSet rs, int rowNum) throws SQLException
                      
          {
                          Emp emp 
          = new Emp();
                          System.out.println(
          "row:" + rowNum);
                          emp.setEmpno(rs.getInt(
          "empno"));
                          emp.setEname(rs.getString(
          "ename"));
                          
          return emp;
                      }

                  }
          ;

                  
          return this.getSimpleJdbcTemplate().queryForObject(sql, mapper, id);
              }

          能不能像Hibernate那樣自動set這些值呢,用反射可以實現.

          package orm;

          import java.lang.reflect.Field;
          import java.sql.ResultSet;
          import java.sql.SQLException;
          import org.springframework.jdbc.core.simple.ParameterizedRowMapper;

          /**
           * 通用的Object包裝類(類型問題,依然是個瓶頸,如果有好的解決方案請pm我)
           * 
           * 功能:查詢對象類型或對象集合時的通用包裝類
           * 
           * 
          @author zdw
           * 
           
          */

          @SuppressWarnings(
          "unchecked")
          public class ObjectMapper implements ParameterizedRowMapper
          {
              
          private Class clazz;

              
          public ObjectMapper(Class clazz)
              
          {
                  
          this.clazz = clazz;
              }


              
          /**
               * 重寫mapRow方法
               
          */

              @Override
              
          public Object mapRow(ResultSet rs, int rowNum) throws SQLException
              
          {
                  
          try
                  
          {
                      Object obj 
          = clazz.newInstance();
                      Field fields[] 
          = obj.getClass().getDeclaredFields();
                      
          for (int i = 0; i < fields.length; i++)
                      
          {
                          Field field 
          = fields[i];
                          
          // 暴力訪問
                          field.setAccessible(true);
                          
          this.typeMapper(field, obj, rs);
                          
          // 恢復默認
                          field.setAccessible(false);
                      }

                      
          return obj;
                  }

                  
          catch (Exception e)
                  
          {
                      e.printStackTrace();
                  }

                  
          return null;
              }


              
          /**
               * 數據類型包裝器
               * 
               * 
          @param field
               *            目標屬性
               * 
          @param obj
               *            目標對象
               * 
          @param rs
               *            結果集
               * 
          @throws Exception
               
          */

              
          private void typeMapper(Field field, Object obj, ResultSet rs)
                      
          throws Exception
              
          {
                  String type 
          = field.getType().getName();
                  
          if (type.equals("java.lang.String"))
                  
          {
                      field.set(obj, rs.getString(field.getName()));
                  }

                  
          else if (type.equals("int"|| type.equals("java.lang.Integer"))
                  
          {
                      field.set(obj, rs.getInt(field.getName()));
                  }

                  
          else if (type.equals("long"|| type.equals("java.lang.Long"))
                  
          {
                      field.set(obj, rs.getLong(field.getName()));
                  }

                  
          else if (type.equals("boolean"|| type.equals("java.lang.Boolean"))
                  
          {
                      field.set(obj, rs.getBoolean(field.getName()));
                  }

                  
          else if (type.equals("java.util.Date"))
                  
          {
                      field.set(obj, rs.getDate(field.getName()));
                  }

              }

          }



          dao:
          /**
               * 查詢操作 (自動setEmp類型所有值)
               * 
               * 
          @return
               
          */

              
          public List queryList()
              
          {
                  
          return this.getJdbcTemplate().query("select * from emp",
                          
          new ObjectMapper(Emp.class));
              }

          單個查詢:
          public Emp queryEmpById2(Integer id)
              
          {
                  String sql 
          = "select * from emp where empno = ?";
                  ObjectMapper om 
          = new ObjectMapper(Emp.class);
                  
          return (Emp) this.getSimpleJdbcTemplate().queryForObject(sql, om, id);
              }
          測試通過:
          7369
          7499
          7521
          7566
          7654
          7698
          7782
          7788
          7839
          7844

          上面是我的一個簡單封裝,在Spring2.5中及以后版本,已經提供了便捷方法:
          /**
               * 查詢操作 (自動setEmp類型所有值)
               * 
               * 
          @return
               
          */

              
          public List queryList()
              
          {
                  
          return this.getSimpleJdbcTemplate().query(   
                          
          "SELECT * from emp",   
                          ParameterizedBeanPropertyRowMapper.newInstance(Emp.
          class));  
              }

              
              
          /**
               * 根據id查詢
               * 
               * 
          @return
               
          */

              
          public Emp queryById(Integer id)
              
          {
                  
          return this.getSimpleJdbcTemplate().queryForObject(   
                          
          "SELECT * from emp where id = ?",   
                          ParameterizedBeanPropertyRowMapper.newInstance(Emp.
          class),7369);  
              }

          這樣就簡單多了,也是用反射實現的.


          posted on 2009-03-30 16:28 々上善若水々 閱讀(4348) 評論(1)  編輯  收藏

          評論

          # re: Spring jdbc 對象Mapper的簡單封裝  回復  更多評論   

          樓上說話請自重。我這是自己寫的。
          2009-04-13 08:34 | 々上善若水々

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 仁寿县| 莒南县| 宜宾市| 郁南县| 苗栗市| 江山市| 莒南县| 芮城县| 石家庄市| 逊克县| 紫云| 石柱| 灌阳县| 长汀县| 辽中县| 米易县| 庐江县| 武定县| 桃源县| 彰化市| 云和县| 富宁县| 明水县| 灵台县| 鄂尔多斯市| 库车县| 唐山市| 明溪县| 乐陵市| 江陵县| 百色市| 安化县| 洪湖市| 横峰县| 上犹县| 铁力市| 湟源县| 金堂县| 平阳县| 洪雅县| 通州市|