Artikel ini secara singkat menjelaskan beberapa metode umum menggunakan Spring untuk terhubung ke database:
Kelas tes utama adalah:
Paket myspring2; impor java.sql.*; import javax.sql.datasource; impor org.springframework.context.applicationContext; impor org. ApplicationContext ctx = new ClassPathXMLapPlicationContext ("ApplicationContext.xml"); DataSource DataSource = ctx.getBean ("DataSource", DataSource.class); String sql = "pilih * dari user_inf"; Koneksi koneksi = DataSource.getConnection (); Pernyataan stm = connection.createStatement (); Hasil RS = STM.ExecuteQuery (SQL); while (rs.next ()) {System.out.println ("Nama pengguna:"); System.out.println (rs.getString (2)); }}}Jenis pertama: Gunakan file konfigurasi driverManagerDataSource yang dilengkapi dengan pegas adalah sebagai berikut:
<? Xml Version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: aop = "http://www.springframework.org/schema S: xsi = "http://www.w3.org/2001/xmlschema-instance" xmlns: context = "http://www.springframework.org/schema/context" xmlns: p = "http:/schema/context" xmlns: p = "http:/schema.smem.sham.sham.smema.org.gwork.org.smema.org.smema.org.shames XSI: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http:/wwww.springfram. 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.spramorkework.org/schema/aop http://www.springframework.org/schema/aop:aop:aop Skema p namespace konfigurasi-> <bean name = "datasource" p: driverclassname = "com.mysql.jdbc.driver" p: url = "jdbc: mysql: // localhost: 3306/test" p: username = "root" p: kata sandi = "123456"/test "p: Username =" root "p: kata sandi =" 123456 "/test" p: USERNAME = "root" p: kata sandi = "123456" sama,-> <!-<bean name = "DataSource"> <properti name = "driverclassname" value = "com.mysql.jdbc.driver" /> <name properti = "url" value = "jdbc: mysql: // localhost: 3306 /test" /<name jdbc = "mysql: // localhost: 3306 /test" /<a name = "name =" USERN = "" "" "" "" "" name "name" name "" value = "123456"/> </t bean> -> </tagel>
Jenis kedua: Sumber data C3P0.
Anda perlu membuat paket toples inti C3P0. Saya menggunakan C3P0-0.9.1.jar, yang relatif stabil dan disarankan untuk digunakan. Secara umum, saat mengunduh hibernate, saya akan membawa satu: Saya menemukannya di bawah jalur hibernate-release-4.3.0.final/lib/optional/c3p0.
File konfigurasi adalah sebagai berikut:
<? 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: context = "http://www.springframework.org/schema/context" xmlns: p = "http:or/schema/context" xmlns: p = "http:pfor XSI: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http:/wwww.springfram. http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop http://www.springfringewework.org/schema/aop/spring-aop-3.0.xsd "nol" nean/naop name = "DataSource" p: driverclass = "com.mysql.jdbc.driver" p: jdbcurl = "jdbc: mysql: // localhost: 3306/test" p: user = "root" p: kata sandi = "123456"> </bean> <!- menggunakan konfigurasi normal. It is recommended to use the above --> <!-- <bean name="dataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver" /> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test" /> <property name="user" value="root" /> <property name="password" value = "123456"/> </t bean> -> </tagel>
Tipe ketiga:
Gunakan plugin DBCP Apache untuk terhubung ke database untuk mengunduh paket JAR: commons-dbcp.jar, commons-pool.jar, commons-collection.jar
File konfigurasi pegas adalah sebagai berikut:
<? Xml Version = "1.0" encoding = "UTF-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: aop = "http://www.springframework.org/orga/aPema/http://www.spramework.org/orga/aPema/sphema/www.spramework.org/orga/aPema/aPema/http" xmlns: tx = "http://www.springframework.org/schema/tx" xmlns: xsi = "http://www.w3.org/2001/xmlschema-instance" xmlns: konteks = "http: xmlns: p = "http://www.springframework.org/schema/p" xsi: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.orgal http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop://www.spramework.org/schema http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "> <!-p namespace konfigurasi menggunakan skema xml-> <name bean =" dataSource "p: driverclassname =" com.mysql.jdbc.driver " P: url = "jdbc: mysql: // localhost: 3306/test" p: username = "root" p: password = "123456"> </bean> <!- Menggunakan konfigurasi properti normal agak merepotkan, tetapi efeknya sama. Disarankan untuk menggunakan yang di atas-> <!-<bean name = "DataSource"> <name properti = "driverclassname" value = "com.mysql.jdbc.driver" /> <name properti = "url" nilai = "jdbc: mysql: // localhost: 3306 /uji" /> <"name =" name = "" name "" name "" name "" name "" name "" name "" name "" name "" value = "123456"/> </t bean> -> </tagel>
Tipe Keempat:
Menggunakan Sumber Data Hibernate membutuhkan paket jar hiberante, versi hibernate1 yang saya gunakan adalah hibernate-rilis-4.3.0.Final
Saat ini, tiga kerangka kerja utama cukup populer. Musim semi umumnya bermitra dengan Hiberante. Metode koneksi database ditulis dalam file konfigurasi Hiberante. File konfigurasi dalam manajemen pegas hibernate
, cukup baca file konfigurasi inti hibernate secara langsung. Saat menggunakan Hibernate untuk terhubung ke database, Anda perlu membaca file konfigurasi hibernate.cfg.xml dan kelas entitas yang sesuai.
Pembaca dapat merujuk ke konfigurasi berikut
<bean id = "sessionFactory"> <name properti = "configLocations"> <list> <value> classpath: com/config/hibernate.cfg.xml </ value> </list> </prop Property> <Name Property = "MappingLocations"> <! </prop Propert>
Di atas adalah semua konten artikel ini. Saya berharap ini akan membantu untuk pembelajaran semua orang dan saya harap semua orang akan lebih mendukung wulin.com.