• Spring 操作数据库


    试了一下spring的JdbcTemplate觉得很好用。首先增加一个连接到mysql数据库的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>
     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")+"
    ");
            }
        }
  • 相关阅读:
    Spring Boot 缓存技术:Spring Boot
    Java基础之Iterable接口
    使用sqlyog连接 Mysql 出现1251错误
    IDEA更改主题插件——Material Theme UI详解
    免安装版的Mysql
    使用Nexus搭建Maven私服
    Spring之注解注入bean
    Idea springboot 配置热部署
    Spring Boot 异常处理与单元测试
    Ubuntu20.04在线安装VMware-Tools
  • 原文地址:https://www.cnblogs.com/zuiyirenjian/p/4005959.html
Copyright © 2020-2023  润新知