インターネット上での春と冬眠の統合に関する多くの記事を見てきましたが、それらの記事は以前に書かれたため、それらの多くはSpring 3やHibernate 4などの古いバージョンでした。したがって、ここで更新されたバージョンを使用して説明します。
プロジェクトの依存関係を追加します
まず第一に、Java Webプロジェクトが必要です。これは、ソフトウェアの依存関係を促進するためにMavenまたはGradle Build Toolsを使用するのが最適です。ここではGradleビルドツールを使用しています。ビルドスクリプトは次のとおりです。 Spring-WebMVCとSpring-ORMの2つのパッケージを導入する限り、他のスプリング依存関係はビルドツールによって自動的に解決されます。また、データソース、Hibernate、JSTLなどの依存関係を導入する必要があります。スクリプトは、Mavenツールで簡単に使用できるように対応するPOMファイルを生成するタスクを定義します。
Group 'yitian.learn'version' 1.0-snapshot'applyプラグイン: 'java'applyプラグイン:' war'applyプラグイン: 'maven'apply from:' https://raw.github.com/akhikhl/gretty/master/pluginscripts/gretty.plugin'sourcecomposificiutiuiiual url "http://maven.aliyun.com/nexus/content/groups/public/" } jcenter()}ext {springVersion = '4.3.6.RELEASE' aspectjVerison = '1.8.10'} dependencies { testCompile group: 'junit', name: 'junit', version: '4.12' compile group: 'org.springframework'、name: 'spring-webmvc'、version:springversion compileグループ: 'org.springframework'、name: 'spring-version compileグループ:' org.glassfish.web '、name:' jstl-impl '、' jstl-impl '、バージョン:' 1.2 '1.16.12'コンパイルグループ: 'org.hibernate'、name: 'hibernate-core'、version: '5.2.6.final' compileグループ: 'mysql'、name: 'mysql-connector-java'、バージョン: '5.1.40'コンパイルグループ: 'org.commons.commons'、 'commonesグループ: 'org.aspectj'、name: 'aspectjweaver'、version:aspectjverison} task writeNewpom {dolast {pom {} .writeto( "$ projectdir/pom.xml")}}}web.xmlを構成します
次に、web-inf/web.xmlファイルを開き、次のコンテンツを追加します。
<?xml version = "1.0" encoding = "utf-8"?> <web-app xmlns = "http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi = "http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation = "http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"バージョン= "3.1" <param-value> /web-inf/applicationcontext.xml </param-value> </context-param> <servlet-name> dispatcher </servlet-name> <servlet-class> org.springframework.web.servlet.dispatcherservlet </servet-on-start> <load-on-start> <async-supported> true </async-supported> </servlet> <servlet-name> dispatcher </servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <リスナー> <リスナークラス> org.spramework.web.context.context.context.contextlowseder </リスナー> </web-app>
スプリングを構成します
Spring Configuration Files /Web-inf/applicationContext.xmlおよび/web-inf/dispatcher-servlet.xmlが2つある必要があります。前者はルート構成ファイルであり、データベースなどのバックエンドおよびグローバルコンポーネントの構成に使用され、後者はMVC構成ファイルであり、MVCおよびWeb関連コンポーネントの構成に使用されます。
次に、/web-inf/applicationcontext.xmlで、冬眠とスプリングを統合するコンポーネントを構成します。データソース、HibernatesessionFactory、Hibernate Transaction Manager、Transaction Connection Point、Hibernateテンプレート、その他の豆を構成し、データを操作するときにHibernateテンプレートを使用して、Springによって制御されるトランザクション管理機能を取得する必要があります。
<?xml version = "1.0" encoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns:xsi = "http://www.w3.org/2001/xmlschema-instance" xmlns:tx = "http://www.springframework.org/schema/tx" xmlns:aop = "http://www.springframework.org/schema/aop" xmlns:context = "http://www.springfrywork xsi:schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> <context:annotation-config/> <! - データソース - > <bean id =" destroperce ""> <jetource " name = "DriverClassName" value = "com.mysql.jdbc.driver"/> <プロパティ名= "url" value = "jdbc:mysql:// localhost:3306/test"/> <プロパティ名= "username" value = "root"/> <プロパティname = "propertial" pardass = "/< id = "SessionFactory"> <プロパティ名= "dataSource" ref = "dataSource"/> <プロパティ名= "hibernateproperties"> <props> <prop key = "hibernate.format_sql"> prop> <prop key = "hibernate.show_sql"> "hibernate.show_sql" key = "hibernate.hbm2ddl.auto"> create </prop> </props> </property> <property name = "packagestoscan" value = "yitian.learn.entity"/> </bean> <! - set hibernateテンプレート - > <bean id = "himbernateTemplate"/bemint " <! - Hibernate Transaction Managerを設定 - > <Bean ID = "TransactionManager"> <Property name = "SessionFactory" Ref = "SessionFactory"/> </bean> <! - Set Hibernate Transaction Manager-> <Bean ID = "TransactionManager"> <Property Name = "SessionFactory" Ref "/> </bean> </bean id = "userdao"/> <! - トランザクション管理の設定 - > <tx:アドバイスid = "txadvice"トランザクションマネージャー= "トランザクションマネージャー"> <tx:属性> <tx:method name = "find*" read-only = "true"/> <tx:method name = "*"/> < <aop:config> <aop:pointcut id = "userdaopointcut" expression = "execution(*yitian.learn.dao。*
次に、Spring Web MVCのコンポーネントを構成します。 Dispatcher-servlet.xmlに次の構成を追加します。ここでは、JSPビューパーサーとタイプコンバーターを追加します。カスタムタイプの変換が必要な場合は、対応するフラグメントを削除できます。
<?xml version = "1.0" encoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns:xsi = "http://www.w3.org/2001/xmlschema-instance" XMLNS:MVC = "http://www.springframework.org/schema/mvc" xmlns:context = "http://www.springframework.org/schema/context" xsi:schemalocation = " http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> <MVC:View-Resolvers> <MVC:JSP ="/web-inf/jsp = jsp view-class = "org.springframework.web.servlet.view.jstlview"/> </mvc:view-resolvers> <mvc:default-servlet handler/> <mvc:annotation-driven conversion-service = "conventionservice"/>>> <コンテキスト= " <プロパティ名= "converters"> <set> <bean/> </set> </property> </bean> </beans>
この時点で、冬眠と春の間の統合が完全に構成されています。最後に、私は小さな例を書き、Githubに置きました。興味のある学生は見ることができます。
要約します
上記は、この記事のSpring Web MVCとHibernateの統合構成のすべての詳細な説明です。すべての人に役立つことを願っています。興味のある友人は、このサイトの他の関連トピックを引き続き参照できます。欠点がある場合は、それを指摘するためにメッセージを残してください。このサイトへのご支援をありがとうございました!