zoukankan      html  css  js  c++  java
  • 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");
            }
        }
  • 相关阅读:
    [nginx]简单内容管理系统的spring项目的nginx.conf配置文件的注释理解
    [日常填坑]部署使用Idea开发的spring框架的多模块项目到服务器
    [日常填坑]centos7.0+版本服务器安装jdk9或者jdk10
    [日常填坑]centos7.0+版本服务器安装mysql
    机器学习实战之kNN算法
    机器学习实战之机器学习主要任务
    模式字符串匹配问题(KMP算法)
    ubuntu 更新引导命令
    ubuntu 14.04 中找不到 libgtk-x11-2.0.so
    maven project 更新总是jre-1.5
  • 原文地址:https://www.cnblogs.com/zghull/p/2757322.html
Copyright © 2011-2022 走看看