spring开发_JDBC *** 作MySQL数据库

spring开发_JDBC *** 作MySQL数据库,第1张

概述java spring jdbc MySQL 数据库的CRUD *** 作

com.b510.bean; Person { ID; String name; age; String sex; Person( ID,String name, age,String sex) { .ID = ID; .name = name; .age = age; .sex = sex; } Person() { } getID() { ID; } setID( ID) { .ID = ID; } String getname() { name; } setname(String name) { .name = name; } getAge() { age; } setAge( age) { .age = age; } String getSex() { sex; } setSex(String sex) { .sex = sex; } }

com.b510.service; java.util.List; com.b510.bean.Person; PersonService { save(Person person); update(Person person); Person getPerson(Integer ID); List getPerson(); delete(Integer ID); }

com.b510.service.impl; java.util.List; javax.sql.DataSource; org.springframework.jdbc.core.JdbcTemplate; com.b510.bean.Person; com.b510.service.PersonService; PersonServiceBean PersonService { DataSource dataSource; JdbcTemplate jdbcTemplate; setDataSource(DataSource dataSource) { .jdbcTemplate = JdbcTemplate(dataSource); } save(Person person) { jdbcTemplate.update("insert into person(name,age,sex)values(?,?,?)", Object[] { person.getname(),person.getAge(), person.getSex() }, [] { java.sql.Types.VARCHAR, java.sql.Types.INTEGER,java.sql.Types.VARCHAR }); } update(Person person) { jdbcTemplate.update("update person set name=?,age=?,sex=? where ID=?", Object[] { person.getname(), person.getSex(),person.getID() }, [] { java.sql.Types.VARCHAR,java.sql.Types.INTEGER, java.sql.Types.VARCHAR,java.sql.Types.INTEGER }); } Person getPerson(Integer ID) { Person person = (Person) jdbcTemplate.queryForObject( "select * from person where ID=?", Object[] { ID }, [] { java.sql.Types.INTEGER }, PersonRowMapper()); person; } @SuppressWarnings("unchecked") List getPerson() { List List = jdbcTemplate.query("select * from person", PersonRowMapper()); List; } delete(Integer ID) { jdbcTemplate.update("delete from person where ID = ?", [] { java.sql.Types.INTEGER }); } }

com.b510.service.impl; java.sql.ResultSet; java.sql.sqlException; org.springframework.jdbc.core.RowMapper; com.b510.bean.Person; PersonRowMapper RowMapper { @OverrIDe Object mapRow(ResultSet set, index) sqlException { Person person = Person(set.getInt("ID"),set.getString("name"),set .getInt("age"),set.getString("sex")); person; } }

com.b510.test; java.util.List; org.springframework.context.ApplicationContext; org.springframework.context.support.ClasspathXmlApplicationContext; com.b510.bean.Person; com.b510.service.PersonService; SpringJDBCTest { main(String[] args) { ApplicationContext act = ClasspathXmlApplicationContext("bean.xml"); PersonService personService = (PersonService) act .getBean("personService"); Person person = Person(); person.setname("苏东坡"); person.setAge(21); person.setSex("男"); personService.save(person); List person1 = personService.getPerson(); System.out.println("++++++++得到所有Person"); (Person person2 : person1) { System.out.println(person2.getID() + " " + person2.getname() + " " + person2.getAge() + " " + person2.getSex()); } Person updatePerson = Person(); updatePerson.setname("divIDe"); updatePerson.setAge(20); updatePerson.setSex("男"); updatePerson.setID(5); personService.update(updatePerson); System.out.println("******************"); Person onePerson = personService.getPerson(2); System.out.println(onePerson.getID() + " " + onePerson.getname() + " " + onePerson.getAge() + " " + onePerson.getSex()); personService.delete(1); } }

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http: http: http: http: ="org.apache.commons.dbcp.BasicdataSource" destroy-method="close"> value="jdbc:MysqL://localhost:3307/spring?useUnicode=true&characterEnCoding=UTF-8" /> ="org.springframework.jdbc.datasource.DataSourceTransactionManager"> ="com.b510.service.impl.PersonServiceBean">

