spring整合springmvc和hibernate

上篇文章使用maven搭建了web环境,这篇来记录下如何使用spring整合springmvc和hibernate,亦即spring+springmvc+hibernate框架整合。第一步:首先配置

大家好,又见面了,我是全栈君。

上篇文章使用maven搭建了web环境,这篇来记录下如何使用spring整合springmvc和hibernate,亦即spring+springmvc+hibernate框架整合。

 

第一步:首先配置spring

  1. 配置spring配置文件applicationContext.xmls
    spring整合springmvc和hibernate
    spring整合springmvc和hibernate

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
     4     xmlns:context="http://www.springframework.org/schema/context"
     5     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
     6     xmlns:mvc="http://www.springframework.org/schema/mvc"
     7     xsi:schemaLocation="http://www.springframework.org/schema/beans 
     8        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     9        http://www.springframework.org/schema/context 
    10        http://www.springframework.org/schema/context/spring-context-3.0.xsd
    11        http://www.springframework.org/schema/tx 
    12        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    13        http://www.springframework.org/schema/aop
    14        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
    15     <!-- 自动扫描 -->
    16     <context:component-scan base-package="com.mvn" />
    17     <!-- 配置自动aop -->
    18     <aop:aspectj-autoproxy />
    19 
    20 </beans>

    View Code

  2. 配置web.xml文件
    spring整合springmvc和hibernate
    spring整合springmvc和hibernate

    <!-- 配置spring context需要读取的配置文件 -->
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext.xml</param-value>
        </context-param>
        <!-- 配置spring listener 以便在web容器启动的时候能自动初始化spring -->
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>    

    View Code

 

第二步:配置spring mvc

  1. 配置spring mvc配置文件spring-web.xmls
    spring整合springmvc和hibernate
    spring整合springmvc和hibernate

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
     4     xmlns:context="http://www.springframework.org/schema/context"
     5     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
     6     xmlns:mvc="http://www.springframework.org/schema/mvc"
     7     xsi:schemaLocation="http://www.springframework.org/schema/beans 
     8        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     9        http://www.springframework.org/schema/context 
    10        http://www.springframework.org/schema/context/spring-context-3.0.xsd
    11        http://www.springframework.org/schema/tx 
    12        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    13        http://www.springframework.org/schema/aop
    14        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    15        http://www.springframework.org/schema/mvc
    16        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    17     <!-- 自动扫描 com.mvn.controller的文件,初始化处理器-->
    18     <context:component-scan base-package="com.mvn.controller" />
    19     
    20     <!-- 配置试图解析器(渲染器) -->
    21     <bean id="viewResolver"
    22         class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    23         <property name="viewClass"
    24             value="org.springframework.web.servlet.view.JstlView" />
    25         <property name="prefix" value="WEB-INF/jsp/" />
    26         <property name="suffix" value=".jsp" />
    27     </bean>
    28 
    29 </beans>

    View Code

  2. 配置web.xml文件
    spring整合springmvc和hibernate
    spring整合springmvc和hibernate

     1 <!-- 配置spring mvc 分发器 -->
     2     <servlet>
     3         <servlet-name>springmvc</servlet-name>
     4         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
     5         <!-- 初始化spring mvc 配置文件的位置 -->
     6         <init-param>
     7             <param-name>contextConfigLocation</param-name>
     8             <param-value>classpath:spring-web.xml</param-value>
     9         </init-param>
    10         <!-- web容器启动的时候就加载springmvc -->
    11         <load-on-startup>1</load-on-startup>
    12     </servlet>
    13 
    14     <servlet-mapping>
    15         <servlet-name>springmvc</servlet-name>
    16         <url-pattern>*.do</url-pattern>
    17     </servlet-mapping>

    View Code

     

第三步:配置hibernate

  1. 将hibernate的相关配置放到spring配置文件applicationContext.xml里面
    spring整合springmvc和hibernate
    spring整合springmvc和hibernate

    <!-- 自动读取配置文件 -->
        <context:property-placeholder location="classpath:spring.properties" />
        <!-- 配置数据源 -->
        <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"
            destroy-method="close">
    
            <property name="driverClassName" value="${jdbc.driverClassName}" />
            <property name="url" value="${jdbc.url}" />
            <property name="username" value="${jdbc.username}" />
            <property name="password" value="${jdbc.password}" />
        </bean>
        <!-- 配置sessionFactory-->
        <bean id="sessionFactory"
            class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
            <property name="dataSource" ref="dataSource"></property>
            <!-- hibernate自动扫描 实体类-->
            <property name="packagesToScan">
                <list>
                    <value>com.mvn.entity</value>
                </list>
            </property>
            <!-- hibernate属性 -->
            <property name="hibernateProperties">
                <value>
                    hibernate.hbm2ddl.auto=${jdbc.hibernate.hbm2ddl.auto}
                    hibernate.dialect=${jdbc.hibernate.dialect}
                    hibernate.show_sql=${jdbc.hibernate.show_sql}
    
                </value>
            </property>
            
        </bean>
        <!-- 事务管理器 -->
        <bean id="transactionManager"
            class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory" />
        </bean>
        <tx:annotation-driven transaction-manager="transactionManager" />

    View Code

  2. 由于用到了需要读取propertie文件,因此要新增
    spring整合springmvc和hibernate
    spring整合springmvc和hibernate

    1 jdbc.driverClassName=com.mysql.jdbc.Driver
    2 jdbc.url=jdbc:mysql://localhost:3306/mvn
    3 jdbc.username=root
    4 jdbc.password=root
    5 
    6 jdbc.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
    7 jdbc.hibernate.show_sql=true
    8 jdbc.hibernate.hbm2ddl.auto=update

    View Code

     

第四步:编写对应的java和jsp代码

  1. 创建entity类
    spring整合springmvc和hibernate
    spring整合springmvc和hibernate

     1 package com.mvn.entity;
     2 
     3 import javax.persistence.Entity;
     4 import javax.persistence.GeneratedValue;
     5 import javax.persistence.Id;
     6 import javax.persistence.Table;
     7 
     8 @Entity
     9 @Table(name="t_user")
    10 public class User {
    11 
    12     private Integer id;
    13     private String name;
    14     private String password;
    15     
    16     @Id
    17     @GeneratedValue
    18     public Integer getId() {
    19         return id;
    20     }
    21     public void setId(Integer id) {
    22         this.id = id;
    23     }
    24     public String getName() {
    25         return name;
    26     }
    27     public void setName(String name) {
    28         this.name = name;
    29     }
    30     public String getPassword() {
    31         return password;
    32     }
    33     public void setPassword(String password) {
    34         this.password = password;
    35     }
    36     
    37 }

    View Code

  2. 创建dao类
    spring整合springmvc和hibernate
    spring整合springmvc和hibernate

     1 package com.mvn.dao;
     2 
     3 import javax.annotation.Resource;
     4 
     5 import org.hibernate.SessionFactory;
     6 import org.hibernate.classic.Session;
     7 import org.springframework.stereotype.Repository;
     8 
     9 import com.mvn.entity.User;
    10 
    11 @Repository
    12 public class UserDao {
    13 
    14     @Resource
    15     SessionFactory sessionFactory;
    16     
    17     public void save(User user){
    18         Session session = sessionFactory.openSession();
    19         session.save(user);
    20         
    21     }
    22 }

    View Code

  3. 创建service类
    spring整合springmvc和hibernate
    spring整合springmvc和hibernate

     1 package com.mvn.service;
     2 
     3 import javax.annotation.Resource;
     4 
     5 import org.springframework.stereotype.Service;
     6 import org.springframework.transaction.annotation.Transactional;
     7 
     8 import com.mvn.dao.UserDao;
     9 import com.mvn.entity.User;
    10 
    11 @Service
    12 public class UserService {
    13     @Resource
    14     UserDao userDao;
    15 
    16     @Transactional
    17     public void save(User user){
    18         userDao.save(user);
    19     }
    20     
    21 }

    View Code

  4. 创建控制器
    spring整合springmvc和hibernate
    spring整合springmvc和hibernate

     1 package com.mvn.controller;
     2 
     3 import javax.annotation.Resource;
     4 
     5 import org.springframework.stereotype.Controller;
     6 import org.springframework.web.bind.annotation.RequestMapping;
     7 
     8 import com.mvn.entity.User;
     9 import com.mvn.service.UserService;
    10 
    11 @Controller
    12 public class SaveUser {
    13 
    14     @Resource
    15     UserService userService;
    16     
    17     @RequestMapping(value="/saveUser")
    18     public String saveUser(User user){
    19         userService.save(user);
    20         return "saveSucess";
    21     }
    22 }

    View Code

  5. 创建jsp文件:WEB-INF\jsp\saveSucess.jsp
    spring整合springmvc和hibernate
    spring整合springmvc和hibernate

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     7 <title>Insert title here</title>
     8 </head>
     9 <body>
    10 Hello
    11 恭喜!
    12 整合成功
    13 </body>
    14 </html>

    View Code

  至此代码写完了,完整的目录结构为:

  spring整合springmvc和hibernate

第五步:进行测试

  1. 创建数据库spring整合springmvc和hibernate
  2. 启动tomcat,由于我们配置了hibernate属性hibernate.hbm2ddl.auto=update,因此会在tomcat启动完成后自动生成表t_user
  3. 通过页面访问

    spring整合springmvc和hibernate

   4.数据库生成数据

     spring整合springmvc和hibernate

至此,整合工作完成。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/120918.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • PHPStorm使用技巧及快捷键

    PHPStorm使用技巧及快捷键

    2021年10月23日
    111
  • L3-002 特殊堆栈(树状数组+二分)「建议收藏」

    L3-002 特殊堆栈(树状数组+二分)「建议收藏」原题链接堆栈是一种经典的后进先出的线性结构,相关的操作主要有“入栈”(在堆栈顶插入一个元素)和“出栈”(将栈顶元素返回并从堆栈中删除)。本题要求你实现另一个附加的操作:“取中值”——即返回所有堆栈中元素键值的中值。给定 N 个元素,如果 N 是偶数,则中值定义为第 N/2 小元;若是奇数,则为第 (N+1)/2 小元。输入格式:输入的第一行是正整数 N(≤10​5​​ )。随后 N 行,每行给出一句指令,为以下 3 种之一:Push keyPopPeekMedian其中 key 是不超过

    2022年8月9日
    6
  • linux下查看java进程日志,linux 下查看java进程[通俗易懂]

    linux下查看java进程日志,linux 下查看java进程[通俗易懂]Maven&lpar;二&rpar;使用eclipse创建maven多模块项目maven作为一种自动化构建工具,在现在的企业应用开发中运用非常普遍.企业项目一般都比较大,多采用maven管理的多模块项目,下面直接上创建步骤一.创建一个maven项目AndroidActivity的生命周期详解应用程序中,一个Activity通常就是一个单独的屏幕,它上面可以显示一些控件也可以监…

    2022年8月24日
    7
  • MQTT服务器搭建与试用,桌面工具连接MQTT服务器

    MQTT服务器搭建与试用,桌面工具连接MQTT服务器简介MQTT(MessageQueuingTelemetryTransport,消息队列遥测传输)是IBM开发的一个即时通讯协议,它比较适合于在低带宽、不可靠的网络的进行远程传感器和控制设备通讯等,正在日益成为物联网通信协议的重要组成部分。MQTT现在主要用于即时通讯,物联网M2M,物联网采集等。本文就社区上常见的开源MQTT服务器在常见操作系统上的搭建做详细介绍。目前一些开源MQTT服…

    2022年5月29日
    42
  • 浅析MOS管工作原理「建议收藏」

    浅析MOS管工作原理「建议收藏」MOS管分为N沟型MOS管和P沟型MOS管N沟型P沟型N沟型:漏极D接正极,源极S接负极,栅极G正电压时导电沟道建立,N沟道MOS管开始工作。P沟型:漏极D接负极,源极S接正极,栅极G负电压时

    2022年8月4日
    6
  • ES6基础:箭头函数

    ES6基础:箭头函数ES6基础系列(三)箭头函数简要分析一个为什么会有箭头函数,它的意义是什么?

    2022年6月15日
    37

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号