• SSH框架整合的其它方式


    --------------------siwuxie095

       

       

       

       

       

       

       

       

    SSH 框架整合的其它方式

       

       

    1、主要是整合 Spring 框架和 Hibernate 框架时,可以不写

    Hibernate 核心配置文件hibernate.cfg.xml

       

       

       

    2、把 Hibernate 核心配置文件中的配置全都转移 Spring

    核心配置文件中

       

       

       

    3、具体实现

       

    applicationContext.xml:

       

    <?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: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/aop

    http://www.springframework.org/schema/aop/spring-aop.xsd

    http://www.springframework.org/schema/context

    http://www.springframework.org/schema/context/spring-context.xsd

    http://www.springframework.org/schema/tx

    http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!-- (1) -->

    <!-- 配置 C3P0 连接池 -->

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">

    <property name="driverClass" value="com.mysql.jdbc.Driver"/>

    <!--

    jdbc:mysql:///test_db jdbc:mysql://localhost:3306/test_db 的简写

    -->

    <property name="jdbcUrl" value="jdbc:mysql:///test_db"/>

    <property name="user" value="root"/>

    <property name="password" value="8888"/>

    </bean>

    <!-- SessionFactory 对象的创建交给 Spring 进行管理 -->

    <bean id="sessionFactory"

    class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">

    <!--

    数据库配置原本是在 Hibernate 核心配置文件中配置的,

    现在 Hibernate 核心配置文件不存在了,所以在这里注

    dataSource

    LocalSessionFactoryBean 中有相关属性,所以可以

    注入

    -->

    <property name="dataSource" ref="dataSource"></property>

    <!-- 配置 Hibernate 基本信息 -->

    <property name="hibernateProperties">

    <props>

    <prop key="hibernate.show_sql">true</prop>

    <prop key="hibernate.format_sql">true</prop>

    <prop key="hibernate.hbm2ddl.auto">update</prop>

    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>

    <!--

    原来的配置:

    <prop key="hibernate.current_session_context_class">thread</prop>

    SSH 框架整合中会报错,要么将这个配置删了,要么改成如下配置

    参考链接:http://blog.csdn.net/maoyuanming0806/article/details/61417995

    -->

    <prop key="hibernate.current_session_context_class">

    org.springframework.orm.hibernate5.SpringSessionContext

    </prop>

    </props>

    </property>

    <!-- 引入映射配置文件 -->

    <property name="mappingResources">

    <list>

    <value>com/siwuxie095/entity/User.hbm.xml</value>

    <!-- <value>....</value> -->

    </list>

    </property>

    </bean>

    <!-- (2) -->

    <!-- 配置 Action 对象 -->

    <bean id="userAction" class="com.siwuxie095.action.UserAction" scope="prototype">

    <property name="userService" ref="userService"></property>

    </bean>

    <!-- 配置 Service 对象 -->

    <bean id="userService" class="com.siwuxie095.service.UserService">

    <property name="userDao" ref="userDaoImpl"></property>

    </bean>

    <!-- 配置 Dao 实现类对象 -->

    <bean id="userDaoImpl" class="com.siwuxie095.dao.impl.UserDaoImpl">

    <property name="hibernateTemplate" ref="hibernateTemplate"></property>

    </bean>

    <!-- 配置 HibernateTemplate 对象 -->

    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">

    <!-- 注入 SessionFactory 对象 -->

    <property name="sessionFactory" ref="sessionFactory"></property>

    </bean>

    <!-- (3) -->

    <!-- 配置事务管理器 HibernateTransactionManager -->

    <bean id="transactionManager"

    class="org.springframework.orm.hibernate5.HibernateTransactionManager">

    <!--注入 SessionFactory 对象 -->

    <property name="sessionFactory" ref="sessionFactory"></property>

    </bean>

    <!-- 开启事务注解 -->

    <tx:annotation-driven transaction-manager="transactionManager"/>

    </beans>

       

       

       

       

       

       

       

       

       

       

    【made by siwuxie095】

  • 相关阅读:
    Leetcode: Insert Delete GetRandom O(1)
    Leetcode: Kth Smallest Element in a Sorted Matrix
    Leetcode: Combination Sum IV && Summary: The Key to Solve DP
    Leetcode: Wiggle Subsequence
    Leetcode: Guess Number Higher or Lower II
    Leetcode: Guess Number Higher or Lower
    Leetcode: Find K Pairs with Smallest Sums
    Leetcode: Super Pow
    Leetcode: Largest Divisible Subset
    Leetcode: Water and Jug Problem && Summary: GCD求法(辗转相除法 or Euclidean algorithm)
  • 原文地址:https://www.cnblogs.com/siwuxie095/p/7439579.html
Copyright © 2020-2023  润新知