一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專(zhuān)注于服務(wù)器技術(shù)及軟件下載分享
分類(lèi)導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - 淺談SpringMVC+Spring3+Hibernate4開(kāi)發(fā)環(huán)境搭建

淺談SpringMVC+Spring3+Hibernate4開(kāi)發(fā)環(huán)境搭建

2020-07-21 12:26shan9liang Java教程

MVC已經(jīng)是現(xiàn)代Web開(kāi)發(fā)中的一個(gè)很重要的部分,本文介紹一下SpringMVC+Spring3+Hibernate4的開(kāi)發(fā)環(huán)境搭建,有興趣的可以了解一下。

早期的項(xiàng)目比較簡(jiǎn)單,多是用JSP 、Servlet + JDBC 直接搞定,后來(lái)使用 Struts1(Struts2)+Spring+Hibernate, 嚴(yán)格按照分層概念驅(qū)動(dòng)項(xiàng)目開(kāi)發(fā),這次又使用 Spring MVC取代Struts來(lái)進(jìn)行開(kāi)發(fā)。

MVC已經(jīng)是現(xiàn)代Web開(kāi)發(fā)中的一個(gè)很重要的部分,下面介紹一下SpringMVC+Spring3+Hibernate4的開(kāi)發(fā)環(huán)境搭建

先大致看一下項(xiàng)目結(jié)構(gòu):

淺談SpringMVC+Spring3+Hibernate4開(kāi)發(fā)環(huán)境搭建

具體的代碼不再演示,主要是走了一個(gè)很平常的路線,mvc-servcie-dao-hibernate的結(jié)構(gòu),源碼可以下載,主要看一下配置文件。解釋見(jiàn)注釋

web.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
 <display-name>SpringMVC</display-name>
 <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
  
 <!-- 配置Spring -->
 <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:config/spring-*.xml</param-value>
 </context-param>
  
  
 <listener>
   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
  
  
 <!-- 配置SpringMVC -->
 <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*:config/spring-servlet.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
 </servlet>
  
 <servlet-mapping>
  <servlet-name>springMVC</servlet-name>
  <url-pattern>/</url-pattern>
 </servlet-mapping>
  
 <!-- 設(shè)置字符集 -->
 <filter>
  <filter-name>encodingFilter</filter-name>
  <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  <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>encodingFilter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
  
  
 <!-- 控制Session的開(kāi)關(guān) -->
 <filter>
    <filter-name>openSession</filter-name>
    <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
 </filter>
  
 <filter-mapping>
  <filter-name>openSession</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
  
</web-app>

淺談SpringMVC+Spring3+Hibernate4開(kāi)發(fā)環(huán)境搭建

spring-servlet.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
  xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans 
   http://www.springframework.org/schema/beans/spring-beans-3.0.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-3.0.xsd">
 
  <!-- 注解掃描的包 -->
  <context:component-scan base-package="com.jialin" />
 
  <!-- 開(kāi)啟注解方案1 -->
  <!-- 注解方法處理 -->
  <!-- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"
    /> -->
  <!-- 注解類(lèi)映射處理 -->
  <!-- <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean> -->
 
  <!-- 開(kāi)啟注解方案2 -->
  <mvc:annotation-driven />
 
  <!-- 靜態(tài)資源訪問(wèn),方案1 -->
  <mvc:resources location="/img/" mapping="/img/**" />
  <mvc:resources location="/js/" mapping="/js/**" />
 
  <!-- 靜態(tài)資源訪問(wèn),方案2 -->
  <!-- <mvc:default-servlet-handler/> -->
 
  <!-- 視圖解釋類(lèi) -->
  <bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/"></property>
    <!--可為空,方便實(shí)現(xiàn)自已的依據(jù)擴(kuò)展名來(lái)選擇視圖解釋類(lèi)的邏輯 -->
    <property name="suffix" value=".jsp"></property>
  </bean>
 
  <!-- 上傳文件bean -->
  <!-- <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
    <property name="defaultEncoding" value="utf-8" /> <property name="maxUploadSize"
    value="10485760000" /> <property name="maxInMemorySize" value="40960" /> 
    </bean> -->
 
</beans> 

spring-hibernate.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
  xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans 
   http://www.springframework.org/schema/beans/spring-beans-3.0.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-3.0.xsd">
   
  <!-- 配置數(shù)據(jù)源 -->
  <bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://127.0.0.1/springmvc" />
    <property name="username" value="root" />
    <property name="password" value="123456" />
  </bean>
 
  <!-- 配置hibernate SessionFactory-->
  <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.hbm2ddl.auto">update</prop>
        <prop key="hibernate.show_sql">true</prop>
        <prop key="hiberante.format_sql">true</prop>
      </props>
    </property>
    <property name="configLocations">
      <list>
        <value>
          classpath*:config/hibernate/hibernate.cfg.xml
        </value>
      </list>
    </property>
  </bean>
 
  <!-- 事務(wù)管理器 -->
  <bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"></property>
  </bean>
   
  <!-- 事務(wù)代理類(lèi) -->
  <bean id="transactionBese"
    class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
    lazy-init="true" abstract="true">
    <property name="transactionManager" ref="transactionManager"></property>
    <property name="transactionAttributes">
      <props>
        <prop key="add*">PROPAGATION_REQUIRED,-Exception</prop>
        <prop key="update*">PROPAGATION_REQUIRED,-Exception</prop>
        <prop key="insert*">PROPAGATION_REQUIRED,-Exception</prop>
        <prop key="modify*">PROPAGATION_REQUIRED,-Exception</prop>
        <prop key="delete*">PROPAGATION_REQUIRED,-Exception</prop>
        <prop key="del*">PROPAGATION_REQUIRED,-Exception</prop>
        <prop key="get*">PROPAGATION_NEVER</prop>
      </props>
    </property>
  </bean>
 
