服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

配制spring事务和jdbctemplate使用


  配制一个applicationcontext.xml如下
配制spring事务和jdbctemplate使用(图一) <? xml version = " 1.0 "  encoding = " utf-8 " ?>
配制spring事务和jdbctemplate使用(图一) <! doctype beans public  " -//spring//dtd bean//en "   " http://www.springframework.org/dtd/spring-beans.dtd " >
配制spring事务和jdbctemplate使用(图一)
配制spring事务和jdbctemplate使用(图一) < beans  default - autowire = " autodetect " >
配制spring事务和jdbctemplate使用(图一)     < import  resource = " classpath:conf/spring/demo.xml "   />
配制spring事务和jdbctemplate使用(图一)     < bean id = " datasource "   class = " org.apache.commons.dbcp.basicdatasource " >  
配制spring事务和jdbctemplate使用(图一)         < property name = " driverclassname " >  
配制spring事务和jdbctemplate使用(图一)             < value > com.mysql.jdbc.driver </ value >  
配制spring事务和jdbctemplate使用(图一)         </ property >  
配制spring事务和jdbctemplate使用(图一)         < property name = " url " >  
配制spring事务和jdbctemplate使用(图一)             < value > jdbc:mysql: // 192.168.1.10:3306/test?characterencoding=utf-8&amp;charactersetresults=utf-8</value>
配制spring事务和jdbctemplate使用(图一)          </ property >
配制spring事务和jdbctemplate使用(图一)         < property name = " username " >
配制spring事务和jdbctemplate使用(图一)             < value > root </ value >
配制spring事务和jdbctemplate使用(图一)         </ property >
配制spring事务和jdbctemplate使用(图一)         < property name = " password " >
配制spring事务和jdbctemplate使用(图一)             < value > xx </ value >
配制spring事务和jdbctemplate使用(图一)         </ property >
配制spring事务和jdbctemplate使用(图一)         < property name = " maxactive " >
配制spring事务和jdbctemplate使用(图一)             < value > 10 </ value >
配制spring事务和jdbctemplate使用(图一)         </ property >
配制spring事务和jdbctemplate使用(图一)         < property name = " maxidle " >
配制spring事务和jdbctemplate使用(图一)             < value > 2 </ value >
配制spring事务和jdbctemplate使用(图一)         </ property >
配制spring事务和jdbctemplate使用(图一)     </ bean >
配制spring事务和jdbctemplate使用(图一)     < bean id = " transactionmanager "
配制spring事务和jdbctemplate使用(图一)         class = " org.springframework.jdbc.datasource.datasourcetransactionmanager " >
配制spring事务和jdbctemplate使用(图一)         < property name = " datasource " >
配制spring事务和jdbctemplate使用(图一)             < ref bean = " datasource "   />
配制spring事务和jdbctemplate使用(图一)         </ property >
配制spring事务和jdbctemplate使用(图一)     </ bean >
配制spring事务和jdbctemplate使用(图一)     < bean id = " jdbctemplate "
配制spring事务和jdbctemplate使用(图一)         class = " org.springframework.jdbc.core.jdbctemplate " >
配制spring事务和jdbctemplate使用(图一)         < property name = " datasource " >
配制spring事务和jdbctemplate使用(图一)             < ref bean = " datasource "   />
配制spring事务和jdbctemplate使用(图一)         </ property >
配制spring事务和jdbctemplate使用(图一)     </ bean >
配制spring事务和jdbctemplate使用(图一) </ beans >
配制spring事务和jdbctemplate使用(图一)对应的testdaoimpl中加入这部分代码
配制spring事务和jdbctemplate使用(图一)     private  jdbctemplate jdbctemplate;
配制spring事务和jdbctemplate使用(图一)    
配制spring事务和jdbctemplate使用(图二)配制spring事务和jdbctemplate使用(图三)     public  jdbctemplate getjdbctemplate()  配制spring事务和jdbctemplate使用(图四) {
配制spring事务和jdbctemplate使用(图五)         return  jdbctemplate;
配制spring事务和jdbctemplate使用(图六)    }
配制spring事务和jdbctemplate使用(图二) 配制spring事务和jdbctemplate使用(图三)     public   void  setjdbctemplate(jdbctemplate jdbctemplate)  配制spring事务和jdbctemplate使用(图四) {
配制spring事务和jdbctemplate使用(图五)         this .jdbctemplate  =  jdbctemplate;
配制spring事务和jdbctemplate使用(图六)    }
配制spring事务和jdbctemplate使用(图一)     // 插入,修改和删除类似
配制spring事务和jdbctemplate使用(图一)     string sql1  =   " insert into testdb1 values('1','2') " ;
配制spring事务和jdbctemplate使用(图一)    jdbctemplate.update(sql1);
配制spring事务和jdbctemplate使用(图一)     // 查询
配制spring事务和jdbctemplate使用(图二) 配制spring事务和jdbctemplate使用(图三)      private   class  beanrowmapper  implements  rowmapper  配制spring事务和jdbctemplate使用(图四) {
配制spring事务和jdbctemplate使用(图七)配制spring事务和jdbctemplate使用(图八)         public  object maprow(resultset rs,  int  rownum)  throws  sqlexception  配制spring事务和jdbctemplate使用(图四) {
配制spring事务和jdbctemplate使用(图五)            string id  =  rs.getstring( " id " );
配制spring事务和jdbctemplate使用(图五)            string title  =  rs.getstring( " title " );
配制spring事务和jdbctemplate使用(图五)            bean bean  =   new  bean(id,title);
配制spring事务和jdbctemplate使用(图五)             return  bean;
配制spring事务和jdbctemplate使用(图九)        }
配制spring事务和jdbctemplate使用(图六)    }
配制spring事务和jdbctemplate使用(图一)    string sql1  =   " select *  from testdb1  " ;
配制spring事务和jdbctemplate使用(图一)    list list  =  jdbctemplate.query(sql1,  new  beanrowmapper());
配制spring事务和jdbctemplate使用(图一)     // call back    (回调)
配制spring事务和jdbctemplate使用(图二) 配制spring事务和jdbctemplate使用(图三)     jt.execute( new  connectioncallback() 配制spring事务和jdbctemplate使用(图四) {
配制spring事务和jdbctemplate使用(图七)配制spring事务和jdbctemplate使用(图八)         public  object doinconnection(java.sql.connection con)  throws  sqlexception, dataaccessexception  配制spring事务和jdbctemplate使用(图四) {
配制spring事务和jdbctemplate使用(图五)             return   null ;
配制spring事务和jdbctemplate使用(图九)        }
配制spring事务和jdbctemplate使用(图六)    } );

扫描关注微信公众号