配制Spring事务和JdbcTemplate使用。(转)

Spring JDBC 数据访问配置
本文介绍了一个使用Spring框架进行数据库连接池配置及JDBC操作的例子。通过配置applicationContext.xml文件,实现了基于Commons DBCP的数据源管理和Spring的事务管理,并展示了如何在DAO层实现增删改查操作。

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

    }
);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值