나는 인터넷에서 Spring과 Hibernate의 통합에 관한 많은 기사를 보았지만, 그 기사는 이전에 작성 되었기 때문에 Spring 3 및 Hibernate 4와 같은 이전 버전은 여기에서 업데이트 된 버전을 사용하여 설명하겠습니다.
프로젝트 종속성을 추가하십시오
우선, 우리는 소프트웨어 종속성을 용이하게하기 위해 Maven 또는 Gradle 빌드 도구를 사용하는 데 가장 적합한 Java 웹 프로젝트가 필요합니다. 여기에 Gradle 빌드 도구를 사용하고 있습니다. 빌드 스크립트는 다음과 같습니다. Spring-Webmvc와 Spring-Orm의 두 패키지를 소개하는 한, 다른 스프링 의존성은 빌드 도구에 의해 자동으로 해결됩니다. 그런 다음 데이터 소스, 최대 절전 모드, JSTL 등과 같은 종속성을 도입해야합니다. 스크립트는 Maven 도구에서 쉽게 사용할 수 있도록 해당 POM 파일을 생성하는 작업을 정의합니다.
Group 'yitian.learn'version'1.0-snapshot'Apply 플러그인 : 'Java'Apply 플러그인 :'War'Apply 플러그인 : 'maven'Apply from :'https://raw.github.com/akhikhl/gretty/mas "http://maven.aliyun.com/nexus/content/groups/public/"} jcenter ()} ext {springversion = '4.3.6.release'agficjverison = '1.8.10'} {testcompile group : 'junit', 'junit', '4.12'compile group : 'org. 이름 : 'Spring-Webmvc', 버전 : Springversion Compile Group : 'org.springframework', 이름 : 'Spring-Orm', 버전 : Springversion Compile Group : 'org.glassfish.web', 이름 : 'JSTL-IMPL', 버전 : '1.2'Compile Group : 'org.projectlombok', 'lembok', '1.16 .12. 'org.hibernate', 이름 : 'Hibernate-Core', 버전 : '5.2.6.final'Compile Group : 'MySQL', 이름 : 'MySQL-Connector-Java', 버전 : '5.1.40'컴파일 그룹 : 'org.apache.commons', 이름 : 'Commons-dbcp2', '2.1', 'org.' 'sagneweaver', 버전 : agagejverison} task writenewpom {dolast {pom {} .writeto ( "$ projectDir/pom.xml")}}Web.xml을 구성하십시오
그런 다음 web-inf/web.xml 파일을 열고 다음 내용을 추가하십시오.
<? xml version = "1.0"alcoding = "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 "> <context-param> <param-value>/web-inf/applicationcontext.xml </param-value> </context-param> <servlet> <servlet-name> dispatcher </servlet-name> <servlet-class> org.springframework.web.servlet.dispatcherservlet </servlet class> </spring-class> 1 </spring-class> <Async-supported> true </async-supported> </servlet> <servlet-mapping> <servlet-name> dispatcher </servlet-name> <Url-pattern>/</url-pattern> </servlet-mapping> <layer> <larg-class> org.springframework.weerb.context.contexternextenlestrone </Listener> </web-app>
스프링을 구성하십시오
두 개의 스프링 구성 파일 /web-inf/applicationcontext.xml 및 /web-inf/dispatcher-servlet.xml이 있어야합니다. 전자는 데이터베이스와 같은 백엔드 및 글로벌 구성 요소를 구성하는 데 사용되는 루트 구성 파일이며 후자는 MVC 구성 파일이며 MVC 및 웹 관련 구성 요소를 구성하는 데 사용되는 MVC 구성 파일입니다.
그런 다음 /web-inf/applicationcontext.xml에서 최대 절전 모드 및 스프링을 통합하는 구성 요소를 구성합니다. 데이터 소스, HibernatessessionFactory, 최대 절전 모드 트랜잭션 관리자, 트랜잭션 연결 지점, 최대 절전 모드 템플릿 및 기타 Bean을 구성한 다음 데이터를 조작 할 때 최대 절전 모드 템플릿을 사용하여 스프링으로 제어되는 트랜잭션 관리 기능을 얻습니다.
<? xml 버전 = "1.0"alcoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans"xmlns : xsi = "http://ww.w.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.springframwork.org/schema/comext" 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/> <!-data id ="datasource ""Destroy "" "" "" "" "" "dataSource value = "com.mysql.jdbc.driver"/> <property name = "url"value = "jdbc : mysql : // localhost : 3306/test"/> <property name = "username"value = "root"/<property name = "password"vale = "12345678"/>> <! <bean id id id id id id vale = "vale"vale " 이름 = "dataSource"ref = "dataSource"/> <property name = "HibernateProperties"> <props> <prop key = "Hibernate.format_sql"> true </prop> <prop key = "hibernate.show_sql"> true </prop> <prop key = "hibernate.show_sql"> prop> <prop> key = "hibernate.hbm2ddl.auto"> create </prop> </props> </property> <property name = "packagestoscan"value = "yitian.learn.entity"/> </bean> <!-set hibernate 템플릿-> <bean id = "hibernateTemplate"> </bean> ref = "sessionFactOcfor" 최대 거래 관리자-> <bean id = "transactionManager"> <property name = "sessionFactory"ref = "sessionFactory"/> </bean> <!-설정 최대 거래 관리자-> <bean id = "TransactionManager"> <Property name = "sessionActory"ref = "sessionCactory"/> </bean> <!-데이터 액세스 대상-> <BEA ID = userDAO "///<! <TX : 조언 ID = "TXADVICE"TRANSACTE-MANAGER = "TRANSACTEMANAGER"> <TX : attributes> <TX : 메소드 이름 = "find*"read-forne = "true"/> <tx : method name = "*"/> </tx : attributes> </tx : 조언>-<aop : config> <aop> <aop>. id = "userDaoPointCut"expression = "execution (*yitian.learn.dao.*.*(..))"/> <aop : Advisor Advice-Ref = "txadvice"pointcut-Ref = "userDaoPointCut"/</aop : config> </bans>
그런 다음 Spring Web MVC의 구성 요소를 구성하십시오. Dispatcher-Servlet.xml에 다음 구성을 추가하십시오. 여기에 JSP 뷰 파서 및 유형 변환기를 추가합니다. 사용자 정의 유형 변환이 필요하지 않으면 해당 조각을 삭제할 수 있습니다.
<? xml 버전 = "1.0"alcoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans"xmlns : xsi = "http://ww.w.w3.org/2001/xmlschema-instance" xmlns : mvc = "http://www.springframework.org/schema/mvc"xmlns : context = "http://www.springframework.org/schema/context"xsi : skemalocation = "http://www.spremframframwork.org/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/sprring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> <mvc : view-resolvers> <mvc : jsp prefix ="/web-inf/jsp/". view-class = "org.springframework.web.servlet.view.jstlview"/> </mvc : view-resolvers> <mvc : mvc : mvc : mvc : mvc : mvc : "conversionervice"/> <context : component-scan base-package = "yitian.nearn id = "ConversionService"> <property name = "converter"> <set> <bean/> </set> </property> </bean> </beans>
이 시점에서 최대 절전 모드와 스프링 간의 통합이 완전히 구성되었습니다. 마침내, 나는 작은 예를 썼고 그것을 Github에 넣었습니다. 관심있는 학생들은 살펴볼 수 있습니다.
요약
위의 내용은이 기사에서 Spring Web MVC 및 최대 절전 모드의 통합 구성에 대한 자세한 설명입니다. 모든 사람에게 도움이되기를 바랍니다. 관심있는 친구는이 사이트의 다른 관련 주제를 계속 참조 할 수 있습니다. 단점이 있으면 메시지를 남겨 두십시오. 이 사이트를 지원해 주신 친구들에게 감사드립니다!