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. Added Springmvc4, Spring4, hibernate4, and data source Druid dependency packages, plugins, dependent package versions

<!--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 to 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 number 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

Add the Springmvc interceptor to the Web. xml file. and 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 below the Src/main/resources directory. Add for example the following in 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 voluntarily register--><mvc:annotation-driven enable-matrix-variables= "true"/><mvc:view-controller path= " /"View-name=" Redirect:/index "/><!--when used in Web. XML Dispatcherservlet <url-pattern>/</url-pattern> 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 set up a controller, test SPRINGMVC whether to build success:

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}}
Interview Address: Http://localhost:8080/hqhop-framework-web/index/test

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

Add, for example, the following in Web. xml:

<!--spring Config file starts--><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 directory to add the following:

#druid DataSource   Druid Data Source configuration information # References 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 below the Src/main/resources directory

<?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.SP Ringframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework. Org/schema/tx/spring-tx.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/ Context/spring-context.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/DATA/JPA http://www.springframework.org/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--&GT;&LT;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}"/><!--Configuration Gets the time that the connection waits to timeout--><property name= "maxwait" value= "${druid.maxwait}"/><!--configuration interval How often to conduct a test. The spare connection that needs to be closed is measured in milliseconds--><property name= "Timebetweenevictionrunsmillis" value= "${ Druid.timebetweenevictionrunsmillis} "/><!--Configure the minimum time for a connection to survive 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 the pscache on each connection assuming 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><!-- Opening the annotation transaction is only valid for the current configuration file--><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, the Springmvc4 + Spring4 + HIBERNATE4 Framework has been integrated.















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.