wilin
          wilin
          posts - 6,comments - 3,trackbacks - 0
          Insert/Update/Delete:
                  String sql = "insert into " + tableName
                          
          + "(id, ipproleid, cpermissionid, iisdelete)"
                          
          + " values(" + getSeqID(tableName) + ",?,?,?)";
                  Object[] values 
          = new Object[] { domain.getIpproleid(),
                          domain.getCpermissionid(),domain.getIisdelete() }
          ;
                  
          this.getJdbcTemplate().update(sql, values);

          Select:
                  String sql = "select * from " + tableName + " where cpermissionid=? and ipproleid=?";
                  Object[] values 
          = new Object[] { permissionID,pproleType };
                  List list 
          = this.getJdbcTemplate().query(sql, values,
                          
          new BaseRowMapper(M2PpuserPermission.class));
                  
          return (list == null || list.size() == 0 ? null : (M2PpuserPermission) list
                          .
          get(0));

          public final class BaseRowMapper implements RowMapper {

              
          private Class cls;
              
              
          /**
               * @param dao
               
          */

              
          public BaseRowMapper(Class cls) {
                  
          this.cls = cls;
              }


              
          public Object mapRow(ResultSet rs, int row) throws SQLException {
                  
          if (rs != null{
                      
          return RSUtil.toBean(rs, cls);
                  }
           else {
                      
          return null;
                  }

              }

          }


          批量更新:
                  final Object[] propValue = getColumnPropValue(domain);
                  final Object[] propName 
          = getColumnPropName();
                  String INSERTPROPSQL 
          = "insert into M_COMMON_COLUMN_PROP "
                      
          + " (icolumnid, cpropname, cpropvalue) values(?,?,?)";
                  getJdbcTemplate().batchUpdate(INSERTPROPSQL,
                          
          new BatchPreparedStatementSetter() {
                              
          public int getBatchSize() {
                                  
          return propName.length;
                              }


                              
          public void setValues(PreparedStatement ps, int i)
                                      throws SQLException 
          {
                                  ps.setInt(
          1, id);
                                  ps.setString(
          2,  propName[i].toString());
                                  ps.setString(
          3,  propValue[i].toString());
                              }

                          }
          );

          Clob/Blob:


          /**
           * @author YunHongTao
           
          */


          public class ExampleSpringLobDao extends JdbcDaoSupport {
              
              
          /**
               * Comment for <code>lobHandler</code>
               * 處理blob、clob字段的對象 提供了方便的處理方法 一定要先初始化 
               * 在配置文件可以如下指定
               *     <bean id="nativeJdbcExtractor"
               *             class="org.springframework.jdbc.support.nativejdbc.WebLogicNativeJdbcExtractor" lazy-init="true"/>
               *        <bean id="oracleLobHandler"
               *             class="org.springframework.jdbc.support.lob.OracleLobHandler" lazy-init="true">
               *         <property name="nativeJdbcExtractor"><ref local="nativeJdbcExtractor"/></property>
               *       </bean> 
               *       <bean id="ExampleSpringLobDao" class="test.ExampleSpringLobDao">     
               *             <property name="dataSource">
               *                 <ref local="dataSource"/>
               *             </property>
               *             <property name="lobHandler">
               *                 <ref bean="oracleLobHandler"/>
               *          </property>
               *         </bean>
               
          */

              
          private LobHandler lobHandler;
              

              
          private String text = "薩克雷發動機三卡領導及罰款司機阿東風口浪尖薩克路燈風紀扣拉薩定界符可濾色鏡阿斗罰款濾色鏡打伏擊送達可分離\n"
                      
          + "薩雷隊交鋒拉開三角楓框架薩克路風機司克拉風紀扣拉薩定界符可立即撒風口浪尖送達風\n"
                      
          + "口浪尖斯大林咖啡堿四克拉定界符可憐三大件罰款來三大件分厘卡三大件罰款拉薩定界符\n";


              
          /**插入clob字段的方法示例
               * @param id
               * @param name
               * @param text
               * @throws DataAccessException
               
          */

              
          public void insertClob(final int id, final String name, final String text)
                      throws DataAccessException 
          {
                  getJdbcTemplate().execute(
                          
          "INSERT INTO yuntest (id,name,text) VALUES (?, ?, ?)",
                          
          new AbstractLobCreatingPreparedStatementCallback(
                                  
          this.lobHandler) {
                              
          protected void setValues(PreparedStatement ps,
                                      LobCreator lobCreator) throws SQLException 
          {
                                  ps.setInt(
          1, id);
                                  ps.setString(
          2, name);
                                  lobCreator.setClobAsString(ps, 
          3, text);
                              }

                          }
          );
              }


              
          /**更新clob字段的方法示例
               * @param name
               * @param text
               * @param id
               * @throws DataAccessException
               
          */

              
          public void updateClob(final String name, final String text, final int id)
                      throws DataAccessException 
          {
                  getJdbcTemplate().execute(
                          
          "UPDATE yuntest set name=?,text=? where id=?",
                          
          new AbstractLobCreatingPreparedStatementCallback(
                                  
          this.lobHandler) {
                              
          protected void setValues(PreparedStatement ps,
                                      LobCreator lobCreator) throws SQLException 
          {
                                  ps.setString(
          1, name);
                                  lobCreator.setClobAsString(ps, 
          2, text);
                                  ps.setInt(
          3, id);

                              }

                          }
          );

              }


              
          /**查詢clob字段的方法示例
               * @return
               * @throws DataAccessException
               
          */

              
          public List getClob() throws DataAccessException {
                  
                  
          return getJdbcTemplate().query(
                          
          "SELECT id,name,text FROM yuntest where id=? ",
                          
          new Object[] new Integer(999999) }new RowMapper() {
                              
          public Object mapRow(ResultSet rs, int rowNum)
                                      throws SQLException 
          {
                                  
          int id = rs.getInt(1);
                                  String name 
          = rs.getString(2);
                                  String text 
          = lobHandler.getClobAsString(rs, 3);
                                  YunTestBean vo 
          = new YunTestBean();
                                  vo.setId(id);
                                  vo.setName(name);
                                  vo.setText(text);
                                  
          return vo;
                              }

                          }
          );
              }


              
          public void deleteClob() {
                  String sql 
          = "delete from yuntest where id = 999999";
                  getJdbcTemplate().execute(sql);
              }



              
          /**
               * @return Returns the lobHandler.
               
          */

              
          public LobHandler getLobHandler() {
                  
          return lobHandler;
              }


              
          /**
               * @param lobHandler
               *            The lobHandler to set.
               
          */

              
          public void setLobHandler(LobHandler lobHandler) {
                  
          this.lobHandler = lobHandler;
              }

          }

          posted on 2005-08-01 15:45 wilin 閱讀(2756) 評論(0)  編輯  收藏 所屬分類: spring

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


          網站導航:
           
          主站蜘蛛池模板: 杨浦区| 衢州市| 叙永县| 丘北县| 永清县| 十堰市| 崇文区| 获嘉县| 都匀市| 田阳县| 略阳县| 阜城县| 望都县| 梨树县| 河西区| 北辰区| 肥乡县| 玛曲县| 时尚| 台湾省| 绥滨县| 凌源市| 房产| 罗江县| 分宜县| 尉犁县| 常宁市| 张北县| 湖州市| 青铜峡市| 汽车| 尚志市| 醴陵市| 武清区| 鹿泉市| 固阳县| 铁岭县| 黎城县| 武夷山市| 威海市| 澄江县|