บทความนี้แนะนำการดำเนินงาน Spring Development_jdbc ของฐานข้อมูล MySQL ดังต่อไปนี้:
โครงสร้างโครงการ:
ตารางฐานข้อมูล:
/spring_1100_spring+jdbc/src/com/b510/bean/person.java
แพ็คเกจ com.b510.bean;/** * คนคลาส Javabean ปกติ * * @author hongten * */บุคคลชั้นเรียนสาธารณะ {/** * หมายเลขรหัส */ID int ส่วนตัว; / ** * ชื่อ */ ชื่อสตริงส่วนตัว; / ** * อายุ */ อายุ int ส่วนตัว; / ** * เพศ */ เซ็กซ์สตริงส่วนตัว; บุคคลสาธารณะ (ID int, ชื่อสตริง, อายุ int, String sex) {this.id = id; this.name = ชื่อ; this.age = อายุ; this.sex = เพศ; } บุคคลสาธารณะ () {} public int getId () {return id; } โมฆะสาธารณะ setId (int id) {this.id = id; } สตริงสาธารณะ getName () {ชื่อคืน; } โมฆะสาธารณะ setName (ชื่อสตริง) {this.name = name; } public int getage () {return Age; } การตั้งค่าโมฆะสาธารณะ (อายุ int) {this.age = อายุ; } Public String getSex () {return sex; } โมฆะสาธารณะ setsex (String sex) {this.sex = sex; - /spring_1100_spring+jdbc/src/com/b510/service/personservice.java
แพ็คเกจ com.b510.service; นำเข้า java.util.list; นำเข้า com.b510.bean.person; อินเทอร์เฟซสาธารณะบุคคลที่บริการ { / ** * บันทึกบุคคล * * @param บุคคล * / โมฆะนามธรรมสาธารณะบันทึก (บุคคลบุคคล); / ** * อัปเดตบุคคล * * @param บุคคล */ การอัปเดตโมฆะนามธรรมสาธารณะ (บุคคลบุคคล); / ** * รับบุคคล * * @param id * @return */ บุคคลที่เป็นนามธรรมบุคคล GetPerson (จำนวนเต็ม); / ** * รับบุคคลทั้งหมด * * @return */ รายการนามธรรมสาธารณะ <person> getPerson (); / ** * ลบบุคคลที่มี ID ที่ระบุ * * @param id */ บทคัดย่อสาธารณะ void delete (จำนวนเต็ม id);} /spring_1100_spring+jdbc/src/com/b510/service/impl/personservicebean.java
แพ็คเกจ com.b510.service.impl; นำเข้า java.util.list; นำเข้า javax.sql.datasource; นำเข้า org.springframework.jdbc.core.jdbctemplate; นำเข้า com.b510.bean.person; PersonServiceBean ใช้งานบุคคล { / ** * แหล่งข้อมูล * / แหล่งข้อมูลส่วนตัวแหล่งข้อมูล; / *** JDBC การดำเนินการคลาสเสริมที่จัดทำโดย Spring*/ ส่วนตัว JDBCTEMPLATE JDBCTEMPLATE; // ตั้งค่าแหล่งข้อมูลโมฆะสาธารณะ setDataSource (DataSource DataSource) {this.jdbctemplate = ใหม่ JDBCTEMPLATE (DataSource); } โมฆะสาธารณะบันทึก (บุคคลบุคคล) {jdbctemplate.update ("แทรกลงในบุคคล (ชื่ออายุเพศ) ค่า (?,?,?)", วัตถุใหม่ [] {person.getName (), person.getage (), person.getsex ()}, int ใหม่ [] {java.sql.ty java.sql.types.varchar}); } การอัปเดตโมฆะสาธารณะ (บุคคลบุคคล) {jdbctemplate.update ("อัปเดตบุคคลที่ตั้งชื่อ =?, อายุ =?, sex =? ที่ไหน id =?", วัตถุใหม่ [] {person.getName (), person.getage (), person.getsex (), person.getId ()} java.sql.types.integer, java.sql.types.varchar, java.sql.types.integer}); } บุคคลสาธารณะ getPerson (จำนวนเต็ม) {person person = (บุคคล) jdbctemplate.QueryForObject ("เลือก * จากบุคคลที่ id =?", วัตถุใหม่ [] {id}, int ใหม่ [] {java.sql.types.Integer}, บุคคลใหม่ คนกลับ; } @suppresswarnings ("ไม่ได้ตรวจสอบ") รายการสาธารณะ <person> getPerson () {รายการ <person> list = jdbctemplate.query ("เลือก * จากบุคคล", personrowmapper ใหม่ ()); รายการคืน; } โมฆะสาธารณะลบ (ID จำนวนเต็ม) {jdbctemplate.update ("ลบจากบุคคลที่ id =?", วัตถุใหม่ [] {id}, int ใหม่ [] {java.sql.types.integer}); - /spring_1100_spring+jdbc/src/com/b510/service/impl/personrowmapper.java
แพ็คเกจ com.b510.service.impl; นำเข้า java.sql.resultset; นำเข้า java.sql.sqlexception; นำเข้า org.springframework.jdbc.core.rowmapper; นำเข้า com.b510.bean.person; sqlexception {person person = บุคคลใหม่ (set.getInt ("id"), set.getString ("ชื่อ"), set. getInt ("อายุ"), set.getString ("เพศ")); คนกลับ; - /spring_1100_spring+jdbc/src/com/b510/test/springjdbctest.java
แพ็คเกจ com.b510.test; นำเข้า java.util.list; นำเข้า org.springframework.context.applicationContext; นำเข้า org.springframework.context.support.classpathxmlapplicationcontext; นำเข้า com.b510.bean.person; Main (String [] args) {ApplicationContext Act = ใหม่ classPathxMlApplicationContext ("bean.xml"); บุคคลที่ให้บริการด้านการบริการ = (บุคคลที่บริการ) พระราชบัญญัติ. getBean ("บุคคลบริการ"); บุคคล = บุคคลใหม่ (); person.setName ("Su Dongpo"); person.setage (21); person.setsex ("ชาย"); // บันทึกบุคคลที่บันทึกไว้ Save (บุคคล); รายการ <person> person1 = personservice.getperson (); System.out.println ("++++++++ รับทุกคน"); สำหรับ (person person2: person1) {system.out.println (person2.getId () + "" + person2.getName () + "" + person2.getage () + "" person2.getsex ()); } บุคคล UpdatePerson = คนใหม่ (); UpdatePerson.setName ("Divide"); updateperson.setage (20); updateperson.setsex ("ชาย"); updateperson.setid (5); // อัปเดต Personservice.update (UpdatePerson); System.out.println ("******************"); // รับบันทึกบุคคล oneperson = personservice.getperson (2); System.out.println (oneperson.getId () + "" + oneperson.getName () + "" + oneperson.getage () + "" + oneperson.getsex ()); // ลบบันทึกบุคคลที่มีการบันทึก (1); - /spring_1100_spring+jdbc/src/bean.xml
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://ww.w3.org/2001/xml xmlns: context = "http://www.springframework.org/schema/context" xmlns: aop = "http://www.springframework.org/schema/aop" xmlns: tx = "http:/ XSI: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://ww.springframework http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://ww.springframework.org/schema http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://ww.springframework แหล่งที่มา-> <bean id = "dataSource" destroy-method = "close"> <property name = "driverclassname" value = "org.gjt.mm.mysql.driver"/> <property name = "url" value = "jdbc: mysql: // localhost: 3307 value = "root" /> <property name = "password" value = "root" /> <!-ค่าเริ่มต้นเมื่อพูลการเชื่อมต่อเริ่มต้น-> <property name = "imitaryize" value = "1" /> <! หลังจากเวลาสูงสุดพูลการเชื่อมต่อสามารถปล่อยการเชื่อมต่อที่ไม่ได้ใช้อย่างช้าๆและลดลงจนกระทั่ง maxidle-> <property name = "maxidle" value = "2" /> <!-ค่าว่างขั้นต่ำ เมื่อจำนวนการเชื่อมต่อที่ไม่ได้ใช้งานน้อยกว่าเกณฑ์พูลการเชื่อมต่อจะใช้ล่วงหน้าสำหรับการเชื่อมต่อบางอย่างเพื่อหลีกเลี่ยงเวลาที่จะใช้เมื่อจุดสูงสุดของน้ำท่วมมา-> <ชื่อคุณสมบัติ = "minidle" value = "1" /> </ebean> <!-ใช้คำอธิบายประกอบเพื่อกำหนดค่าการทำธุรกรรม สำหรับตัวจัดการธุรกรรมของแหล่งข้อมูลการฉีดแหล่งข้อมูลที่เรากำหนดไว้ในแหล่งข้อมูลคุณสมบัติของคลาส DataSourceTransactionManager-> <bean id = "txManager"> <property name = "DataSource" ref = "DataSource" /> </epean> 1.xmlns: tx = "http://www.springframework.org/schema/tx 2.http: //www.springframework.org/schema/tx http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd ใช้คำอธิบายประกอบ @transaction เพื่อใช้ตัวจัดการธุรกรรม-> <tx: การทำธุรกรรม-การทำธุรกรรม DataSource คุณสมบัติ-> <property name = "DataSource" ref = "DataSource"> </property> </ebean> </ebeans>
ผลการดำเนินการ;
2012-3-9 23:30:57 org.springframework.context.support.abstractapplicationcontext ข้อมูลการเตรียมการ: รีเฟรช org.springframework.context.support.classpathxmlapplication context@1a05308: ชื่อแสดงชื่อ [org.springframework.context.support.classpathxmlapplicationcontext@1a05308]; วันที่เริ่มต้น [วันศุกร์ 09 23:30:57 CST 2012]; รากของบริบทลำดับชั้น 2012-3-9 23:30:57 org.springframework.beans.factory.xml.xmlbeanDefinitionReader loadBeanDefinitions: การโหลดคำจำกัดความ XML จากทรัพยากรพา ธ คลาส org.springframework.context.support.abstractaphingContext ข้อมูลข้อมูลที่ได้รับ: โรงงานถั่วสำหรับบริบทแอปพลิเคชัน [org.springframework.context.support.classpathxmlapplicationcontex org.springframework.beans.factory.support.defaultlistablebeanfactory@2bb5142012-3-9 23:30:58 org.springframework.beans.factory.support.defaultlistablebeanfactory org.springframework.beans.factory.support.defaultlistablebeanfactory@2bb514: การกำหนดถั่ว [DataSource, TxManager, org.springframework.aop.config.internalautoproxycreator, org.springframework.transaction.annotation.annotationtransactionattributesour CE#0, org.springframework.transaction.interceptor.TransactionInterceptor#0, org.springframework.transaction.config.internaltransactionAdvisor, บุคคลบริการ]; รากของลำดับชั้นของโรงงาน +++++++++++ รับ person2 tomcat 12 หญิง 3 Hongten 21 ชาย 4 Liufang 21 หญิง 5 Divide 20 ชาย 6 Jone 20 หญิง 7 Su Dongpo 21 ชาย *********************
แน่นอนเราสามารถใช้ไฟล์การกำหนดค่าเพื่อจัดเก็บข้อมูลแหล่งข้อมูลของเรา:
/spring_1100_spring+jdbc/src/jdbc.properties
driverclassName = org.gjt.mm.mysql.driverurl = jdbc/: mysql/: // localhost/: 3307/ฤดูใบไม้ผลิ? useunicode/= true & characterencoding/= utf-8username = rootworder = rootinitialsize = 1maxactive = 300maxidle
ตามลำดับที่จะแก้ไข:
/spring_1100_spring+jdbc/src/bean.xml
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://ww.w3.org/2001/xml xmlns: context = "http://www.springframework.org/schema/context" xmlns: aop = "http://www.springframework.org/schema/aop" xmlns: tx = "http:/ XSI: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://ww.springframework http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://ww.springframework.org/schema http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://ww.springframework.org/schema/ttp:/ JDBC.Properties ไฟล์การกำหนดค่า-> <บริบท: สถานที่ตั้งสถานที่ตั้งตำแหน่ง = "classpath: jdbc.properties" /> <!-แหล่งข้อมูลการกำหนดค่า-> <bean id = "dataSource" destroy-method = "close"> <property name = "value =" $ {driverclassname} " name = "username" value = "$ {username}" /> <property name = "รหัสผ่าน" value = "$ {password}" /> <!-ค่าเริ่มต้นเมื่อพูลการเชื่อมต่อเริ่มต้น-> <property name = "ค่าเริ่มต้น" value = "$ {ค่าเริ่มต้น}" /> <! ค่า. หลังจากเวลาสูงสุดพูลการเชื่อมต่อสามารถปล่อยส่วนหนึ่งของการเชื่อมต่อที่ไม่ได้ใช้ลดลงจนกระทั่ง maxidle-> <property name = "MaxIdle" value = "$ {MaxIdle}" /> <!-ค่าที่ไม่ได้ใช้งานขั้นต่ำ เมื่อจำนวนการเชื่อมต่อที่ไม่ได้ใช้งานน้อยกว่าเกณฑ์พูลการเชื่อมต่อจะใช้ล่วงหน้าสำหรับการเชื่อมต่อบางอย่างเพื่อหลีกเลี่ยงเวลาที่จะใช้เมื่อจุดสูงสุดของน้ำท่วมมาถึง-> <ชื่อคุณสมบัติ = "minidle" value = "$ {minidle}" /> </ebean> <! สำหรับตัวจัดการธุรกรรมของแหล่งข้อมูลการฉีดแหล่งข้อมูลที่เรากำหนดไว้ในแหล่งข้อมูลคุณสมบัติของคลาส DataSourceTransactionManager-> <bean id = "txManager"> <property name = "DataSource" ref = "DataSource" /> </epean> 1.xmlns: tx = "http://www.springframework.org/schema/tx 2.http: //www.springframework.org/schema/tx http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd ใช้คำอธิบายประกอบ @transaction เพื่อใช้ตัวจัดการธุรกรรม-> <tx: การทำธุรกรรม-การทำธุรกรรม DataSource คุณสมบัติ-> <property name = "DataSource" ref = "DataSource"> </property> </ebean> </ebeans> ผลการดำเนินการเหมือนกัน:
2012-3-10 0:23:59 ORG.SPRINGFRAMEWORK.CONTEXT.SUPPORT.ABSTRACTAPPLICINCONTENTENTE การเตรียมข้อมูล: รีเฟรช org.springframework.context.support.classpathxmlapplication [org.springframework.context.support.classpathxmlapplicationcontext@c1b531]; วันที่เริ่มต้น [วันเสาร์ 10 มี.ค. 00:23:59 CST 2012]; รูทของบริบทลำดับชั้น 2012-3-10 0:23:59 org.springframework.beans.factory.xml.xmlbeanDefinitionReader loadbeanDefinitions ข้อมูล: การโหลดคำจำกัดความของถั่ว XML จากทรัพยากรพา ธ org.springframework.context.support.abstractaphingContext ข้อมูลข้อมูลที่ได้รับ: โรงงานถั่วสำหรับบริบทแอปพลิเคชัน [org.springframework.context.support.classpathxmlapplicationcontex org.springframework.beans.factory.support.defaultlistablebeanfactory@1aa57fb2012-3-10 0:23:59 org.springframework.core.io.support.propertiesloadersupport loadproperties 0:23:59 org.springframework.beans.factory.support.defaultlistablebeanfactory preinstantiatesingletons ข้อมูล: singletons ล่วงหน้าใน org.springframework.beans.factory.support.defaultlistable [org.springframework.beans.factory.config.propertyplaceholderconfigurer#0, DataSource, TxManager, org.springframework.aop.config.internalautoproxycreator on.annotationTransactionAtTributesource#0, org.springframework.transaction.interceptor.transactionInterceptor#0, org.springframework.transaction.config.internalTransactionAdvisor รากของลำดับชั้นของโรงงาน +++++++++++++ รับ person2 tomcat 12 หญิง 3 3 Hongten 21 ชาย 4 Liufang 21 หญิง 5 Divide 20 ชาย 6 Jone 20 หญิง 7 Su Dongpo 21 ชาย 8 Su Dongpo 21 ชาย *****************************
ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น