spring整合springmvc和hibernate

时间:2023-03-08 16:46:25
spring整合springmvc和hibernate

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

第一步:首先配置spring

  1. 配置spring配置文件applicationContext.xmls
     <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    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-3.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
    <!-- 自动扫描 -->
    <context:component-scan base-package="com.mvn" />
    <!-- 配置自动aop -->
    <aop:aspectj-autoproxy /> </beans>
  2. 配置web.xml文件
    <!-- 配置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>

第二步:配置spring mvc

  1. 配置spring mvc配置文件spring-web.xmls
     <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    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-3.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    <!-- 自动扫描 com.mvn.controller的文件,初始化处理器-->
    <context:component-scan base-package="com.mvn.controller" /> <!-- 配置试图解析器(渲染器) -->
    <bean id="viewResolver"
    class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass"
    value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
    </bean> </beans>
  2. 配置web.xml文件
     <!-- 配置spring mvc 分发器 -->
    <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- 初始化spring mvc 配置文件的位置 -->
    <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring-web.xml</param-value>
    </init-param>
    <!-- web容器启动的时候就加载springmvc -->
    <load-on-startup>1</load-on-startup>
    </servlet> <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.do</url-pattern>
    </servlet-mapping>

第三步:配置hibernate

  1. 将hibernate的相关配置放到spring配置文件applicationContext.xml里面
    <!-- 自动读取配置文件 -->
    <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" />
  2. 由于用到了需要读取propertie文件,因此要新增
     jdbc.driverClassName=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost:3306/mvn
    jdbc.username=root
    jdbc.password=root jdbc.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
    jdbc.hibernate.show_sql=true
    jdbc.hibernate.hbm2ddl.auto=update

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

  1. 创建entity类
     package com.mvn.entity;
    
     import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.Table; @Entity
    @Table(name="t_user")
    public class User { private Integer id;
    private String name;
    private String password; @Id
    @GeneratedValue
    public Integer getId() {
    return id;
    }
    public void setId(Integer id) {
    this.id = id;
    }
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    public String getPassword() {
    return password;
    }
    public void setPassword(String password) {
    this.password = password;
    } }
  2. 创建dao类
     package com.mvn.dao;
    
     import javax.annotation.Resource;
    
     import org.hibernate.SessionFactory;
    import org.hibernate.classic.Session;
    import org.springframework.stereotype.Repository; import com.mvn.entity.User; @Repository
    public class UserDao { @Resource
    SessionFactory sessionFactory; public void save(User user){
    Session session = sessionFactory.openSession();
    session.save(user); }
    }
  3. 创建service类
     package com.mvn.service;
    
     import javax.annotation.Resource;
    
     import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional; import com.mvn.dao.UserDao;
    import com.mvn.entity.User; @Service
    public class UserService {
    @Resource
    UserDao userDao; @Transactional
    public void save(User user){
    userDao.save(user);
    } }
  4. 创建控制器
     package com.mvn.controller;
    
     import javax.annotation.Resource;
    
     import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping; import com.mvn.entity.User;
    import com.mvn.service.UserService; @Controller
    public class SaveUser { @Resource
    UserService userService; @RequestMapping(value="/saveUser")
    public String saveUser(User user){
    userService.save(user);
    return "saveSucess";
    }
    }
  5. 创建jsp文件:WEB-INF\jsp\saveSucess.jsp
     <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    Hello
    恭喜!
    整合成功
    </body>
    </html>

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

  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

至此,整合工作完成。