J2EE开发框架搭建 - springmvc4 + spring4 + hibernate4 整合
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了J2EE开发框架搭建 - springmvc4 + spring4 + hibernate4 整合相关的知识,希望对你有一定的参考价值。
1. 打开hqhop-framework-parent项目下的pom.xml文件。加入springmvc4 , spring4 , hibernate4 ,以及数据源druid的依赖包,插件,依赖包版本
<!-- data source 相关jar?--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>${druid.version}</version> </dependency> <!-- web相关jar? --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>${servlet.version}</version> <scope>provided</scope> </dependency> <!-- aspectj相关jar包 --> <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相关jar? --> <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>
在pom.xml文件里加入maven-compiler-plugin插件:
<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>
在pom.xml文件里在加入上版本号信息:
<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> <hibernate.version>4.3.5.Final</hibernate.version> <junit.version>4.11</junit.version> <servlet.version>3.0.1</servlet.version> <spring.version>4.0.0.RELEASE</spring.version> <spring.data.commons.version>1.6.1.RELEASE</spring.data.commons.version> <druid.version>0.2.23</druid.version> </properties>2. 在hqhop-framework-web项目中增加springmvc
在web.xml文件里加入springmvc 的拦截器。以及字符编码拦截器
<!-- 设置servlet编码開始 --> <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> <!-- 设置servlet编码结束 --> <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>在src/main/resources目录以下建立一个spring-mvc.xml文件。在spring-mvc.xml文件里加入例如以下内容:
<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"> <!-- 开启controller注解支持,扫描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> <!-- 会自己主动注冊了 --> <mvc:annotation-driven enable-matrix-variables="true"/> <mvc:view-controller path="/" view-name="redirect:/index" /> <!-- 当在web.xml 中 DispatcherServlet使用 <url-pattern>/</url-pattern> 映射时,能映射静态资源 --> <mvc:default-servlet-handler /> <!-- 静态资源映射 --> <mvc:resources mapping="/static/**" location="/WEB-INF/static/" /> <!-- ========================= VIEW定义 ========================= --> <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>
最后建立一个controller,測试springmvc是否搭建成功:
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"; //返回视图名称 web-inf/jsp/webcome.jsp } }訪问地址: http://localhost:8080/hqhop-framework-web/index/test
3. hqhop-framework-web项目中增加spring
在web.xml中加入例如以下内容:
<!-- Spring配置文件開始 --> <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配置文件结束 -->在src/main/resources目录以下建立一个resources.properties,增加以下内容:
#druid datasource druid数据源配置信息 #參考 https://github.com/alibaba/druid/wiki/%E9%85%8D%E7%BD%AE_DruidDataSource%E5%8F%82%E8%80%83%E9%85%8D%E7%BD%AE druid.initialSize=10 druid.minIdle=10 druid.maxActive=50 druid.maxWait=60000 druid.timeBetweenEvictionRunsMillis=60000 druid.minEvictableIdleTimeMillis=300000 druid.validationQuery=SELECT 'x' druid.testWhileIdle=true druid.testOnBorrow=false druid.testOnReturn=false druid.poolPreparedStatements=true druid.maxPoolPreparedStatementPerConnectionSize=20 druid.filters=wall,stat #druid.connectionProperties=config.decrypt=true connection.url=jdbc:mysql://localhost:3306/hqhop_framework?在src/main/resources目录以下建立一个spring-config.xmlautoReconnect=true&useUnicode=true&characterEncoding=utf-8 connection.username=root connection.password=root #hibernate hibernate配置信息 hibernate.show_sql=true hibernate.format_sql=false hibernate.hbm2ddl.auto=update hibernate.dialect=org.hibernate.dialect.MySQLDialect
<?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.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> /> <!-- 开启AOP监听 仅仅对当前配置文件有效 --> <aop:aspectj-autoproxy expose-proxy="true" /> <!-- 扫描注解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> <!-- 数据源 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <!-- 基本属性 url、user、password --> <property name="url" value="${connection.url}" /> <property name="username" value="${connection.username}" /> <property name="password" value="${connection.password}" /> <!-- 配置初始化大小、最小、最大 --> <property name="initialSize" value="${druid.initialSize}" /> <property name="minIdle" value="${druid.minIdle}" /> <property name="maxActive" value="${druid.maxActive}" /> <!-- 配置获取连接等待超时的时间 --> <property name="maxWait" value="${druid.maxWait}" /> <!-- 配置间隔多久才进行一次检測。检測须要关闭的空暇连接,单位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="${druid.timeBetweenEvictionRunsMillis}" /> <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> <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}" /> <!-- 打开PSCache,而且指定每一个连接上PSCache的大小 假设用Oracle,则把poolPreparedStatements配置为true,mysql能够配置为false。
--> <property name="poolPreparedStatements" value="${druid.poolPreparedStatements}" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="${druid.maxPoolPreparedStatementPerConnectionSize}" /> <!-- 配置监控统计拦截的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> <!-- 开启注解事务 仅仅对当前配置文件有效 --> <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" /> <!-- 配置Hibernate事务管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> </beans>
以上是关于J2EE开发框架搭建 - springmvc4 + spring4 + hibernate4 整合的主要内容,如果未能解决你的问题,请参考以下文章
基于SpringMVC4.3.2+Spring4.3.2+MyBatis3.4.1搭建SSM框架
搭建springmvc4 spring4 hibernate4整合框架tomcat用啥版本
SpringMVC4+Spring4+Hibernate4框架整合
springmvc4 mybatis 整合 框架源码 bootstrap SSM