이 기사는 Spring을 사용하여 데이터베이스에 연결하는 몇 가지 일반적인 방법을 간략하게 설명합니다.
주요 테스트 클래스는 다음과 같습니다.
패키지 myspring2; import java.sql.*; import javax.sql.datasource; import org.springframework.context.applicationcontext; import org.springframework.context.support.classpathxmlapplicationcontext; public myspringtest {)) gritic void main (). ApplicationContext CTX = 새 ClassPathXmlApplicationContext ( "ApplicationContext.xml"); DataSource DataSource = ctx.getBean ( "DataSource", DataSource.class); 문자열 sql = "select * from user_inf"; 연결 연결 = DataSource.getConnection (); 문자 stm = connection.createstatement (); resultSet rs = stm.ExecuteQuery (SQL); while (rs.next ()) {system.out.println ( "username :"); System.out.println (rs.getstring (2)); }}}첫 번째 유형 : Spring과 함께 제공되는 DriverManagerDataSource 구성 파일 사용은 다음과 같습니다.
<? xml 버전 = "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.springfrfamwork.org/schema/tx s : xsi = "http://www.w3.org/2001/xmlschema-instance"xmlns : context = "http://www.springframework.org/schema/context"xmlns : p = "http://www.springframwork.org/schema/p. xsi : schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx 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.springframework.org/schema/sprger-aop--33.0.xml p 네임 스페이스 구성-> <bean name = "dataSource"p : driver className = "com.mysql.jdbc.driver"p : url = "jdbc : mysql : // localhost : 3306/test"p : username = "root"p : password = "123456"/> <! 동일,-> <!-<bean name = "dataSource"> <property name = "driverclassName"value = "com.mysql.jdbc.driver" /> <속성 이름 = "url"value = "jdbc : mysql : // localhost : 3306 /test" /> <property name = "username" "root" /> </bean> -> </beans>
두 번째 유형 : C3P0 데이터 소스.
C3P0의 코어 JAR 패키지를 만들어야합니다. C3P0-0.9.1.jar를 사용합니다. 이는 비교적 안정적이며 사용하는 것이 좋습니다. 일반적으로 최대 절전 모드를 다운로드 할 때는 하나를 가져올 것입니다. Hibernate release-4.3.0.final/lib/optional/c3p0 경로에서 찾았습니다.
구성 파일은 다음과 같습니다.
<? 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://www.spramframframwork.org/org/g. xsi : schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 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/sprring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">-xml schema를 사용한 P NONSPIGURATIO 이름 = "dataSource"p : driverclass = "com.mysql.jdbc.driver"p : jdbcurl = "jdbc : mysql : // localhost : 3306/test"p : user = "root"p : password = "123456"> </bean> <!- 일반 구성을 사용하는 것은 약간 동일합니다. 위의 위의-> <!-<bean name = "dataSource"> <property name = "driver class"value = "com.mysql.jdbc.jdbc.jdbc.jdbc.driver" /> <property name = "jdbcurl"value = "jdbc : mysql : // localhost : 3306 /test"= "use"valess "" ///////crembower name = ""root "" / / / / / /test name = "jdbc를 사용하는 것이 좋습니다. 값 = "123456"/> </bean> -> </beans>
세 번째 유형 :
Apache의 DBCP 플러그인을 사용하여 데이터베이스에 연결하여 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.org/aop" xmlns : tx = "http://www.springframework.org/schema/tx"xmlns : xsi = "http://www.w3.org/2001/xmlschema-instance"xmlns : context = "http://www.spramfram.org/context. xmlns : p = "http://www.springframework.org/schema/p"xsi : schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/schema/sprone-beans-.schema http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.spramework.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 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "> <!-p 네임 스페이스 구성 xml schema를 사용한 네임 스페이스 구성-> <bean name ="dataSource "p : driver className ="com.mysql.jdbc.driver " p : url = "jdbc : mysql : // localhost : 3306/test"p : username = "root"p : password = "123456"> </bean> <!- 일반적인 속성 구성을 사용하는 것은 약간 번거롭지 만 효과는 동일합니다. 위의 위의-> <!-<bean name = "dataSource"> <property name = "driver className"value = "com.mysql.jdbc.jdbc.jdbc.jdbc.jdbc.jdbc.jdbc.jdbc.jdbc.jdbc.jdbc.jdbc.jdbc.jdbc.driver" /> <property name = "url"value = "jdbc : mysql : // localhost : 3306 /test"<property name = "username" "use <property name ="username "" ""/////////crampalding 값 = "123456"/> </bean> -> </beans>
네 번째 유형 :
Hibernate Data Source를 사용하면 Hiberante Core Jar 패키지가 필요합니다.
현재 세 가지 주요 프레임 워크는 매우 인기가 있습니다. Spring은 일반적으로 Hiberante와 파트너십을 맺습니다. 데이터베이스 연결 방법은 Hiberante 구성 파일로 작성됩니다. 최대 절전 모드 스프링 관리의 구성 파일
, 최대 절전 모드 코어 구성 파일을 직접 읽으십시오. Hibernate를 사용하여 데이터베이스에 연결하면 Hibernate.cfg.xml 구성 파일 및 해당 엔터티 클래스를 읽어야합니다.
독자는 다음 구성을 참조 할 수 있습니다
<bean id = "sessionFactory"> <속성 이름 = "configlocations"> <list> <value> classpath : com/config/hibernate.cfg.xml </value> </list> </property> <property name = "mappinglocations"> <!-모든 엔트리 클래스 매핑 파일-> <list> <valuepath : com/list>. </property>
위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.