首页 > 代码库 > J2EE开发框架搭建(2) - springmvc4 + spring4 + hibernate4 整合
J2EE开发框架搭建(2) - 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=http://www.mamicode.com/"org.springframework.web.servlet.view.JstlView" />>最后建立一个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/test3. 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=http://www.mamicode.com/"${connection.url}" />>到此为止,springmvc4 + spring4 + hibernate4框架整合完成。
J2EE开发框架搭建(2) - springmvc4 + spring4 + hibernate4 整合
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。