Spring+SpringMVC+Hibernate整合实例讲解

 更新时间:2020年03月10日 14:46:32   作者:LeoRiver  
在本篇文章里小编给大家整理的是关于Spring+SpringMVC+Hibernate整合实例讲解,需要的朋友们可以学习下。

使用Maven构建项目,用pom.xml引入相应jar,配置以下文件

创建spring.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="http://www.springframework.org/schema/beans"
  xmlns:aop="http://www.springframework.org/schema/aop"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
  <!-- 整合Hibernate -->
  <context:property-placeholder location="classpath:db.properties"/>
  
  <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="${jdbc.driver}"/>
    <property name="jdbcUrl" value="${jdbc.url}"/>
    <property name="user" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
  </bean>
  
  <bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="configLocation" value="classpath:hibernate.cfg.xml"/>
    <property name="mappingLocations" value="classpath:mapping/*.hbm.xml"></property>
  </bean>
  
  <bean name="hibernateTransactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"></property>
  </bean>
  
  <tx:advice id="txAdvice" transaction-manager="hibernateTransactionManager"></tx:advice>  
  
  <aop:config>
    <aop:pointcut expression="execution(* com.forum.service.*.*(..))" id="pointCut"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="pointCut"/>
  </aop:config>
  
  <context:component-scan base-package="com.forum">
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
  </context:component-scan>
</beans>

创建springmvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="http://www.springframework.org/schema/beans"
  xmlns:aop="http://www.springframework.org/schema/aop"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:mvc="http://www.springframework.org/schema/mvc"
  xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
  <!-- 配置MessageConvertor -->
  <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    <property name="messageConverters">
      <list>
        <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
          <property name="supportedMediaTypes">
            <list>
              <value>application/json;charset=utf-8</value>
            </list>
          </property>
        </bean>
      </list>
    </property>
  </bean>
  
  <!-- 配置模板引擎 -->
  <bean name="templateResolver" class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
    <property name="prefix" value="/WEB-INF/views/"/>
    <property name="suffix" value=".html"/>
    <property name="templateMode" value="HTML"/>
    <property name="cacheable" value="false"/>
    <property name="characterEncoding" value="UTF-8"/>
  </bean>
  
  <bean name="templateEngine" class="org.thymeleaf.spring5.SpringTemplateEngine">
    <property name="templateResolver" ref="templateResolver"/>
  </bean>
  
  <bean class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
    <property name="templateEngine" ref="templateEngine"/>
    <property name="characterEncoding" value="utf-8"/>
  </bean>
  
  <mvc:annotation-driven></mvc:annotation-driven>
  
  <mvc:default-servlet-handler/>
  
  <context:component-scan base-package="com.forum">
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
  </context:component-scan>
</beans>

创建hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!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>
    <!-- 设置方言 -->
    <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
    <!-- 配置生成SQL的打印 -->
    <property name="hibernate.show_sql">true</property>
    <property name="hibernate.format_sql">true</property>
    <!-- 设置自动生成表 -->
    <property name="hibernate.hbm2ddl.auto">update</property>
    <property name="hibernate.connection.isolation">4</property>
  </session-factory>
</hibernate-configuration>

创建db.properties

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/数据库名
jdbc.username="" #这里自己填写
jdbc.password="" #这里自己填写

整合SSH的配置文件已经完成

到此这篇关于Spring+SpringMVC+Hibernate整合实例讲解的文章就介绍到这了,更多相关Spring+SpringMVC+Hibernate整合内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 浅谈一下maven优缺点及使用和特点

    浅谈一下maven优缺点及使用和特点

    这篇文章主要介绍了浅谈一下maven优缺点及使用和特点,一个项目管理工具软件,那么maven项目有什么优缺点呢,让我们一起来看看吧
    2023-03-03
  • 如何用nacos搭建微服务注册配置中心

    如何用nacos搭建微服务注册配置中心

    这篇文章主要介绍了如何用nacos搭建微服务注册配置中心问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-05-05
  • 深入理解Java虚拟机 JVM 内存结构

    深入理解Java虚拟机 JVM 内存结构

    本节将会介绍一下JVM的内存结构,JVM运行时数据区的各个组成部分:堆,方法区,程序计数器,Java虚拟机栈,本地方法栈,还会对Java堆的分代划分做个简单的介绍
    2021-09-09
  • 我赌你不清楚Spring中关于Null的这些事

    我赌你不清楚Spring中关于Null的这些事

    这篇文章主要介绍了我赌你不清楚Spring中关于Null的这些事,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-06-06
  • MyBatis开发Dao层的两种方式实现(原始Dao层开发)

    MyBatis开发Dao层的两种方式实现(原始Dao层开发)

    这篇文章主要介绍了MyBatis开发Dao层的两种方式实现(原始Dao层开发),并对数据库进行增删查改,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-12-12
  • Java由浅入深通关抽象类与接口上

    Java由浅入深通关抽象类与接口上

    在类中没有包含足够的信息来描绘一个具体的对象,这样的类称为抽象类,接口是Java中最重要的概念之一,它可以被理解为一种特殊的类,不同的是接口的成员没有执行体,是由全局常量和公共的抽象方法所组成,本文给大家介绍Java抽象类和接口,感兴趣的朋友一起看看吧
    2022-04-04
  • 详解Java 类的加载机制

    详解Java 类的加载机制

    这篇文章主要介绍了Java 类的加载机制,帮助大家更好的理解和学习Java,感兴趣的朋友可以了解下
    2020-08-08
  • Java多线程Thread类的使用及注意事项

    Java多线程Thread类的使用及注意事项

    这篇文章主要介绍了Java多线程Thread类的使用及注意事项,在java标准库中提供了一个Thread类来表示/操作线程,Thread类也可以视为是java标准库提供的API
    2022-06-06
  • SpringBoot @ModelAttribute使用场景分析

    SpringBoot @ModelAttribute使用场景分析

    这篇文章主要介绍了SpringBoot @ModelAttribute使用场景分析,文中通过实例代码图文相结合给大家介绍的非常详细,需要的朋友可以参考下
    2021-08-08
  • 使用Okhttp实现上传文件+参数请求接口form-data

    使用Okhttp实现上传文件+参数请求接口form-data

    在进行接口对接时,常遇到需要传递多种类型参数及文件上传的情况,解决此问题的关键在于参数传递和文件上传的正确处理,在Service层和Controller层的传参,可以通过@RequestParam标注或直接使用请求实体类,但若结合文件上传,则不应使用@RequestBody注解
    2024-10-10

最新评论