程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA編程入門知識 >> 配制Spring事務和JdbcTemplate使用

配制Spring事務和JdbcTemplate使用

編輯:JAVA編程入門知識

  配制一個applicationContext.XML如下
   
  <? xml version = " 1.0 "  encoding = " UTF-8 " ?>
   <! DOCTYPE beans PUBLIC  " -//SPRING//DTD BEAN//EN "   " http://www.springframework.org/dtd/spring-beans.dtd " >
   
   < beans  default - autowire = " autodetect " >
       < import  resource = " classpath:conf/spring/demo.xml "   />
       < bean id = " DataSource "   class = " org.apache.commons.dbcp.BasicDataSource " > 
           < property name = " driverClassName " > 
               < value > com.mysql.jdbc.Driver </ value > 
           </ property > 
           < property name = " url " > 
               < value > jdbc:mysql: // 192.168.1.10:3306/test?characterEncoding=UTF-8&amp;characterSetResults=UTF-8</value>
            </ property >
           < property name = " username " >
               < value > root </ value >
           </ property >
           < property name = " passWord " >
               < value > xx </ value >
           </ property >
           < property name = " maxActive " >
               < value > 10 </ value >
           </ property >
           < property name = " maxIdle " >
               < value > 2 </ value >
           </ property >
       </ bean >
       < bean id = " TransactionManager "
           class = " org.springframework.jdbc.datasource.DataSourceTransactionManager " >
           < property name = " dataSource " >
  
                < ref bean = " DataSource "   />
           </ property >
       </ bean >
       < bean id = " JdbcTemplate "
           class = " org.springframework.jdbc.core.JdbcTemplate " >
           < property name = " dataSource " >
               < ref bean = " DataSource "   />
           </ property >
       </ bean >
   </ beans >
  對應的TestDaoImpl中加入這部分代碼
       private  JdbcTemplate jdbcTemplate;
     
       public  JdbcTemplate getJdbcTemplate()   {
           return  jdbcTemplate;
      }
        public   void  setJdbcTemplate(JdbcTemplate jdbcTemplate)   {
           this .jdbcTemplate  =  jdbcTemplate;
      }
       // 插入,修改和刪除類似
       String sql1  =   " insert into testdb1 values('1','2') " ;
      jdbcTemplate.update(sql1);
       // 查詢
         private   class  BeanRowMapper  implements  RowMapper   {
           public  Object mapRow(ResultSet rs,  int  rowNum)  throws  SQLException   {
              String id  =  rs.getString( " ID " );
              String title  =  rs.getString( " TITLE " );
              Bean bean  =   new  Bean(id,title);
               return  bean;
          }
      }
      String sql1  =   " select *  from testdb1  " ;
      List list  =  jdbcTemplate.query(sql1,  new  BeanRowMapper());
       // call back    (回調)
        jt.execute( new  ConnectionCallback()  {
           public  Object doInConnection(Java.sql.Connection con)  throws  SQLException, DataAccessException   {
  
                return   null ;
          }
      } );


 
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved