posts - 15,  comments - 34,  trackbacks - 27

          Spring對(duì)JDBC進(jìn)行了非常優(yōu)雅的封裝,通過一系列的模板方法,只需簡(jiǎn)單的幾行代碼就可實(shí)現(xiàn)數(shù)據(jù)庫(kù)的訪問。通過Spring的JdbcTemplate訪問數(shù)據(jù)庫(kù)從而實(shí)現(xiàn)用戶驗(yàn)證。Access數(shù)據(jù)庫(kù),建立表Account,包含兩個(gè)字段:

              username:VARCHAR(20),主鍵;
              password:VARCHAR(20)。

          然后輸入一些測(cè)試數(shù)據(jù),注冊(cè)到系統(tǒng)DSN,名字為Blog。接下來我們?cè)赥omcat中配置一個(gè)名為“jdbc/blog”的DataSource如下:

          如果你使用其他數(shù)據(jù)庫(kù),只需要保證表的邏輯結(jié)構(gòu)和JDNI名“jdbc/blog”。在AccountManager中,使用JdbcTemplate訪問數(shù)據(jù)庫(kù)來驗(yàn)證用戶:

              Account getAccount(String username, String password) throws Exception {
                  
          // validate the password:
                  InitialContext ctx = new InitialContext();
                  DataSource dataSource 
          = (DataSource)ctx.lookup("java:comp/env/jdbc/blog");
                  JdbcTemplate jt 
          = new JdbcTemplate(dataSource);
                  String pass 
          = (String) jt.queryForObject(
                      
          "select password from Account where username=?",
                      
          new Object[] {username}, String.class);
                  
          if(password.equals(pass))
                  
          {
                      Account account 
          = new Account();
                      account.setUsername(username);
                      account.setPassword(password);
                      
          return account;
                  }

                  
          throw new Exception("authorization failed.");
              }

           

          編譯運(yùn)行,輸入hello.c?username=xxx&password=xxx來測(cè)試用戶登錄,如果匹配數(shù)據(jù)庫(kù)中的記錄,會(huì)顯示出用戶名,否則可以看到authorization failed異常。要更友好的顯示登陸失敗,可以在Controller中導(dǎo)向一個(gè)login_failed.jsp并給出登陸失敗的原因。

          下面來看這個(gè)AccountManager,很顯然在驗(yàn)證邏輯中寫入了JNDI查找DataSource的代碼,這將導(dǎo)致比較“生硬”的編碼。“依賴注入”此刻顯示出了威力:讓容器注入DataSource:

          public class AccountManager implements java.io.Serializable {
              
          private DataSource dataSource;
              
          public void setDataSource(DataSource dataSource) {
                  
          this.dataSource = dataSource;
              }


              Account getAccount(String username, String password) throws Exception 
          {
                  JdbcTemplate jt 
          = new JdbcTemplate(dataSource);
                  
              }

          }

          OK,現(xiàn)在這個(gè)AccountManager變得優(yōu)雅多了,現(xiàn)在如何配置DataSource的注入呢?Spring提供了一個(gè)DataSourceUtils類,通過靜態(tài)方法getDataSourceFromJndi來獲得容器中配置的DataSource。我們添加以下配置:

              <bean id="dataSource"
                    class
          ="org.springframework.jdbc.datasource.DataSourceUtils"
                    factory-method
          ="getDataSourceFromJndi">
                  
          <constructor-arg><value>jdbc/blogvalue>constructor-arg>
              
          bean>
              
          <bean id="accountManager" class="AccountManager">
                  
          <property name="dataSource">
                      
          <ref bean="dataSource" />
                  
          property>
              
          bean>
           
          現(xiàn)在重新部署,效果和以前一樣了,但是相關(guān)代碼已被"放到",配置文件中了
          posted on 2005-02-04 11:20 jacky 閱讀(2025) 評(píng)論(1)  編輯  收藏 所屬分類: Open source

          FeedBack:
          # re: spring的jdbc封裝
          2011-09-14 14:21 | 呂紅芬
          OK,Thank you !!!  回復(fù)  更多評(píng)論
            
          <2025年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          常用鏈接

          留言簿(10)

          隨筆檔案

          文章分類

          文章檔案

          相冊(cè)

          收藏夾

          java

          搜索

          •  

          最新評(píng)論


          主站蜘蛛池模板: 巍山| 宝山区| 三河市| 时尚| 烟台市| 塔河县| 达日县| 武冈市| 古交市| 芦溪县| 镇康县| 巨鹿县| 彭泽县| 临夏市| 三门县| 原阳县| 广水市| 淳化县| 大荔县| 班玛县| 潢川县| 马鞍山市| 监利县| 盐亭县| 峨眉山市| 丰顺县| 凭祥市| 宾阳县| 海口市| 洮南市| 林口县| 镇沅| 当涂县| 大名县| 夏津县| 麻江县| 西藏| 闽侯县| 华亭县| 温州市| 洛浦县|