この記事では、Springを使用してデータベースに接続するいくつかの一般的な方法について簡単に説明します。
主なテストクラスは次のとおりです。
packy myspring2; Import java.sql。*; Import javax.sql.datasource; import org.springframework.context.applicationcontext; import org.springframework.context.support.classpathxmlapplicationcontext;公開クラスMyspringtest {] string bioid() ApplicationContext ctx = new ClassPathxMLApplicationContext( "ApplicationContext.xml"); DataSource DataSource = ctx.getBean( "DataSource"、dataSource.class);文字列sql = "select * from user_inf";接続接続= dataSource.getConnection();ステートメントstm = connection.createStatement();結果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.spring framework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xmln s:xsi = "http://www.w3.org/2001/xmlschema-instance" xmlns:context = "http://www.springframework.org/schema/context" xmlns:p = "http://ww.springframework. 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.sspringframeworkwork.org/schema/spring-m.0.c. xml schema p namespace configuration-> <bean name = "dataSource" p:driverclassname = "com.mysql.jdbc.driver" p:url = "jdbc:// localhost:3306/test" p:username = "root" p:purdert = "purffert on on in nomer on-use ot in nom->しかし、効果は同じです - > <! - <bean name = "dataSource"> <プロパティ名= "driverclassName" value = "com.mysql.jdbc.driver" /> <プロパティ名= "url" value = " value = "123456"/> </bean> - > </beans>
2番目のタイプ:C3P0データソース。
C3P0のコアジャーパッケージを作成する必要があります。 C3P0-0.9.1.Jarを使用しています。これは比較的安定しており、推奨されています。一般的に、Hibernateをダウンロードするとき、私は1つを持ってきます:私はそれを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 = " 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-/spring-context-/spring-contex http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xxd "> <bean name = "dataSource" p:driverclass = "com.mysql.jdbc.driver" p:jdbcurl = "jdbc:mysql:// localhost:3306/test" p:user = "root" p:パスワード= "123456"> </bean> <上記を使用することをお勧めします。<bean name = "dataSource"> <プロパティ名= "driverclass" value = "com.mysql.jdbc.driver" /> <プロパティ名= "jdbcurl" value = " value = "123456"/> </bean> - > </beans>
3番目のタイプ:
ApacheのDBCPプラグインを使用してデータベースに接続してJARパッケージをダウンロードします:commons-dbcp.jar、commons-pool.jar、commons-collection.jar
Springの構成ファイルは次のとおりです。
<?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.springframework.org/schema/p" xsi:schemalocation = "http://www.springframework http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework/ 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 http://www.springframewark.org/schema/aop/spring-aop-3.0.xsd "> <! - xml schemaを使用したPネームスペース構成 - P:driverClassName = "com.mysql.jdbc.driver" p:url = "jdbc:mysql:// localhost:3306/test" p:username = "root" p:password = "123456"> </bean> <! - 通常の構成を使用することは、少しトラブルです。上記を使用することをお勧めします - > <! - <bean name = "datasource"> <プロパティ名= "driverclassname" value = "com.mysql.jdbc.driver" /> <プロパティ名= "url"値= " value = "123456"/> </bean> - > </beans>
4番目のタイプ:
Hibernateデータソースを使用するには、Hiberante Core Jarパッケージが必要です。
現在、3つの主要なフレームワークは非常に人気があります。春は一般的にHiberanteと提携しています。データベース接続方法は、Hiberante構成ファイルに記述されています。 HibernateのSpring Managementの構成ファイル
、Hibernate Core Configurationファイルを直接お読みください。 Hibernateを使用してデータベースに接続する場合、hibernate.cfg.xml構成ファイルと対応するエンティティクラスを読む必要があります。
読者は次の構成を参照できます
<bean id = "sessionfactory"> <プロパティ名= "configlocations"> <list> <value> classpath:com/config/hibernate.cfg.xml </value> </list> </property> <property name = "mappinglocations"> <! - すべてのエンティティクラスマッピングファイル - > <> <> <> <> </higbernate/</higbernate/ </property>
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。