waterye

          使用Spring JDBC Framework簡(jiǎn)化開(kāi)發(fā)

          純JDBC操作, 對(duì)某些項(xiàng)目來(lái)說(shuō), 也許更好, Spring JDBC Framework讓你不用關(guān)心Connection, Statement, ResultSet.

          定義數(shù)據(jù)源
          <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
              
          <property name="jndiName">
                  
          <value>java:/comp/env/jdbc/oracle</value>
              
          </property>
          </bean>

          定義事務(wù)管理器
          <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
              
          <property name="dataSource" ref="dataSource" />
          </bean>

          定義dao
          <bean id="customerDAO" class="com.waterye.dao.impl.CustomerDAOImpl">
              
          <property name="dataSource" ref="dataSource" />
              
          <property name="transactionManager" ref="transactionManager" />
          </bean>

          demo
          public class CustomerDaoImpl extends JdbcDaoSupport implements CustomerDAO {
              
          private DataSource dataSource; 
              pirvate TransactionManager transactionManager;

              
          public void setDataSource(DataSource dataSource) {
                  
          this.dataSource = dataSource;
              }

              
          public void setTransactionManager(DataSourceTransactionManager transactionManager) {
                  
          this.transactionManager = transactionManager;
              } 

              
          public Map get(Integer id) throws Exception {
                  String querySql 
          = "select * from customer where id = ?";

                  
          return getJdbcTemplate().queryForMap(querySql, new Object[] { id }); 
              }

              
          public void insert(final Map customer) throws Exception {
                  String seqSql 
          = "select customer_seql.nextval from dual"
                  String insertSql 
          = "insert into customer (id, code, name, status) values (?, ?, ?, ?)";
                  
                  TransactionTemplate transactionTemplate 
          = new TransactionTemplate(transactionManager);
                  transactionTemplate.execute(
          new TransactionCallbackWithoutResult() { 
                      
          protected void doInTransactionWithoutResult(TransactionStatus status) { 
                          JdbcTemplate jdbcTemplate 
          = getJdbcTemplate();
                          
          int id = jdbcTemplate.queryForInt(seqSql);
                          Object[] params 
          = new Object[] { new Integer(id), customer.get("code"), customer.get("name"), map.get("status") };
                          jdbcTemplate.update(insertSql, params);
                      }
                  }
              }

              
          public void update(final Map customer) throws Exception {
                  
          //  
              }

              
          public void delete(Integer id) throws Exception {
                  String deleteSql 
          = "delete from customer where id = ?";

                  TransactionTemplate transactionTemplate 
          = new TransactionTemplate(transactionManager);
                  transactionTemplate.execute(
          new TransactionCallbackWithoutResult() { 
                      
          protected void doInTransactionWithoutResult(TransactionStatus status) { 
                          getJdbcTemplate().update(deleteSql, 
          new Object[] { id });
                      }
                  }
              }

              
          public List findValidCustomers() throws Exception {
                  String querySql 
          = "select * from customer where status = 'valid' order by code";

                  
          return getJdbcTemplate().query(querySql, new OracleColumnMapRowMapper()); 
              }

          說(shuō)明:
          1. 沒(méi)有使用聲明性事務(wù), 使用編程式事務(wù)
          2. 沒(méi)有使用POJO模式,使用HashMap, (ActiveMapper還在sandbax狀態(tài))
          3. OracleColumnMapRowMapper implements RowMapper, 實(shí)現(xiàn)oracle風(fēng)格到j(luò)ava bean風(fēng)格mapping
              如: 字段customer_id 對(duì)應(yīng)rowMap.get("customerId");

          posted on 2005-08-21 20:13 waterye 閱讀(2446) 評(píng)論(3)  編輯  收藏 所屬分類(lèi): spring

          Feedback

          # re: 使用Spring JDBC Framework簡(jiǎn)化開(kāi)發(fā) 2005-08-25 12:37 江南白衣@ITO

          透明同志說(shuō),代碼量控制在30%以?xún)?nèi)的文章是好文章,按這個(gè)標(biāo)準(zhǔn).......  回復(fù)  更多評(píng)論   

          # re: 使用Spring JDBC Framework簡(jiǎn)化開(kāi)發(fā) 2005-08-25 13:01 Water Ye@ITO

          本人語(yǔ)言表達(dá)能力較差  回復(fù)  更多評(píng)論   

          # re: 使用Spring JDBC Framework簡(jiǎn)化開(kāi)發(fā) 2005-08-25 21:38 江南白衣@ITO

          又據(jù)Joel on software同志說(shuō),"影響別人的能力很重要,所以要在blog里認(rèn)真鍛煉寫(xiě)作能力阿。", so,代碼掛帥的blog風(fēng)格要改一下了哦。  回復(fù)  更多評(píng)論   

          主站蜘蛛池模板: 湘潭县| 福建省| 奉化市| 台山市| 承德市| 宝鸡市| 永新县| 山西省| 渝北区| 和政县| 本溪| 星子县| 铜川市| 罗甸县| 揭东县| 辉县市| 阳春市| 宜阳县| 通化市| 佳木斯市| 巴中市| 淅川县| 灯塔市| 石林| 天峨县| 桓台县| 陇南市| 砀山县| 旬阳县| 峡江县| 民勤县| 龙井市| 寻甸| 监利县| 明星| 苏尼特左旗| 都兰县| 阳泉市| 库伦旗| 莒南县| 开平市|