随笔-72  评论-63  文章-0  trackbacks-0
     < bean  id ="dataSource"  class ="org.springframework.jdbc.datasource.DriverManagerDataSource"  destroy-method ="close" >
        
< property  name ="driverClassName" >< value > org.hibernate.dialect.MySQLDialect </ value ></ property >
        
< property  name ="url" >
            
< value > jdbc:mysql://127.0.0.1:3306/springtest&useUnicode=true &amp; characterEncoding=utf8 </ value >
            
</ property >
        
< property  name ="username" >< value > root </ value ></ property >
        
< property  name ="password" >< value ></ value ></ property >
    
</ bean >

    然后在转换程序里面get这个dataSource,new 一个JdbcTemplate(dataSource)

     public   void  testCopyData()  throws  Exception{
        DataSource ds 
=  (DataSource)applicationContext.getBean( " dataSource " );
         JdbcTemplate jt  =   new  JdbcTemplate(ds);
        System.out.println(
" Total posts: " + jt.queryForInt( " select count(*) from wp_posts " ));
        assertNotNull(ds);
        
        List cates 
=  jt.queryForList( " select * from wp_categories " );
        
int  i =   0 ;
        
for (Iterator ite  =  cates.iterator(); ite.hasNext();){
            i
++ ;
            Map result 
=  (Map) ite.next();
            Category cate 
=   new  Category();
            cate.setName((String)result.get(
" cat_name " ));
            cate.setOrder(i);
            
if (i == 1 )
                cate.setDefaultCategory(
true );
            cateManager.saveCategory(cate);
            System.out.println(
" cat_name: " + result.get( " cat_name " ) + " \n " );
        }
    }
posted on 2006-05-01 23:54 船长 阅读(323) 评论(0)  编辑  收藏

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


网站导航: