• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: 小 中 大 | 推荐给好友 上一篇 | 下一篇

配制Spring事务和JdbcTemplate使用

发布: 2007-7-04 13:34 | 作者: admin | 来源:  网友评论 | 查看: 87次 | 进入软件测试论坛讨论

领测软件测试网 配制一个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 ;
        }

    }
);

延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备2023014753号-2
技术支持和业务联系:[email protected] 电话:010-51297073

软件测试 | 领测国际 | ISTQB | ISTQB官网 | TMMi | TMMi认证 | 国际软件测试工程师认证 | 领测软件测试网