最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

spring mvc4.1.6 spring4.1.6 hibernate4.3.11 mysql5.5.25開發(fā)環(huán)境搭建圖文教程

 更新時(shí)間:2016年06月21日 15:56:19   作者:風(fēng)一樣的碼農(nóng)  
這篇文章主要介紹了spring mvc4.1.6 + spring4.1.6 + hibernate4.3.11+mysql5.5.25開發(fā)環(huán)境搭建圖文教程,需要的朋友可以參考下

一、準(zhǔn)備工作
開始之前,先參考上一篇: 
struts2.3.24 + spring4.1.6 + hibernate4.3.11 + mysql5.5.25 開發(fā)環(huán)境搭建及相關(guān)說明
思路都是一樣的,只不過把struts2替換成了spring mvc 

二、不同的地方
工程目錄及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配置:
 不同的地方主要是配置自動掃描的時(shí)候,要排除@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>

 <!--配數(shù)據(jù)源 -->
 <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>
 <!-- 如出現(xiàn)Too many connections, 注意修改mysql的配置文件my.ini,增大最多連接數(shù)配置項(xiàng),(查看當(dāng)前連接命令: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>
 
 <!-- 配置事務(wù)管理器 -->
 <bean id="transactionManager"
 class="org.springframework.orm.hibernate4.HibernateTransactionManager">
 <property name="sessionFactory" ref="sessionFactory" />
 </bean>
 
 <!-- 事務(wù)的傳播特性 -->
 <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,這里要恢復(fù)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" >
 <!-- 恢復(fù)父容器設(shè)置的 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:內(nèi)容直接作為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();
 }
 }
} 

三、運(yùn)行程序
運(yùn)行程序,添加用戶、查詢用戶功能正常;
另外二級緩存也正常工作,第二次查詢已經(jīng)沒有操作數(shù)據(jù)庫了;

@author   風(fēng)一樣的碼農(nóng)

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • java打印出菱形圖案實(shí)例詳解

    java打印出菱形圖案實(shí)例詳解

    在本篇文章里小編給大家分享的是關(guān)于java打印出菱形圖案實(shí)例詳解,需要的朋友們可以學(xué)習(xí)下。
    2020-02-02
  • 淺談Maven安裝及環(huán)境配置出錯的解決辦法

    淺談Maven安裝及環(huán)境配置出錯的解決辦法

    這篇文章主要介紹了淺談Maven安裝及環(huán)境配置出錯的解決辦法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • Java實(shí)現(xiàn)設(shè)計(jì)模式之責(zé)任鏈模式

    Java實(shí)現(xiàn)設(shè)計(jì)模式之責(zé)任鏈模式

    責(zé)任鏈模式是一種行為設(shè)計(jì)模式,允許你將請求沿著處理鏈發(fā)送,然后處理者都可對其進(jìn)行處理,完成后可以再將其傳遞給下一個處理者。下面將會舉例說明什么是責(zé)任鏈模式,責(zé)任鏈模式該如何使用
    2022-08-08
  • Java替換jar包中class文件的方法(親測有用)

    Java替換jar包中class文件的方法(親測有用)

    這篇文章主要介紹了如何在不重新打包整個jar包的情況下,僅替換其中的某個Java類文件,文中通過代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2025-02-02
  • Mybatis代碼生成器Mybatis Generator(MBG)實(shí)戰(zhàn)詳解

    Mybatis代碼生成器Mybatis Generator(MBG)實(shí)戰(zhàn)詳解

    本文我們主要實(shí)戰(zhàn)Mybatis官方的代碼生成器:Mybatis Generator(MBG),掌握它以后,可以簡化大部分手寫代碼,我們只需要寫復(fù)雜邏輯代碼,需要的朋友可以參考下
    2023-05-05
  • 使用MyBatis攔截器實(shí)現(xiàn)sql查詢權(quán)限動態(tài)修改代碼實(shí)例

    使用MyBatis攔截器實(shí)現(xiàn)sql查詢權(quán)限動態(tài)修改代碼實(shí)例

    這篇文章主要介紹了使用MyBatis攔截器實(shí)現(xiàn)sql查詢權(quán)限動態(tài)修改代碼實(shí)例,為了不耦合,現(xiàn)在的方案是在需要鑒權(quán)的Mybatis?Mapper方法上增加一個注解,在運(yùn)行過程中判斷該注解存在即對sql進(jìn)行修改,需要的朋友可以參考下
    2023-08-08
  • 詳解springmvc 中controller與jsp傳值

    詳解springmvc 中controller與jsp傳值

    本篇文章主要介紹了springmvc 中controller與jsp傳值,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-07-07
  • Java9新特性中的模塊化詳解

    Java9新特性中的模塊化詳解

    今天介紹一個Java?9的功能,模塊化(Modular),這可能使Java有史以來最大的Feature,對Java9模塊化相關(guān)知識感興趣的朋友一起看看吧
    2022-03-03
  • Java 深入淺出分析Synchronized原理與Callable接口

    Java 深入淺出分析Synchronized原理與Callable接口

    Synchronized關(guān)鍵字解決的是多個線程之間訪問資源的同步性,synchronized關(guān)鍵字可以保證被它修飾的方法或者代碼塊在任意時(shí)刻只能有一個線程執(zhí)行,Runnable是執(zhí)行工作的獨(dú)立任務(wù),但是不返回任何值。如果我們希望任務(wù)完成之后有返回值,可以實(shí)現(xiàn)Callable接口
    2022-03-03
  • java實(shí)現(xiàn)簡單掃雷小游戲

    java實(shí)現(xiàn)簡單掃雷小游戲

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)簡單掃雷小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-07-07

最新評論

龙州县| 仪陇县| 永吉县| 湾仔区| 原平市| 金秀| 米脂县| 台中县| 雷州市| 黎川县| 蒲城县| 湟中县| 四平市| 荆州市| 德化县| 宝山区| 桂东县| 邵武市| 翁源县| 烟台市| 堆龙德庆县| 阜南县| 思南县| 池州市| 桐乡市| 巴中市| 浏阳市| 南川市| 五莲县| 潞城市| 恩平市| 荔浦县| 阜城县| 化州市| 东明县| 岳阳县| 南宁市| 城口县| 炉霍县| 万全县| 临城县|