欢迎访问 生活随笔!

凯发k8官方网

当前位置: 凯发k8官方网 > 前端技术 > javascript >内容正文

javascript

spring 操作数据库 -凯发k8官方网

发布时间:2024/1/17 javascript 25 豆豆
凯发k8官方网 收集整理的这篇文章主要介绍了 spring 操作数据库 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

试了一下spring的jdbctemplate觉得很好用。首先增加一个连接到mysql数据库的datasource

<bean id="datasource2" class="org.springframework.jdbc.datasource.drivermanagerdatasource" destroy-method="close"><property name="driverclassname"><value>org.hibernate.dialect.mysqldialectvalue>property><property name="url"><value>jdbc:mysql://192.168.0.240:3306/wordpressωuseunicode=true&characterencoding=utf8value>property><property name="username"><value>rootvalue>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") "\n");}}

 

转载于:https://www.cnblogs.com/zuiyirenjian/p/4005959.html

总结

以上是凯发k8官方网为你收集整理的spring 操作数据库的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得凯发k8官方网网站内容还不错,欢迎将凯发k8官方网推荐给好友。

网站地图