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

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

内容简介:上篇文章介绍了一种整合方式,不妨就叫做有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)


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

查看所有标签

猜你喜欢:

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

Text Processing in Python

Text Processing in Python

David Mertz / Addison-Wesley Professional / 2003-6-12 / USD 54.99

Text Processing in Python describes techniques for manipulation of text using the Python programming language. At the broadest level, text processing is simply taking textual information and doing som......一起来看看 《Text Processing in Python》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码