欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring+SpringMVC+Hibernate整合實(shí)例講解

 更新時(shí)間:2020年03月10日 14:46:32   作者:LeoRiver  
在本篇文章里小編給大家整理的是關(guān)于Spring+SpringMVC+Hibernate整合實(shí)例講解,需要的朋友們可以學(xué)習(xí)下。

使用Maven構(gòu)建項(xiàng)目,用pom.xml引入相應(yīng)jar,配置以下文件

創(chuàng)建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>

創(chuàng)建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>

創(chuàng)建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>
    <!-- 設(shè)置方言 -->
    <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
    <!-- 配置生成SQL的打印 -->
    <property name="hibernate.show_sql">true</property>
    <property name="hibernate.format_sql">true</property>
    <!-- 設(shè)置自動(dòng)生成表 -->
    <property name="hibernate.hbm2ddl.auto">update</property>
    <property name="hibernate.connection.isolation">4</property>
  </session-factory>
</hibernate-configuration>

創(chuàng)建db.properties

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/數(shù)據(jù)庫(kù)名
jdbc.username="" #這里自己填寫
jdbc.password="" #這里自己填寫

整合SSH的配置文件已經(jīng)完成

到此這篇關(guān)于Spring+SpringMVC+Hibernate整合實(shí)例講解的文章就介紹到這了,更多相關(guān)Spring+SpringMVC+Hibernate整合內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 深入解析Java中的Class Loader類加載器

    深入解析Java中的Class Loader類加載器

    這篇文章主要介紹了Java中的類加載器,是Java入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2016-03-03
  • 使用ftpClient下載ftp上所有文件解析

    使用ftpClient下載ftp上所有文件解析

    最近項(xiàng)目需要寫個(gè)小功能,需求就是實(shí)時(shí)下載ftp指定文件夾下的所有文件(包括子目錄)到本地文件夾中,保留文件到目錄路徑不變。今天小編給大家分享使用ftpClient下載ftp上所有文件的方法,需要的的朋友參考下吧
    2017-04-04
  • java實(shí)現(xiàn)獲取用戶的MAC地址

    java實(shí)現(xiàn)獲取用戶的MAC地址

    本文給大家匯總介紹了下使用java實(shí)現(xiàn)獲取客戶端用戶的MAC地址的方法,當(dāng)然最后一種更全面一些,有需要的小伙伴們可以根據(jù)需求自由選擇。
    2015-10-10
  • 解決使用gateway后靜態(tài)資源失效的問題

    解決使用gateway后靜態(tài)資源失效的問題

    這篇文章主要介紹了解決使用gateway后靜態(tài)資源失效的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • Java調(diào)用IK分詞器進(jìn)行分詞方式,封裝工具類

    Java調(diào)用IK分詞器進(jìn)行分詞方式,封裝工具類

    這篇文章主要介紹了Java調(diào)用IK分詞器進(jìn)行分詞方式,封裝工具類,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • 啟動(dòng)SpringBoot報(bào)JavaMail加載錯(cuò)誤的原因分析和解決

    啟動(dòng)SpringBoot報(bào)JavaMail加載錯(cuò)誤的原因分析和解決

    這篇文章給大家介紹了啟動(dòng)SpringBoot報(bào)JavaMail加載錯(cuò)誤的原因分析和解決,文中通過代碼示例給出了詳細(xì)的原因分析和解決方法,對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下
    2024-01-01
  • JavaEE線程安全實(shí)現(xiàn)線程池方法

    JavaEE線程安全實(shí)現(xiàn)線程池方法

    這篇文章主要介紹了JavaEE線程安全實(shí)現(xiàn)線程池方法,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-06-06
  • Spring純注解配置實(shí)現(xiàn)代碼示例解析

    Spring純注解配置實(shí)現(xiàn)代碼示例解析

    這篇文章主要介紹了Spring純注解配置實(shí)現(xiàn)代碼示例解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-08-08
  • java錯(cuò)誤:?不支持發(fā)行版本?22的簡(jiǎn)單解決方法

    java錯(cuò)誤:?不支持發(fā)行版本?22的簡(jiǎn)單解決方法

    這篇文章主要給大家介紹了關(guān)于java錯(cuò)誤:?不支持發(fā)行版本?22的簡(jiǎn)單解決方法,這個(gè)錯(cuò)誤通常是由于Java版本不兼容導(dǎo)致的,請(qǐng)檢查您的項(xiàng)目所使用的Java版本是否與您當(dāng)前安裝的Java版本一致,需要的朋友可以參考下
    2024-06-06
  • 詳解java整合solr5.0之solrj的使用

    詳解java整合solr5.0之solrj的使用

    本篇文章主要介紹了詳解java整合solr5.0之solrj的使用 ,具有一定的參考價(jià)值,有興趣的可以了解下
    2017-06-06

最新評(píng)論