บทความนี้อธิบายวิธีการทั่วไปหลายวิธีในการใช้สปริงเพื่อเชื่อมต่อกับฐานข้อมูล:
คลาสทดสอบหลักคือ:
แพ็คเกจ myspring2; นำเข้า java.sql.*; นำเข้า Javax.sql.datasource; นำเข้า org.springframework.context.applicationContext; นำเข้า org.springframework.context.support.classpathxmlapplicationContext; ctx = ใหม่ classpathxmlapplicationcontext ("applicationcontext.xml"); DataSource dataSource = ctx.getBean ("DataSource", dataSource.class); String sql = "เลือก * จาก user_inf"; การเชื่อมต่อการเชื่อมต่อ = dataSource.getConnection (); คำสั่ง stm = connection.createstatement (); Resultset RS = STM.ExecuteQuery (SQL); ในขณะที่ (rs.next ()) {system.out.println ("ชื่อผู้ใช้:"); System.out.println (Rs.getString (2)); -ประเภทแรก: ใช้ไฟล์กำหนดค่า DriverManagerDataSource ที่มาพร้อมกับฤดูใบไม้ผลิมีดังนี้:
<? xml version = "1.0" encoding = "utf-8"?> <ถั่ว xmlns = "http://www.springframework.org/schema/beans" xmlns: aop = "http: //www.spring framework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xmln s: xsi = "http://www.w3.org/2001/xmlschema-instance" xmlns: บริบท = "http://www.springframework.org/schema/contex XSI: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http:/www.springframework http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://ww.springframework XML schema p การกำหนดค่า namespace-> <bean name = "dataSource" p: driverclassname = "com.mysql.jdbc.driver" p: url = "jdbc: mysql: // localhost: 3306/test" p: username = "presseds เอฟเฟกต์เหมือนกัน-> <!-<bean name = "dataSource"> <property name = "driverclassName" value = "com.mysql.jdbc.driver" /> <property name = "url" value = "jdbc: mysql: // localhost: 3306 /test" /> < value = "123456"/> </ebean> -> </epeans>
ประเภทที่สอง: แหล่งข้อมูล C3P0
คุณต้องสร้างแพ็คเกจ Core Jar ของ C3P0 ฉันใช้ C3P0-0.9.1.JAR ซึ่งค่อนข้างเสถียรและแนะนำให้ใช้ โดยทั่วไปเมื่อดาวน์โหลด Hibernate ฉันจะนำหนึ่งมา: ฉันพบมันภายใต้ Hibernate-release-4.3.0.final/lib/optional/c3p0 Path
ไฟล์กำหนดค่ามีดังนี้:
<? xml version = "1.0" encoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: aop = "http://www.springframework.org/schema/aop" xmlns: tx = "http://www.springframework.org/schema/tx" xmlns: xsi = "http://www.w3.org/2001/xmlschema-instance" xmlns: บริบท = "http://www.springframework.org/schema/context XSI: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http:/www.springframework http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.to <bean name = "dataSource" p: driverclass = "com.mysql.jdbc.driver" p: jdbcurl = "jdbc: mysql: // localhost: 3306/test" p: user = "root" p: password = "123456" ขอแนะนำให้ใช้ด้านบน-> <!-<bean name = "dataSource"> <property name = "driverclass" value = "com.mysql.jdbc.driver" /> <property name = "jdbcurl" value = "jdbc: mysql: // localhost: 3306 /ทดสอบ value = "123456"/> </ebean> -> </epeans>
ประเภทที่สาม:
ใช้ปลั๊กอิน DBCP ของ Apache เพื่อเชื่อมต่อกับฐานข้อมูลเพื่อดาวน์โหลดแพ็คเกจ JAR: Commons-dbcp.jar, Commons-pool.jar, Commons-collection.jar
ไฟล์การกำหนดค่าของสปริงมีดังนี้:
<? xml version = "1.0" encoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: aop = "http://www.springframework xmlns: tx = "http://www.springframework.org/schema/tx" xmlns: xsi = "http://www.w3.org/2001/xmlschema-instance xmlns: p = "http://www.springframework.org/schema/p" xsi: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "> <!-p namespace การกำหนดค่าโดยใช้ XML schema-> <bean name =" DataSource " p: url = "jdbc: mysql: // localhost: 3306/test" p: username = "root" p: password = "123456"> </ebean> <!- การใช้การกำหนดค่าปกติของคุณสมบัติเป็นปัญหาเล็กน้อย ขอแนะนำให้ใช้ด้านบน-> <!-<bean name = "dataSource"> <property name = "driverclassName" value = "com.mysql.jdbc.driver" /> <property name = "url" value = "jdbc: mysql: // localhost: 3306 /test" /> value = "123456"/> </ebean> -> </epeans>
ประเภทที่สี่:
การใช้แหล่งข้อมูลไฮเบอร์เนตต้องใช้แพ็คเกจ Jar Hiberante Core, รุ่นของ Hibernate1 ที่ฉันใช้คือ Hibernate-release-4.3.0.final
ปัจจุบันสามเฟรมเวิร์กหลักได้รับความนิยมค่อนข้างมาก ฤดูใบไม้ผลิเป็นพันธมิตรกับ Hiberante โดยทั่วไป วิธีการเชื่อมต่อฐานข้อมูลถูกเขียนในไฟล์การกำหนดค่า Hiberante ไฟล์การกำหนดค่าในสปริงการจัดการของไฮเบอร์เนต
เพียงอ่านไฟล์กำหนดค่า Hibernate Core โดยตรง เมื่อใช้ไฮเบอร์เนตเพื่อเชื่อมต่อกับฐานข้อมูลคุณต้องอ่านไฟล์การกำหนดค่า hibernate.cfg.xml และคลาสเอนทิตีที่สอดคล้องกัน
ผู้อ่านสามารถอ้างถึงการกำหนดค่าต่อไปนี้
<bean id = "SessionFactory"> <property name = "configlocations"> <list> <dalue> classpath: com/config/hibernate.cfg.xml </value> </list> </porement> <property name = "mapinglocations"> <! </property>
ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น