SSH整合(3)之整合Hibernate

SSH整合(3)之整合Hibernate

微信搜索 zze_coding 或扫描 👉 二维码关注我的微信公众号获取更多资源推送:

方式一:保留 Hibernate 配置文件

1、编写实体映射文件:

<!-- com/zze/domain/User.hbm.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="com.zze.domain.User" table="user">
        <id name="id" column="id">
            <generator class="native"/>
        </id>
        <property name="name" column="name" length="32"/>
        <property name="age" column="age"/>
    </class>
</hibernate-mapping>

2、在 Hibernate 核心配置文件中引入实体映射文件:

<mapping resource="com/zze/domain/User.hbm.xml"/>

3、修改 Dao 代码,使用 Hibernate 提供的 Dao 模板类:

// com.zze.dao.impl.UserDaoImpl
package com.zze.dao.impl;

import com.zze.dao.UserDao;
import com.zze.domain.User;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
// 使用 Hibernate 提供的 Dao 模板类
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {

    @Override
    public void save(User user) {
        this.getHibernateTemplate().save(user);
    }
}

4、将 Hibernate 的核心工厂类实例交给 Spring 创建:

<!-- 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: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">
    <!--Spring 整合 Hibernate-->
    <!--引入 Hibernate 配置信息-->
    <bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="configLocation" value="classpath:hibernate.cfg.xml"/>
    </bean>
    <!--
    <bean name="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    -->

    <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <!-- 开启注解事务 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>

    <!--Spring 整合 Struts2-->
    <!--
    让 Spring 来创建 Action 实例
        注意,Action 类要配置为多例
    -->
    <bean name="userAction" class="com.zze.web.action.UserAction" scope="prototype">
        <property name="userService" ref="userService"/>
    </bean>

    <bean name="userService" class="com.zze.service.impl.UserServiceImpl">
        <property name="userDao" ref="userDao"/>
    </bean>

    <bean name="userDao" class="com.zze.dao.impl.UserDaoImpl">
        <property name="sessionFactory" ref="sessionFactory"/>
        <!--
        <property name="hibernateTemplate" ref="hibernateTemplate"/>
        -->
    </bean>
</beans>

5、测试:

<!-- com.zze.web.action.UserAction -->
package com.zze.web.action;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.zze.domain.User;
import com.zze.service.UserService;

public class UserAction extends ActionSupport implements ModelDriven<User> {
    private User user = new User();

    @Override
    public User getModel() {
        return user;
    }

    private UserService userService;

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public String save() {
        userService.save(user);
        return NONE;
    }
}

image.png

image.png

方式二:去除 Hibernate 配置文件

1、新建数据库连接信息属性文件:

# jdbc.properties
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///test
jdbc.username=root
jdbc.password=root

2、删除 Hibernate 配置文件 hibernate.cfg.xml,在 Spring 配置文件中完成 Hibernate 相关配置:

<!-- 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: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">
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!--配置 C3P0 数据源-->
    <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driverClass}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    <!--Spring 整合 Hibernate-->
    <!--引入 Hibernate 配置信息-->
    <bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <!-- 注入连接池 -->
        <property name="dataSource" ref="dataSource"/>
        <!-- 配置Hibernate的相关属性 -->
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>

        <!-- 设置映射文件 -->
        <property name="mappingResources">
            <list>
                <value>com/zze/domain/User.hbm.xml</value>
            </list>
        </property>
    </bean>
    <!--
    <bean name="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    -->

    <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <!-- 开启注解事务 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>

    <!--Spring 整合 Struts2-->
    <!--
    让 Spring 来创建 Action 实例
        注意,Action 类要配置为多例
    -->
    <bean name="userAction" class="com.zze.web.action.UserAction" scope="prototype">
        <property name="userService" ref="userService"/>
    </bean>

    <bean name="userService" class="com.zze.service.impl.UserServiceImpl">
        <property name="userDao" ref="userDao"/>
    </bean>

    <bean name="userDao" class="com.zze.dao.impl.UserDaoImpl">
        <property name="sessionFactory" ref="sessionFactory"/>
        <!--
        <property name="hibernateTemplate" ref="hibernateTemplate"/>
        -->
    </bean>
</beans>

3、测试:

// com.zze.web.action.UserAction
package com.zze.web.action;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.zze.domain.User;
import com.zze.service.UserService;

public class UserAction extends ActionSupport implements ModelDriven<User> {
    private User user = new User();

    @Override
    public User getModel() {
        return user;
    }

    private UserService userService;

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public String save() {
        userService.save(user);
        return NONE;
    }
}

image.png

image.png

Copyright: 采用 知识共享署名4.0 国际许可协议进行许可

Links: https://www.zze.xyz/archives/ssh3.html

Buy me a cup of coffee ☕.