</beans

spring-core.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:p="http://www.springframework.org/schema/p"
 xmlns:mvc="http://www.springframework.org/schema/mvc"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans 
   http://www.springframework.org/schema/beans/spring-beans-3.0.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-3.0.xsd">
    
  <!-- 引入其他配置文件,可以為多個(gè) -->
  <import resource="classpath*:config/spring/spring-user.xml"/>
   
 </beans

spring-user.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd" [
<!ENTITY contextInclude SYSTEM "org/springframework/web/context/WEB-INF/contextInclude.xml">
]>
 
<beans>
  <!-- Spring Bean -->
  <bean id="userDao" class="com.jialin.dao.UserDao">
    <property name="sessionFactory" ref="sessionFactory"></property>
  </bean>
   
  <bean id="userManagerBase" class="com.jialin.service.UserManager">
    <property name="userDao" ref="userDao"></property>
  </bean>
   
  <!-- parent為transactionBese,表示支持事務(wù) -->
  <bean id="userManager" parent="transactionBese">
    <property name="target" ref="userManagerBase"></property>
  </bean>
   
</beans>

hibernate.cfg.xml

?
1
2
3
4
5
6
7
8
9
10
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
 
<hibernate-configuration>
  <session-factory>
    <!-- 引入需要映射的類(lèi) -->
    <mapping class="com.jialin.entity.User"/>
  </session-factory>
</hibernate-configuration>

下面再來(lái)看看Controller

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package com.jialin.controller;
 
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
 
import com.jialin.entity.User;
import com.jialin.service.IUserManager;
 
@Controller  //類(lèi)似Struts的Action
@RequestMapping("/user"
public class UserController {
   
  @Resource(name="userManager") // 獲取spring配置文件中bean的id為userManager的,并注入
  private IUserManager userManager;
   
  @RequestMapping("/addUser"// 請(qǐng)求url地址映射,類(lèi)似Struts的action-mapping
  public String addUser(User user){
    if(userManager.addUser(user))
    {
      // 重定向
      return "redirect:/user/getAllUser";
    }else
    {
      return "/fail";
    }
     
  }
   
  @RequestMapping("/updateUser")
  public String updateUser(User user,HttpServletRequest request){
    if (userManager.updateUser(user))
    {
      user = userManager.getOneUser(user);
      request.setAttribute("user", user);
      return "/UserEdit";
    }else
    {
      return "/fail";
    }
     
  }
   
  @RequestMapping("/delUser")
  public void delUser(User user,HttpServletResponse response){
    String result = "{\"result\":\"error\"}";
     
    if(userManager.delUser(user)){
      result = "{\"result\":\"success\"}";
    }
    PrintWriter out = null;
    response.setContentType("application/json");
     
    try {
      out = response.getWriter();
      out.write(result);
    } catch (IOException e) {
      e.printStackTrace();
    }
 
  }
  @RequestMapping("/toAddUser")
  public String toAddUser(){
    return "/UserAdd";
  }
   
  @RequestMapping("/toUpdateUser")
  public String toUpdateUser(User user,HttpServletRequest request){
    User user1=userManager.getOneUser(user);
     
    request.setAttribute("user1", user1);
     
    return "/UserEdit";
  }
   
  @RequestMapping("/getAllUser")
  public String getAllUser(HttpServletRequest request){
     
    List userList=userManager.getAllUser();
     
    request.setAttribute("userlist", userList);
     
    return "/UserMain";
  }
   
}

源碼下載……

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:http://blog.csdn.net/shan9liang/article/details/9117233

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 色网在线观看 | 午夜神器老司机高清无码 | 夫妻性生活在线 | 欧美办公室silkstocking | 护士被多人调教到失禁h | 四虎影视黄色 | 日韩精品视频在线观看免费 | 丝袜足液精子免费视频 | 玩高中女同桌肉色短丝袜脚文 | 日韩欧美亚洲每日更新网 | 午夜伦理yy44008影院 | 波多野结衣xxxx性精品 | 日日射视频| 亚州在线视频 | yellow视频在线观看 | 2019午夜福合集高清完整版 | 91精品久久| 日日摸日日碰夜夜爽97纠 | 日日摸日日碰夜夜爽97纠 | 欧美视频网址 | 男女姓交大视频免费观看 | 激情涩涩| 日本在线一区 | 99视频福利 | 免费高清在线 | 国产一区二区免费福利片 | 美女班主任下面好爽好湿好紧 | 免费看60分钟大片视频播放 | 精品一区二区三区五区六区 | 波多野结衣一区免费作品 | 手机看片国产免费现在观看 | 9re视频这里只有精品 | 国产一区二区免费福利片 | 好深快点再快点好爽视频 | 91夜夜人人揉人人捏人人添 | 亚洲第一二三四区 | 国产美女亚洲精品久久久久久 | 日韩欧美视频二区 | 男人使劲躁女人视频免费 | 国产成+人+综合+欧美 亚洲 | 成人精品一级毛片 |