[Java web]Spring+Struts2+Hibernate整合过程(2)

栏目: Struts · 发布时间: 7年前

内容简介:上篇文章介绍了一种整合方式,不妨就叫做有hibernate配置文件的方式,这里介绍一种不用hibernate.cfg.xml的一种配置方式,为了方便,就仍在上篇的demo中,继续修改了。因为hibernate.cfg.xml中配置的是数据库的相关配置,以及映射关系,关于这部分配置完全可以在applicationContext.xml中进行整合。数据库的4要素也可以单独提取到jdbc.properties文件中。在src目录下新建jdbc.properties文件,将4要素配置上

摘要

上篇文章介绍了一种整合方式,不妨就叫做有hibernate配置文件的方式,这里介绍一种不用hibernate.cfg.xml的一种配置方式,为了方便,就仍在上篇的demo中,继续修改了。

步骤

因为hibernate.cfg.xml中配置的是数据库的相关配置,以及映射关系,关于这部分配置完全可以在applicationContext.xml中进行整合。数据库的4要素也可以单独提取到jdbc.properties文件中。

在src目录下新建jdbc.properties文件,将4要素配置上

jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql:///sshdemo
jdbc.user=root
jdbc.password=abcd

在applicationContext.xml引入该文件即可

[Java web]Spring+Struts2+Hibernate整合过程(2)

修改后配置如下

<?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:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!--第一种方式 -->
    <!--加载sessionFactory配置-->
    <!--<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">-->
    <!--<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>-->
    <!--</bean>-->
    <!--第二种-->
    <!--配置连接池,以及引入jdbc.properties属性文件-->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <bean id="c3p0DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driverClass}"/>
        <property name="user" value="${jdbc.user}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}"/>
    </bean>
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="c3p0DataSource"/>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.format_sql">true</prop>
                <!--可以添加其他的配置,这里不再写了,可以参考hibernate.cfg.xml中的配置-->
            </props>
        </property>
        <!--映射关系-->
        <property name="mappingResources">
            <list>
                <value>com/ssh/domain/User.hbm.xml</value>
            </list>
        </property>
    </bean>
    <!-- 注入事务管理器,提交事务和回滚事务 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    <!--开启事务注解方式-->
    <tx:annotation-driven transaction-manager="transactionManager"/>
    <!--配置userDao-->
    <bean class="com.ssh.dao.UserDao" id="userDao">
        <!--注入sessionFactory-->
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <!--配置userService-->
    <bean id="userService" class="com.ssh.service.UserService">
        <!--注入userDao-->
        <property name="userDao" ref="userDao"></property>
    </bean>
    <!--将action的创建交给spring ioc 注意action是多例的 一定要将scope设置为prototype-->
    <bean class="com.ssh.action.UserAction" id="userAction" scope="prototype"/>

</beans>

这时完全可以把hibernate.cfg.xml删除

测试结果

[Java web]Spring+Struts2+Hibernate整合过程(2)


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

视觉SLAM十四讲

视觉SLAM十四讲

高翔、张涛、等 / 电子工业出版社 / 2017-3 / 75

《视觉SLAM十四讲:从理论到实践》系统介绍了视觉SLAM(同时定位与地图构建)所需的基本知识与核心算法,既包括数学理论基础,如三维空间的刚体运动、非线性优化,又包括计算机视觉的算法实现,例如多视图几何、回环检测等。此外,还提供了大量的实例代码供读者学习研究,从而更深入地掌握这些内容。 《视觉SLAM十四讲:从理论到实践》可以作为对SLAM 感兴趣的研究人员的入门自学材料,也可以作为SLAM......一起来看看 《视觉SLAM十四讲》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

在线进制转换器
在线进制转换器

各进制数互转换器

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具