欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

spring mvc4.1.6 spring4.1.6 hibernate4.3.11 mysql5.5.25开发环境搭建图文教程

程序员文章站 2024-03-12 16:21:02
一、准备工作 开始之前,先参考上一篇:  思路都是一样的,只不过把struts2替换成了spring mvc  二、不同的地方 工程目录...

一、准备工作
开始之前,先参考上一篇: 

思路都是一样的,只不过把struts2替换成了spring mvc 

二、不同的地方
工程目录及jar包:

 action包改成controller;
 删除struts2 jar包,添加spring mvc包(已有的话,不需添加);

    spring mvc4.1.6 spring4.1.6 hibernate4.3.11 mysql5.5.25开发环境搭建图文教程

spring mvc4.1.6 spring4.1.6 hibernate4.3.11 mysql5.5.25开发环境搭建图文教程

web.xml配置: 

跟之前不同的地方是把struts2的过滤器替换成了一个servlet,主要目的是路由url,交给spring mvc处理; 

<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="webapp_id" version="3.0">
 <display-name>ssh</display-name>

 <context-param>
 <param-name>contextconfiglocation</param-name>
 <param-value>classpath:applicationcontext.xml</param-value>
 </context-param>
 <listener>
 <listener-class>org.springframework.web.context.contextloaderlistener</listener-class>
 </listener>

 <welcome-file-list>
 <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>


 <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:springmvc-servlet.xml</param-value>
 </init-param>
 <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
 <servlet-name>springmvc</servlet-name>
 <url-pattern>*.do</url-pattern>
 </servlet-mapping>


</web-app> 

applicationcontext.xml配置:
 不同的地方主要是配置自动扫描的时候,要排除@controller组件,这些bean是由spring mvc 去生成的;
 其它配置跟前一篇一样; 

