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? autoReconnect=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

在src/main/resources目录以下建立一个spring-config.xml

<?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>

到此为止,springmvc4 + spring4 + hibernate4框架整合完毕。

J2EE开发框架搭建(2) - springmvc4 + spring4 + hibernate4 整合的更多相关文章

  1. springMVC4+spring4+hibernate4框架搭建

    最近项目不是很忙,整理了一个springMVC框架给大家分享下,框架结构:springMVC4+spring4+hibernate4,主要是spring配置.springmvc配置.hibernate ...

  2. Struts2+Spring4+Hibernate4整合超详细教程

    Struts2.Spring4.Hibernate4整合 超详细教程 Struts2.Spring4.Hibernate4整合实例-下载 项目目的: 整合使用最新版本的三大框架(即Struts2.Sp ...

  3. J2EE开发框架搭建(1) - maven搭建多项目

    怎样使用maven搭建多个项目 1. 创建一个maven project 2. 在frame-parent项目上面点击右键,新建Maven Module 3. 完毕之后再建立一个web项目 4. 依照 ...

  4. spring4+hibernate4 整合

    1.web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version=" ...

  5. Maven+struts2+spring4+hibernate4的环境搭建

    搭建Maven+struts2+spring4+hibernate4其实并不难!但开始弄的时候还是费了我好大的力气,老是出现这样那样的错误!好了,废话不多说,开始搭建开发环境. 一.Myeclipse ...

  6. Struts2+Hibernate4+Spring4框架整合搭建Java项目原型

    收藏 http://www.cnblogs.com/mageguoshi/p/5850956.html Struts2+Hibernate4+Spring4框架整合搭建Java项目原型

  7. 基于Spring4+SpringMVC4+Mybatis3+Hibernate4+Junit4框架构建高性能企业级的部标GPS监控平台

    开发企业级的部标GPS监控平台,投入的开发力量很大,开发周期也很长,选择主流的开发语言以及成熟的开源技术框架来构建基础平台,是最恰当不过的事情,在设计之初就避免掉了技术选型的风险,避免以后在开发过程中 ...

  8. 基于Spring4+SpringMVC4+Mybatis3+Hibernate4+Junit4框架构建高性能企业级的部标1077视频监控平台

    开发企业级的部标GPS监控平台,投入的开发力量很大,开发周期也很长,选择主流的开发语言以及成熟的开源技术框架来构建基础平台,是最恰当不过的事情,在设计之初就避免掉了技术选型的风险,避免以后在开发过程中 ...

  9. spring4+hibernate4+maven环境搭建

    本文主要介绍利用maven搭建spring4+hibernate4开发环境. 首先我们创建一个maven项目,具体步骤就不详细介绍了,看看我们pom.xml文件 <project xmlns=& ...

随机推荐

  1. oracle dg坏境主库redolog改动大小

    --备库standby 主库四个redolog 曾经都是50M大小 SQL> alter database recover managed standby database cancel;   ...

  2. tomcat启动后ids页面无法访问

    修改servers-->tomcat6.0-->server.xml <Context docBase="/tds7030-web" path="&qu ...

  3. servlet 将输入内容通过拼接页面的方式显示出来

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  4. django学习之Model(二)

    继续(一)的内容: 1-跨文件的Models 在文件头部import进来,然后用ForeignKey关联上: from django.db import models from geography.m ...

  5. VMware 11安装Mac OS X 10.10 及安装Mac Vmware Tools(超详细),以及动态调整虚拟机硬盘大小

    先上一张效果图兴奋一下,博主穷屌丝一个,只能通过虚拟黑苹果体验下高富帅的生活,感觉超爽的,废话不多说的,直接上图了! 目录: 1.安装所需软件下载: 2.Mac OS X10.10 安装基本步骤: 3 ...

  6. 基于visual Studio2013解决C语言竞赛题之0806平均分

     题目

  7. php 使用curl发起https请求

    今天一个同事反映,使用curl发起https请求的时候报错:“SSL certificate problem, verify that the CA cert is OK. Details: erro ...

  8. c++11 新特性之 autokeyword

    C++11是对眼下C++语言的扩展和修正.C++11包含大量的新特性:包含lambda表达式,类型推导keywordauto.decltype,和模板的大量改进. g++编译c++11命令加上 -st ...

  9. codeforces #267 C George and Job(DP)

    职务地址:http://codeforces.com/contest/467/problem/C 太弱了..这题当时都没做出来..思路是有的,可是自己出的几组数组总是过不去..今天又又一次写了一遍.才 ...

  10. Android bitmap序列化

    最近在开发中需要用到图片bitmap的序列化并进行传递,发现bitmap是没有序列化的,下面是自己实现的一个序列化方法,分享下. 以下是通过byte[]来进行序列化的,因为bitmap是没有序列化的, ...