Dieser Artikel erläutert kurz mehrere gängige Methoden zur Verwendung von Spring zur Verbindung zu Datenbanken:
Die Hauptprüfklasse ist:
Paket mySpring2; import Java.sql. ApplicationContext ctx = new classPathXmlApplicationContext ("applicationContext.xml"); DataSource dataSource = ctx.getbean ("DataSource", dataSource.class); String SQL = "Select * aus user_inf"; Connection Connection = dataSource.getConnection (); Anweisung STM = Connection.CreateStatement (); ResultSet rs = stm.executeQuery (SQL); while (rs.Next ()) {System.out.println ("Benutzername:"); System.out.println (Rs.getString (2)); }}}Der erste Typ: Verwenden Sie die DriverManagerDataSource -Konfigurationsdatei, die mit Spring geliefert wird, wie folgt:
<xml Version = "1.0" coding = "utf-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: aop = "http://www.springframework.org/schema/aop" xmlns: tx = "http://wwwww.SpringFramework.Org/SchaM. S: xsi = "http://www.w3.org/2001/xmlschema-instance" xmlns: context = "http://www.spingframework.org/schema/context" xmlns: p = "http XSI: Schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.sspringFramework 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://www.spingframe.org/Schema/aop/aop./aop./APRING/APRING-ASPRING-3.0.0.1.-! XML-Schema P-Namespace-Konfiguration-> <bean name = "dataSource" p: driverClassName = "com.mysql.jdbc.driver" p: url = "jdbc: mysql: // localhost: 3306/test" p: userername = "root" p: passwods = "123456"/> <!-use Normale. Der Effekt ist der gleiche,-> <!-<bean name = "dataSource"> <Eigenschaft name = "triverClassName" value = "com.mysql.jdbc.driver value = "123456"/> </bean> -> </beans>
Der zweite Typ: C3P0 -Datenquelle.
Sie müssen das Kern -JAR -Paket von C3P0 erstellen. Ich benutze C3P0-0.9.1.jar, das relativ stabil und empfohlen ist. Im Allgemeinen werde ich beim Herunterladen von Hibernate einen mitbringen: Ich habe es unter dem Hibernate-Release-4.3.0.final/lib/optional/c3p0 Path gefunden.
Die Konfigurationsdatei lautet wie folgt:
<? 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 XSI: Schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.sspringFramework http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/context/context/context/context/contex/contex/contex/contex/context/context/contex/conted./Sconted. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "> <! name = "dataSource" p: driverClass = "com.mysql.jdbc.driver" p: jdbcurl = "jdbc: mysql: // localhost: 3306/test" p: user = "root" p: password = "123456"> </bean> <!! Es wird empfohlen, das obige-> <!-<bean name = "dataSource"> <Eigenschaft name = "triverClass" value = "com.mysql.jdbc.driver" /> <Eigenschaft Name = "Jdbcurl" value = "jdbc: mysql: // Localhost: 3306 /test" /> <Stest " /> <Stest" /> <Stest " /> <Stest" /> <Stest " /> <Stest" /> "<-"-"-"-"-"-"-"-"-"-"-" -Root "-Root". value = "123456"/> </bean> -> </beans>
Der dritte Typ:
Verwenden Sie das DBCP-Plugin von Apache, um eine Verbindung zur Datenbank herzustellen, um die JAR-Pakete herunterzuladen: commons-dbcp.jar, commons-pool.jar, columection.jar
Die Konfigurationsdatei von Spring lautet wie folgt:
<? xmlns: tx = "http://www.springframework.org/schema/tx" xmlns: xsi = "http://www.w3.org/2001/xmlschema-sinstance xmlns: p = "http://www.springframework.org/schema/p" xsi: schemalocation = "http://www.springframework.org/schema/beans http://www.springframe.org/schema/beans/Spring-bans- 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.spingframework.org/sschema/aop. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "> <!-P-Namespace-Konfiguration mit XML Schema-> <Bean name =" dataSource "p: driverClassname =" com.mysql.jdbc.ddrver "" p: url = "jdbc: mysql: // localhost: 3306/test" p: userername = "root" p: password = "123456"> </bean> <!- Die normale Konfiguration der Eigenschaft ist etwas problematisch, aber der Effekt ist derselbe. Es wird empfohlen, das obige-> <!-<bean name = "dataSource"> <Eigenschaft name = "DriverClassName" value = "com.mysql.jdbc.driver" /> <Eigenschaft name = "url" value = "jdbc: mysql: // localHost: 3306 /test" /u. value = "123456"/> </bean> -> </beans>
Der vierte Typ:
Die Verwendung von Hibernate-Datenquellen erfordert das Hiberante-Kern-JAR-Paket. Die Version von Hibernate1, die ich verwende
Derzeit sind die drei großen Rahmenbedingungen sehr beliebt. Der Frühling ist im Allgemeinen mit Hiberante zusammengearbeitet. Die Datenbankverbindungsmethode ist in der Hiberante -Konfigurationsdatei geschrieben. Die Konfigurationsdatei im Frühjahrsmanagement von Hibernate
Lesen Sie einfach die Hibernate -Kernkonfigurationsdatei direkt durch. Wenn Sie Hibernate zur Verbindung zur Datenbank verwenden, müssen Sie die Konfigurationsdatei hibernate.cfg.xml und entsprechende Entitätsklassen lesen.
Die Leser können sich auf die folgende Konfiguration beziehen
<bean id = "sessionFactory"> <Eigenschaft name = "configLocations"> <list> <Wirast> classpath: com/config/hibernate.cfg.xml </value> </list> </Eigenschaft> <Eigenschaft Name = "MappingLocations"> <!-Alle Entitity-Klassen-Mapping-Dateien-Wert> <value> Class> com/hibernate/hibernate: </Property>
Das obige ist der gesamte Inhalt dieses Artikels. Ich hoffe, es wird für das Lernen aller hilfreich sein und ich hoffe, jeder wird Wulin.com mehr unterstützen.