2012-3-9 23:30:57 org.springframework.context.support.AbstractApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.support.ClasspathXmlApplicationContext@1a05308: display name [org.springframework.context.support.ClasspathXmlApplicationContext@1a05308]; startup date [Fri Mar 09 23:30:57 CST 2012]; root of context hIErarchy 2012-3-9 23:30:57 org.springframework.beans.factory.xml.XmlBeanDeFinitionReader loadBeanDeFinitions 信息: Loading XML bean deFinitions from path resource [bean.xml] 2012-3-9 23:30:58 org.springframework.context.support.AbstractApplicationContext obtainFreshbeanfactory 信息: Bean factory application context [org.springframework.context.support.ClasspathXmlApplicationContext@1a05308]: org.springframework.beans.factory.support.Defaultlistablebeanfactory@2bb514 2012-3-9 23:30:58 org.springframework.beans.factory.support.Defaultlistablebeanfactory preInstantiateSingletons 信息: Pre-instantiating singletons in org.springframework.beans.factory.support.Defaultlistablebeanfactory@2bb514: defining beans [dataSource,txManager,org.springframework.aop.config.internalAutoproxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor,personService]; root of factory hIErarchy ++++++++得到所有Person 2 TomCat 12 女 3 hongten 21 男 4 liufang 21 女 5 divIDe 20 男 6 Jone 20 女 7 苏东坡 21 男 ****************** 2 TomCat 12 女

driverClassname=org.gjt.mm.MysqL.Driver url=jdbc\:MysqL\: username=root password=root initialSize=1 maxActive=300 maxIDle=2 minIDle=1

相应要修改:

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http: http: http: http: ="org.apache.commons.dbcp.BasicdataSource" destroy-method="close"> ="org.springframework.jdbc.datasource.DataSourceTransactionManager"> ="com.b510.service.impl.PersonServiceBean">

运行结果是相同的:

2012-3-10 0:23:59 org.springframework.context.support.AbstractApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.support.ClasspathXmlApplicationContext@c1b531: display name [org.springframework.context.support.ClasspathXmlApplicationContext@c1b531]; startup date [Sat Mar 10 00:23:59 CST 2012]; root of context hIErarchy 2012-3-10 0:23:59 org.springframework.beans.factory.xml.XmlBeanDeFinitionReader loadBeanDeFinitions 信息: Loading XML bean deFinitions from path resource [bean.xml] 2012-3-10 0:23:59 org.springframework.context.support.AbstractApplicationContext obtainFreshbeanfactory 信息: Bean factory application context [org.springframework.context.support.ClasspathXmlApplicationContext@c1b531]: org.springframework.beans.factory.support.Defaultlistablebeanfactory@1aa57fb 2012-3-10 0:23:59 org.springframework.core.io.support.PropertIEsLoaderSupport loadPropertIEs 信息: Loading propertIEs file from path resource [jdbc.propertIEs] 2012-3-10 0:23:59 org.springframework.beans.factory.support.Defaultlistablebeanfactory preInstantiateSingletons 信息: Pre-instantiating singletons in org.springframework.beans.factory.support.Defaultlistablebeanfactory@1aa57fb: defining beans [org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#0,dataSource,personService]; root of factory hIErarchy ++++++++得到所有Person 2 TomCat 12 女 3 hongten 21 男 4 liufang 21 女 5 divIDe 20 男 6 Jone 20 女 7 苏东坡 21 男 8 苏东坡 21 男 ****************** 2 TomCat 12 女 总结

以上是内存溢出为你收集整理的spring开发_JDBC *** 作MySQL数据库全部内容,希望文章能够帮你解决spring开发_JDBC *** 作MySQL数据库所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1270038.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存