<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:p="http://www.springframework.org/schema/p"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:jdbc="http://www.springframework.org/schema/jdbc"
 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-4.1.xsd
  http://www.springframework.org/schema/jdbc
  http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-4.1.xsd">
 
 <!-- scans the classpath for annotated components (including @repostory 
 and @service that will be auto-registered as spring beans -->  
 <context:component-scan base-package="ssh" >
 <!--排除@controller组件,该组件由springmvc配置文件扫描 -->
 <context:exclude-filter type="annotation" expression="org.springframework.stereotype.controller" />
 </context:component-scan>

 <!--配数据源 -->
 <bean name="datasource" class="com.mchange.v2.c3p0.combopooleddatasource" destroy-method="close">
 <property name="driverclass" value="com.mysql.jdbc.driver" />
 <property name="jdbcurl" value="jdbc:mysql://localhost:3306/demo" />
 <property name="user" value="root" />
 <property name="password" value="root" />
 
 <property name="acquireincrement" value="1"></property> 
 <property name="initialpoolsize" value="80"></property> 
 <property name="maxidletime" value="60"></property> 
 <property name="maxpoolsize" value="80"></property> 
 <property name="minpoolsize" value="50"></property> 
 <property name="acquireretrydelay" value="1000"></property> 
 <property name="acquireretryattempts" value="60"></property> 
 <property name="breakafteracquirefailure" value="false"></property>
 <!-- 如出现too many connections, 注意修改mysql的配置文件my.ini,增大最多连接数配置项,(查看当前连接命令:show processlist) -->
 </bean>

 
 <bean id="sessionfactory" class="org.springframework.orm.hibernate4.localsessionfactorybean">
 <property name="datasource" ref="datasource" />

 <property name="hibernateproperties">
  <props>
  <prop key="hibernate.dialect">org.hibernate.dialect.mysqldialect</prop>
  <prop key="hibernate.show_sql">true</prop>
  <prop key="hibernate.hbm2ddl.auto">update</prop>
  <prop key="connection.pool_size">10</prop>
  <prop key="current_session_context_class">thread</prop>
  <prop key="hibernate.cache.use_second_level_cache">true</prop>
  <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.ehcacheregionfactory</prop>
  <prop key="hibernate.cache.use_query_cache">true</prop>
  <prop key="hibernate.cache.provider_configuration_file_resource_path">ehcache.xml</prop>
  </props>
 </property>
 <property name="mappinglocations"> 
  <list> 
  <value>classpath:ssh/model/user.hbm.xml</value> 
  </list> 
 </property> 
 <!-- 
 <property name="annotatedclasses">
  <list>
  <value>ssh.model.user</value>
  </list>
 </property>
 -->
 </bean>
 
 <!-- 配置事务管理器 -->
 <bean id="transactionmanager"
 class="org.springframework.orm.hibernate4.hibernatetransactionmanager">
 <property name="sessionfactory" ref="sessionfactory" />
 </bean>
 
 <!-- 事务的传播特性 -->
 <tx:advice id="txadvice" transaction-manager="transactionmanager">
 <tx:attributes>
  <tx:method name="add*" propagation="required" read-only="false" rollback-for="java.lang.exception"/>
  <tx:method name="delete*" propagation="required" read-only="false" rollback-for="java.lang.exception"/>
  <tx:method name="update*" propagation="required" read-only="false" rollback-for="java.lang.exception"/>
  <tx:method name="save*" propagation="required" read-only="false" rollback-for="java.lang.exception"/>
 </tx:attributes>
 </tx:advice>
 

 <aop:config>
 <aop:pointcut id="pcmethod" expression="execution(* ssh.service..*.*(..))" />
 <aop:advisor pointcut-ref="pcmethod" advice-ref="txadvice" />
 </aop:config>
 
 
 

 <!-- 自定义aop处理 测试 -->
 <bean id="aoptest" class="ssh.aop.aoptest"></bean>
 <bean id="myaop" class="ssh.aop.myaop"></bean>
 <aop:config proxy-target-class="true">
 <aop:aspect ref="myaop">
  <aop:pointcut id="pcmethodtest" expression="execution(* ssh.aop.aoptest.test*(..))"/>
  <aop:before pointcut-ref="pcmethodtest" method="before"/>
  <aop:after pointcut-ref="pcmethodtest" method="after"/>
 </aop:aspect>
 </aop:config>
 


 </beans> 

springmvc-servlet.xml配置:
 配置自动扫描ssh.controller包下的@controller,这里要恢复applicationcontext.xml中配置的exclude-filter;
 配置视图解析器,有很多解析器,这里以internalresourceviewresolver为例; 

<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:mvc="http://www.springframework.org/schema/mvc"
 xmlns:p="http://www.springframework.org/schema/p"
 xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xsi:schemalocation="
 http://www.springframework.org/schema/beans 
 http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-4.1.xsd
 http://www.springframework.org/schema/mvc
 http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
 http://www.springframework.org/schema/tx
 http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
 ">
 
 <!-- 启动自动扫描该包下所有的bean(例如@controller) -->
 <context:component-scan base-package="ssh.controller" >
 <!-- 恢复父容器设置的 exclude-filter,注意包扫描路径ssh.controller-->
 <context:include-filter type="annotation" expression="org.springframework.stereotype.controller" /> 
 </context:component-scan>
 
 <!-- 定义视图解析器 -->
 <bean class="org.springframework.web.servlet.view.internalresourceviewresolver">
 <property name="prefix">
  <value>/web-inf/jsp/</value>
 </property>
 <property name="suffix">
  <value>.jsp</value>
 </property>
 </bean>
 
</beans> 

编写controller:
 由于使用spring mvc替换struts2,也就没有了action包了,删除,并新建controller包,在包下新建usercontroller类;
 @requestmapping:映射url;
 @responsebody:内容直接作为body返回;

 usercontroller.java 

package ssh.controller;

import java.io.printwriter;
import java.util.list;

import javax.annotation.resource;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;

import org.apache.log4j.logger;
import org.springframework.stereotype.controller;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.responsebody;

import ssh.aop.aoptest;
import ssh.model.user;
import ssh.service.userservice;

import com.google.gson.gson;

@controller
@requestmapping("/user")
public class usercontroller {
 logger logger = logger.getlogger(usercontroller.class);
 @resource
 private userservice userservice;
 @resource
 private aoptest aoptest;
 
 @requestmapping(value="/adduser")
 @responsebody
 public void adduser(httpservletrequest request, httpservletresponse response){
 printwriter out = null;
 try{
  response.setcontenttype("text/html;charset=utf-8");
  string account = request.getparameter("account");
  string name = request.getparameter("name");
  string address = request.getparameter("address"); 
  user user = new user();
  user.setaccount(account);
  user.setaddress(address);
  user.setname(name);
  userservice.add(user);
  out = response.getwriter();
  out.write(new gson().tojson("success"));
 }catch(exception e){
  e.printstacktrace();
  logger.error(e.getmessage());
  if(out != null)
  out.write(new gson().tojson("fail"));
 }finally{
  out.flush();
  out.close();
 }
 
 }
 
 @requestmapping(value="/queryuser")
 @responsebody
 public void queryalluser(httpservletrequest request, httpservletresponse response){
 printwriter out = null;
 
 aoptest.test1();
 aoptest.test2();
 
 try {

  response.setcontenttype("text/html;charset=utf-8");
 
  gson gson = new gson();
  list<user> userlist= userservice.queryalluser();
  string gsonstr = gson.tojson(userlist);
  
  out = response.getwriter();
  out.write(gsonstr);
 } catch (exception e) {
  e.printstacktrace();
  logger.error(e.getmessage());
  if(out != null)
  out.write(new gson().tojson("fail"));
 }finally{
  out.flush();
  out.close();
 }
 }
} 

三、运行程序
运行程序,添加用户、查询用户功能正常;
另外二级缓存也正常工作,第二次查询已经没有操作数据库了;

spring mvc4.1.6 spring4.1.6 hibernate4.3.11 mysql5.5.25开发环境搭建图文教程

@author   风一样的码农

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。