Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > Java > spring操作数据库如此简单
【标  题】:spring操作数据库如此简单
【关键字】:spring
【来  源】:http://www.blogjava.net/rory/archive/2006/04/07/39699.html

spring操作数据库如此简单

Your Ad Here 以前一直都没有试过,前几天打算把wordpress换成自己写的程序,就想到了数据的导入和导出,首先想到的是用数据库工具来导。可是觉得有些麻烦,我自己的程序是用spring+hibernate的。后来我就试了一下spring的JdbcTemplate,就和HibernateTemplate一样的好用。首先增加一个连接到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");
????????}
????}
浅析MVC框架中View层的优雅设计及实例:【上一篇】
形式化关系型模型转换方法的一些观点:【下一篇】
【相关文章】
  • spring中使用proxool作数据源连接池的配置。
  • struts+hibernate+spring 开发入门
  • spring入门
  • Hibernate hates Spring
  • 参加BEA北京User Group活动 --开源软件开发大讲堂之Spring篇 有感
  • 如何在Spring中使用JasperReport
  • 在eclipse rcp中使用spring框架
  • 初见 springside
  • spring持久化
  • spring提供的事务管理
  • 【随机文章】
  • JDK6.0的新特性之四:使用Compiler API
  • Ajax基础(一)
  • paste
  • [讨论]临时对象有地址么?
  • 把你的老数码相机也变摄像头
  • 【转贴】OCTREE 教程
  • 二叉树的遍历:前序,中序,后序,层序--包括递归和非递归实现
  • vs.net中创建asp.net程序时,出现:http/1.1 500 internal server error
  • 贴两张FC6
  • 与服务器进行异步通信---Xjax
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 bbb软讯网络 All Rigths Reserved.