spring dataSource操作数据库

首先增加一个连接到wp数据库的dataSource

     < bean  id ="dataSource2"  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://192.168.0.240:3306/wordpressω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(dataSource2),这样就ok了。很简单吧。

     public   void  testCopyData()  throws  Exception{
        DataSource ds 
=  (DataSource)applicationContext.getBean( " dataSource2 " );
        
        CategoryManager cateManager 
=  (CategoryManager) applicationContext.getBean( " categoryManager " );
        
        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 " );
        }
    }

转载于:https://www.cnblogs.com/zghull/archive/2012/11/06/2757322.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值