posts - 15,  comments - 34,  trackbacks - 27

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

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

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

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

              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.");
              }

           

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

          下面來看這個AccountManager,很顯然在驗證邏輯中寫入了JNDI查找DataSource的代碼,這將導致比較“生硬”的編碼。“依賴注入”此刻顯示出了威力:讓容器注入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)在這個AccountManager變得優(yōu)雅多了,現(xiàn)在如何配置DataSource的注入呢?Spring提供了一個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 閱讀(2019) 評論(1)  編輯  收藏 所屬分類: Open source

          FeedBack:
          # re: spring的jdbc封裝
          2011-09-14 14:21 | 呂紅芬
          OK,Thank you !!!  回復  更多評論
            
          <2025年5月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          常用鏈接

          留言簿(10)

          隨筆檔案

          文章分類

          文章檔案

          相冊

          收藏夾

          java

          搜索

          •  

          最新評論


          主站蜘蛛池模板: 洪泽县| 江川县| 津市市| 永兴县| 顺平县| 县级市| 盐源县| 南澳县| 遂昌县| 土默特右旗| 资阳市| 恩施市| 邵武市| 武平县| 渭源县| 田林县| 昌图县| 柳州市| 行唐县| 怀远县| 北流市| 湄潭县| 鹰潭市| 炎陵县| 册亨县| 长春市| 新巴尔虎右旗| 明溪县| 扶沟县| 汝城县| 连州市| 个旧市| 灵川县| 木里| 凯里市| 台前县| 伊金霍洛旗| 灵丘县| 焉耆| 大渡口区| 腾冲县|