Java EE Development Framework (2)-springmvc4 + Spring4 + hibernate4 Integration

Source: Internet
Author: User

1. Open the Pom.xml file under the Hqhop-framework-parent project, add Springmvc4, Spring4, hibernate4, and data source Druid dependency package, plug-in, dependency package version number

<!--data source related jar? --><dependency><groupid>com.alibaba</groupid><artifactid>druid</artifactid> <version>${druid.version}</version></dependency><!--web-related jars? --><dependency><groupid>javax.servlet</groupid><artifactid>javax.servlet-api</ artifactid><version>${servlet.version}</version><scope>provided</scope></ dependency><!--ASPECTJ related jar package--><dependency><groupid>org.aspectj</groupid>< Artifactid>aspectjrt</artifactid><version>${aspectj.version}</version></dependency> <dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId> <version>${aspectj.version}</version></dependency><!--Spring related jars? --><dependency><groupid>org.springframework</groupid><artifactid>spring-orm</ Artifactid><version>${spring.version}</version><exclusions><exclusion><artifactid>commons-logging</artifactid>< groupid>commons-logging</groupid></exclusion></exclusions></dependency>< dependency><groupid>org.springframework.data</groupid><artifactid>spring-data-commons< /artifactid><version>${spring.data.commons.version}</version></dependency><dependency ><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId>< Version>${spring.version}</version></dependency><dependency><groupid> org.springframework</groupid><artifactid>spring-context-support</artifactid><version>$ {spring.version}</version></dependency><!--Hibernate4--><dependency><groupid> org.hibernate</groupid><artifactid>hibernate-core</artifactid><version>${ Hibernate.version}</version></dependency>

Add the Maven-compiler-plugin plugin in the Pom.xml file:

<build><finalname>${project.artifactid}</finalname><plugins><plugin><groupid >org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId>< version>3.0</version><configuration><source>${jdk.version}</source><target>${ Jdk.version}</target><encoding>${project.build.sourceencoding}</encoding></configuration ></plugin></plugins></build>

In the Pom.xml file, add the version information:

<properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>< jdk.version>1.7</jdk.version><support.basedir>${project.parent.basedir}/src/support</ Support.basedir><site.basedir>${project.parent.basedir}</site.basedir><aspectj.version> 1.7.4</aspectj.version>
2. Add SPRINGMVC to the Hqhop-framework-web project

Adding SPRINGMVC interceptors to the Web. xml file, as well as character encoding interceptors

