تشرح هذه المقالة بإيجاز العديد من الطرق الشائعة لاستخدام الربيع للاتصال بقواعد البيانات:
فئة الاختبار الرئيسية هي:
حزمة myspring2 ؛ استيراد java.sql.*؛ استيراد javax.sql.datasource ؛ استيراد org.springframework.context.applicationContext ؛ import org.springframework.context.support.classpathxmlictoxt ؛ ApplicationContext ctx = جديد classPathxMlAppLicationContext ("ApplicationContext.xml") ؛ datasource dataSource = ctx.getBean ("datasource" ، datasource.class) ؛ String SQL = "SELECT * من user_inf" ؛ اتصال الاتصال = datasource.getConnection () ؛ بيان stm = connection.createstatement () ؛ resultset rs = stm.executequery (SQL) ؛ بينما (rs.next ()) {system.out.println ("اسم المستخدم:") ؛ System.out.println (Rs.GetString (2)) ؛ }}}النوع الأول: استخدم ملف تكوين DriverManagerDataSource الذي يأتي مع الربيع كما يلي:
<؟ 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" XSI: schemalocation = "http://www.springframework.org/schema/beans 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 <!-استخدم تكوين مساحة اسم Schema P Schema P-> <bean name = "datasource" p: driverClassName = "com.mysql.jdbc.driver" p: url = "jdbc: mysql: // localhost: 3306/test" p: username = "root" p: "password =" المقارنة ، ولكن التأثير هو نفسه ،-> <!-<bean name = "datasource"> <property name = "driverClassName" value = "com.mysql.jdbc.driver" /> <property name = "url" value = "ualpt" = "uspres". value = "123456"/> </ban> -> </bans>
النوع الثاني: مصدر بيانات C3P0.
تحتاج إلى جعل حزمة الجرة الأساسية من C3P0. يمكنني استخدام C3P0-0.9.1.jar ، وهو مستقر نسبيًا وموصى به. بشكل عام ، عند تنزيل Hibernate ، سأحضر واحدة: لقد وجدته تحت مسار الإطفاء-RELEASE-4.3.0.Final/LIB/OPTIONAL/C3P0.
ملف التكوين كما يلي:
<؟ 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" XSI: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/Aop <bean name = "datasource" p: driverclass = "com.mysql.jdbc.driver" p: jdbcurl = "jdbc: mysql: // localhost: 3306/test" p: user = "root" p: "123456"> </ يوصى باستخدام ما ورد أعلاه-> <!-<bean name = "dataSource"> <property name = "driverclass" value = "com.mysql.jdbc.driver" /> <property name = "jdbcurl value = "123456"/> </ban> -> </bans>
النوع الثالث:
استخدم المكون الإضافي DBCP من Apache للاتصال بقاعدة البيانات لتنزيل حزم الجرة: Commons-DBCP.JAR ، Commons-Pool.jar ، Commons-Collection.jar
ملف تكوين الربيع كما يلي:
<؟ xmlns: tx = "http://www.springframework.org/schema/tx" xmlns: xsi = "http://www.w3.org/2001/xmlschema- xmlns: context = "http://www.springframework.org/schema/context" xmlns: p = "http://www.springframework.org/schema/p" http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "> <! P: driverClassName = "com.mysql.jdbc.driver" p: url = "jdbc: mysql: // localhost: 3306/test" p: username = "root" p: password = "123456"> </bean> <! يوصى باستخدام ما ورد أعلاه-> <!-<bean name = "dataSource"> <property name = "driverClassName" value = "com.mysql.jdbc.driver" /> <property name = "url" value = "jdbc: mysql: // localhost: 3306 /test" /> userern value = "123456"/> </ban> -> </bans>
النوع الرابع:
يتطلب استخدام مصدر بيانات Hibernate حزمة جرة Hibrante Core ، وهو إصدار Hibernate1 الذي أستخدمه هو heibernate-release-4.3.0.final
حاليًا ، تحظى الأطر الثلاثة الرئيسية بشعبية كبيرة. يتم شراكة الربيع عمومًا مع Hiberante. تتم كتابة طريقة اتصال قاعدة البيانات في ملف تكوين Hibrante. ملف التكوين في إدارة الربيع من السباتي
، ما عليك سوى قراءة ملف التكوين الأساسي لـ Hibernate مباشرة. عند استخدام Hibernate للاتصال بقاعدة البيانات ، تحتاج إلى قراءة ملف تكوين hibernate.cfg.xml وفئات الكيانات المقابلة.
يمكن للقراء الرجوع إلى التكوين التالي
<bean id = "sessionfactory"> <property name = "configlocations"> <list> <value> classpath: com/config/hibernate.cfg.xml </value> </list> </sprention> <property name = "mappinglocations">! </property>
ما سبق هو كل محتوى هذه المقالة. آمل أن يكون ذلك مفيدًا لتعلم الجميع وآمل أن يدعم الجميع wulin.com أكثر.