DANCE WITH JAVA

开发出高质量的系统

常用链接

统计

积分与排名

好友之家

最新评论

配制Spring声明型事务和JdbcTemplate的使用

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

    }
);

posted on 2007-02-08 18:33 dreamstone 阅读(5439) 评论(3)  编辑  收藏 所属分类: 其它开源框架

评论

# re: 配制Spring声明型事务和JdbcTemplate的使用[未登录] 2007-02-09 11:42 xmlspy

继承JdbcDaoSupport就不用显式声明JdbcTemplate 了  回复  更多评论   

# re: 配制Spring声明型事务和JdbcTemplate的使用 2007-02-11 01:35 dreamstone

继承虽然能够简化,但是也会带来一些限制,所以使用继承还是使用显示的声明要根据需求,如果没有特别的需求,使用继承倒是可以节省不少配置  回复  更多评论   

# re: 配制Spring声明型事务和JdbcTemplate的使用 2008-04-08 11:55 JackyChow

代码可读性极差!请问变量jt是哪里来的?  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航: