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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • vim 配置文件 .vimrc

    vim 配置文件 .vimrc在终端下使用vim进行编辑时,默认情况下,编辑的界面上是没有显示行号、语法高亮度显示、智能缩进等功能的。为了更好的在vim下进行工作,需要手动设置一个配置文件:.vimrc。在启动vim时,当前用户根目录下的.vimrc文件会被自动读取,该文件可以包含一些设置甚至脚本,所以,一般情况下把.vimrc文件创建在当前用户的根目录下比较方便,即创建的命令为:$vi~/.vi

    2022年5月7日
    45
  • (20211206更新)ubuntu18.04 安装Python3.8.3、jupyter notebook远程连接配置、虚拟环境搭建。及torch、tensorflow成功下载[通俗易懂]

    (20211206更新)ubuntu18.04 安装Python3.8.3、jupyter notebook远程连接配置、虚拟环境搭建。及torch、tensorflow成功下载[通俗易懂]最近搞了一个低配云服务器,就瞎整。多次踩坑后,进行记录问题之前有试过删除软链接、进行替换#删除软连接sudorm-rf/usr/bin/python3sudorm-rf/usr/bin/pip3#新建软连接sudoln-s/usr/local/python3/bin/python3.8/usr/bin/python3sudoln-s/usr/local/python3/bin/pip3.8/usr/bin/pip3但是吧,后续的pipinstall会出

    2022年6月23日
    38
  • matlab中fmincon函数的使用

    matlab中fmincon函数的使用fmincon是用于求解非线性多元函数最小值的matlab函数。其语法格式比较多,参数也是各有各的意思,我就举几个例子,大家可从代码中去体会其使用方法。示例一%%只是大概写个板式,这里的fun2和fun3要自己去定义,这里就不写出来了,前面%有介绍过clcclearA=[321;123;200;030;002]b=[120;80;96;102;40]lb=zero

    2022年4月28日
    129
  • jquery ajax实例代码_jquery ajax详解

    jquery ajax实例代码_jquery ajax详解Jquery在异步提交方面封装的很好,直接用AJAX非常麻烦,Jquery大大简化了我们的操作,不用考虑浏览器的诧异了。推荐一篇不错的jQueryAjax实例文章,忘记了可以去看看,地址为:http://www.cnblogs.com/yeer/archive/2009/07/23/1529460.html和http://www.w3school.com.cn/jquery/

    2022年8月16日
    6
  • linux如何安装node_node 环境变量

    linux如何安装node_node 环境变量前言:linux中安装node环境步骤:第一步:官网下载node安装包,点我进入第二步:解压到个人的根目录下,也就是/home/haoxing(这是你自己的名字)/nodejs把文件夹名字改成nodejs方便使用第三步:配置环境变量1,打开终端入口2,输入命令:注意,带sudo是可编辑,不带的是只读sudovim/etc/profile3,输入你的密码4,shift+i打开编辑模式,加上以下代码,注意/home/…

    2025年11月5日
    3
  • vue入门教程(一)「建议收藏」

    vue入门教程(一)「建议收藏」1.vue简介1.1vue是什么官网:https://cn.vuejs.org/Vue是一套用于构建用户界面的渐进式框架。与其它大型框架不同的是,Vue被设计为可以自底向上逐层应用。Vue的核心库只关注视图层,不仅易于上手,还便于与第三方库或既有项目整合。另一方面,当与现代化的工具链以及各种支持类库结合使用时,Vue也完全能够为复杂的单页应用提供驱动。1.2vue的特点1)遵循MVVM模式2)编码简洁,体积小,运行效率高,适合移动/PC端开发.

    2022年6月4日
    33

发表回复

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

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