Chinaunix首页 | 论坛 | 博客
  • 博客访问: 673749
  • 博文数量: 779
  • 博客积分: 5000
  • 博客等级: 大校
  • 技术积分: 5000
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-27 13:28
文章分类

全部博文(779)

文章存档

2011年(1)

2008年(778)

我的朋友

分类:

2008-10-27 13:28:22

配制一个applicationContext.xml如下
xml version = " 1.0 "  encoding = " UTF-8 " ?>
DOCTYPE beans PUBLIC  " -//SPRING//DTD BEAN//EN "   " " >

< 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&characterSetResults=UTF-8
         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 ;
        }

    }
);
【责编:Peng】

--------------------next---------------------

阅读(312) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~