国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

配制Spring事務和JdbcTemplate使用

2019-11-17 05:56:46
字體:
來源:轉載
供稿:網友

配制一個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 ;
        }
    } );



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宕昌县| 辉县市| 广宁县| 兰考县| 江达县| 洪雅县| 西宁市| 临泽县| 临武县| 万州区| 任丘市| 塔城市| 仙游县| 和顺县| 翁源县| 边坝县| 长垣县| 安泽县| 大关县| 淮阳县| 伊春市| 壤塘县| 平潭县| 沁阳市| 犍为县| 陵川县| 凤阳县| 沈丘县| 光山县| 兴义市| 汉源县| 平果县| 璧山县| 灌云县| 安平县| 黄平县| 青川县| 乌恰县| 合川市| 宾阳县| 若尔盖县|