IDEA整合Spring4.3.18+Hibernate4.2.2+MySql

本文详细介绍了如何在项目中整合Spring和Hibernate框架,包括数据库配置、Spring与Hibernate的配置、JUnit4单元测试等内容。通过阅读本文,你可以了解如何设置数据库连接、配置Spring的ApplicationContext、实现Hibernate的SessionFactory以及进行事务管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

新建包含Spring和Hibernate的工程

在这里插入图片描述
注意版本选择

配置数据库

数据库连接测试配置,参见我之前写的博客 传送门
数据库属性配置文件
db.properties

jdbc.user=root
jdbc.password=123456
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/spring4
jdbc.initPoolSize=5
jdbc.maxPoolSize=10

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:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
    <!--配置自动扫描的包-->
    <context:component-scan base-package="hibernate">

    </context:component-scan>
    <!--    配置数据源-->
    <!--    导入资源文件-->
    <context:property-placeholder location="db.properties"/>
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.password}"></property>
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
        <property name="driverClass" value="${jdbc.driverClass}"></property>
        <property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
        <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
    </bean>
    <!--    配置Hiberate的SessionFactory  实例:通过Spring提供的LocalSessionFactory-->
    <bean id="sessionFactory"
          class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <!-- 配置数据源属性       -->
        <property name="dataSource" ref="dataSource"/>
        <!--  配置Hiberate配置文件的位置名称      -->
        <!--        <property name="configLocation" value="hibernate.cfg.xml"/>-->
        <!-- 使用 hibernateProperties 属相来配置 Hibernate 原生的属性 -->
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
        <!--  配置Hiberate映射文件的位置和名称,可以使用通配符-->
        <property name="mappingLocations" value="hibernate/entities/*.hbm.xml"/>
    </bean>

<!--        配置Spring的声明事务-->
<!--     1、配置事务管理器-->
        <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory"/>
        </bean>
<!--         2、配置事务属性,需要事务管理器-->
        <tx:advice id="txAdvice" transaction-manager="transactionManager">
            <tx:attributes>
                <tx:method name="get*" read-only="true"/>
<!--事务传播行为-->
                <tx:method name="purchase" propagation="REQUIRES_NEW"/>
                <tx:method name="*"></tx:method>
            </tx:attributes>
        </tx:advice>
<!--         3、配置事务切点,并把切点和事务属性关联起来-->
        <aop:config>
            <aop:pointcut expression="execution(* hibernate.services.*.*(..))" id="txPointcut"/>       <!--关联起来-->
            <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
        </aop:config>
</beans>

JUnit4单元测试

工程目录下,右键新建Test目录,如下
在这里插入图片描述
右键Test目录,-> Mark Directory as -> Test Resources Root
在这里插入图片描述
然后就可以新建JunitTest类进行测试,测试代码如下:

import hibernate.services.BookShopServices;
import hibernate.services.Cashier;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.sql.DataSource;

import java.sql.SQLException;
import java.util.Arrays;

import static myprint.Print.println;

public class JunitTest {
    private ApplicationContext ctx = null;
    private BookShopServices bookShopServices = null;
    private Cashier cashier = null;
    {
        ctx = new ClassPathXmlApplicationContext("ApplicationContext.xml");
        bookShopServices = ctx.getBean(BookShopServices.class);
        cashier = ctx.getBean(Cashier.class);
    }

    @Test
    public void testCashier(){
        cashier.checkout("aa", Arrays.asList("1001","1002"));
    }

    @Test
    public void testBookShopServices(){
        bookShopServices.purchase("aa","1001");
    }


    @Test
    public void testDataSource() {
        DataSource dataSource = ctx.getBean(DataSource.class);
        try {
            println(dataSource.getConnection());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

项目工程文件

传送门

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值