<!--set the servlet encoding to start--><filter><filter-name>set Character encoding</filter-name>< filter-class>org.springframework.web.filter.characterencodingfilter</filter-class><async-supported >true</async-supported><init-param><param-name>encoding</param-name><param-value >utf-8</param-value></init-param><init-param><param-name>forceencoding</ Param-name><param-value>true</param-value></init-param></filter><filter-mapping ><filter-name>set Character encoding</filter-name><url-pattern>/*</url-pattern></ filter-mapping><!--Set the servlet encoding end--><servlet><servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param> <param-name>contextconfiglocation</param-name><param-value>classpath:spring-mvc.xml</ Param-value></init-param><load-on-startup>1</load-on-startup><async-supported>true</async-supported> </servlet><servlet-mapping><servlet-name>springmvc</servlet-name><url-pattern>/ </url-pattern></servlet-mapping>
Create a Spring-mvc.xml file under the Src/main/resources folder and add the following to the Spring-mvc.xml file:
<beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "xmlns:p=" http://www.springframework.org/schema/p "xmlns:context="/HTTP/ Www.springframework.org/schema/context "xmlns:mvc=" Http://www.springframework.org/schema/mvc "xsi:schemalocation       = "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd       Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd "><!-- Turn on controller annotation support, scan controller--><context:component-scan base-package=<span style= "color: #ff0000;" > "Com.hqhop.framework.*.controller" </span> use-default-filters= "false" ><context:include-filter type= "Annotation" expression= "Org.springframework.stereotype.Controller"/><context:include-filter type= " Annotation "expression=" Org.springframework.web.bind.annotation.ControllerAdvice "/></context:component-scan> <!--will automatically register--><mvc:annotation-driven enable-matrix-variables= "true"/><mvc:view-controller path= "/" View-name= "Redirect:/index"/><!--when using <url-pattern>/</url-pattern> in Web. XML dispatcherservlet mapping, you can map static resources--><mvc:default-servlet-handler/><!--static resource mappings--><mvc:resources mapping= "/static/**" location= "/web-inf/static/"/><!--========================= View definition =========================--><bean class= "Org.springframework.web.servlet.view.InternalResourceViewResolver" ><property name= "Viewclass" value = "Org.springframework.web.servlet.view.JstlView"/><property name= "ContentType" value= "text/html"/>< Property name= "prefix" value= "/web-inf/jsp/"/><property name= "suffix" value= ". JSP"/></bean></ Beans>

Finally build a controller to test whether the SPRINGMVC is successful:

Package Com.hqhop.framework.web.controller;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.stereotype.controller;import Org.springframework.ui.model;import Org.springframework.web.bind.annotation.requestmapping;import Com.hqhop.framework.common.bind.annotation.currentuser;import Com.hqhop.framework.shiro.entity.user;import Com.hqhop.framework.shiro.service.UserService; @Controller <span style= "color: #ff0000;" > @RequestMapping ("/index") </span>public class Indexcontroller {<span style= "color: #ff0000;" > @RequestMapping ("/test") </span>public String Test () {User user = new User (); User.setname ("silentwu333"); User.setpassword ("123456"); User.setage (24); User U = userservice.save (user); return "Welcome";    Return view name   web-inf/jsp/webcome.jsp}}
Access Address: Http://localhost:8080/hqhop-framework-web/index/test

3. Add spring to the Hqhop-framework-web project

Add the following in Web. xml:

<!--spring configuration file Start--><context-param><param-name>contextconfiglocation</param-name>< param-value>            classpath:spring-config.xml        </param-value></context-param><listener> <listener-class>org.springframework.web.context.contextloaderlistener</listener-class></ listener><!--Spring configuration file Ends-
Create a resources.properties below the Src/main/resources folder and add the following:

#druid DataSource   Druid Data Source configuration information # Reference https://github.com/alibaba/druid/wiki/%E9%85%8D%E7%BD%AE_DruidDataSource% e5%8f%82%e8%80%83%e9%85%8d%e7%bd%aedruid.initialsize=10druid.minidle=10druid.maxactive=50druid.maxwait= 60000druid.timebetweenevictionrunsmillis=60000druid.minevictableidletimemillis=300000druid.validationquery= SELECT ' x ' druid.testwhileidle=truedruid.testonborrow=falsedruid.testonreturn=falsedruid.poolpreparedstatements= truedruid.maxpoolpreparedstatementperconnectionsize=20druid.filters=wall,stat#druid.connectionproperties= config.decrypt=trueconnection.url=jdbc:mysql://localhost:3306/hqhop_framework?autoreconnect=true& Useunicode=true&characterencoding=utf-8connection.username=rootconnection.password=root#hibernate    Hibernate configuration Information hibernate.show_sql=truehibernate.format_sql=falsehibernate.hbm2ddl.auto=updatehibernate.dialect= Org.hibernate.dialect.MySQLDialect
Create a spring-config.xml under the Src/main/resources folder

<?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:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:tx= " Http://www.springframework.org/schema/tx "xmlns:context=" Http://www.springframework.org/schema/context "xmlns: Jpa= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/DATA/JPA" xsi:schemalocation= "http://www.springframework.org/schema/ Beans Http://www.springframework.org/schema/beans/spring-beans.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http ://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx HTTP://WWW.SPRINGF Ramework.org/schema/tx/spring-tx.xsd Http://www.springframework.org/schema/context http://www.springframework.or G/schema/context/spring-context.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/DATA/JPA http://www.springframework.or G/schema/data/jpa/spring-jpa.xsd "><context:property-plaCeholder location<span style= "color: #ff0000;" >= "Classpath:resources.properties" </span>/><!--turn on AOP listening is only valid for the current profile--><aop:aspectj-autoproxy Expose-proxy= "true"/><!--scan annotation bean--><context:component-scan base-package= "<span style=" color:# ff0000; " >com.hqhop.framework.*,org.spring</span> "><context:exclude-filter type=" annotation "expression=" Org.springframework.stereotype.Controller "/></context:component-scan><!--data source--><bean id=" DataSource "class=" Com.alibaba.druid.pool.DruidDataSource "init-method=" Init "destroy-method=" Close "><!-- Basic property URL, user, password--><property name= "url" value= "${connection.url}"/><property name= "username" Value= "${connection.username}"/><property name= "password" value= "${connection.password}"/><!-- Configuration initialization size, MIN, max--><property name= "initialsize" value= "${druid.initialsize}"/><property name= "MinIdle" Value= "${druid.minidle}"/><property name="Maxactive" value= "${druid.maxactive}"/><!--Configure the time to get the connection wait timeout--><property name= "maxwait" value= "${ druid.maxwait} "/><!--configuration interval to detect the idle connection that needs to be closed, in milliseconds--><property name=" Timebetweenevictionrunsmillis "value=" ${druid.timebetweenevictionrunsmillis} "/><!--Configure the minimum lifetime of a connection in a pool in milliseconds --><property name= "Minevictableidletimemillis" value= "${druid.minevictableidletimemillis}"/>< Property Name= "Validationquery" value= "${druid.validationquery}"/><property name= "Testwhileidle" value= "${ Druid.testwhileidle} "/><property name=" Testonborrow "value=" ${druid.testonborrow} "/><property name=" Testonreturn "value=" ${druid.testonreturn} "/><!--open Pscache, and specify the size of Pscache on each connection if you use Oracle, Configure Poolpreparedstatements to True,mysql can be configured to False. --><property name= "poolpreparedstatements" value= "${druid.poolpreparedstatements}"/><property name= " Maxpoolpreparedstatementperconnectionsize "Value=" ${druid.maxpoolpreparedstatementperconnectionsize} "/><!--Configure monitoring statistics interception filters--><property name=" Filters "value=" ${druid.filters} "/><!--<property Name= "ConnectionProperties" value= "${druid.connectionproperties}"/>--></bean><bean id= " Sessionfactory "class=" Org.springframework.orm.hibernate4.LocalSessionFactoryBean "><property name=" DataSource "ref=" DataSource "/><property name=" Packagestoscan "><list><value> Com.hqhop.framework.*.entity</value></list></property><property name= "HibernateProperties" ><props><prop key= "Hibernate.dialect" >${hibernate.dialect}</prop><prop key= " Hibernate.show_sql ">${hibernate.show_sql}</prop><prop key=" Hibernate.format_sql ">${ Hibernate.format_sql}</prop><prop key= "Hibernate.hbm2ddl.auto" >${hibernate.hbm2ddl.auto}</prop ></props></property></bean><!--Open annotation transactions are only valid for the current profile--><tx:annotation-driven Transaction-manager= "TransactionManager" Proxy-target-class= "true"/><!--Configure Hibernate transaction manager--><bean id= "TransactionManager" class= " Org.springframework.orm.hibernate4.HibernateTransactionManager "><property name=" sessionfactory "ref=" Sessionfactory "/></bean></beans>


So far, Springmvc4 + SPRING4 + hibernate4 Framework integration is complete.















Java EE Development Framework (2)-springmvc4 + Spring4 + hibernate4 Integration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.