程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 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開辟情況搭建圖文教程

編輯:關於JAVA

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開辟情況搭建圖文教程)文章只能為提供參考,不一定能成為您想要的結果。以下是spring mvc4.1.6 spring4.1.6 hibernate4.3.11 mysql5.5.25開辟情況搭建圖文教程正文


1、預備任務
開端之前,先參考上一篇: 
struts2.3.24 + spring4.1.6 + hibernate4.3.11 + mysql5.5.25 開辟情況搭建及相干解釋
思緒都是一樣的,只不外把struts2調換成了spring mvc 

2、分歧的處所
工程目次及jar包:

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

   

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.谷歌.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();
 }
 }
} 

3、運轉法式
運轉法式,添加用戶、查詢用戶功效正常;
別的二級緩存也正常任務,第二次查詢曾經沒有操作數據庫了;

@author   風一樣的碼農

以上就是本文的全體內容,願望對年夜家的進修有所贊助,也願望年夜家多多支撐。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved