spring 整合hibernate项目配置文件

1、首先在资源包下创建db.properties文件


添加内容如下:记得修改成你自己的数据库的用户名、密码和你的数据库名字

jdbc.user=username
jdbc.password=password
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/xxx


jdbc.initPoolSize=5
jdbc.maxPoolSize=10


2、配置hibernate.cf.xml文件,把原来文件中的<session-factory>中的配置的数据源删除,这里在applicationContext中配置的数据源,在下面的文件中只配置下图说到的属性


所有代码如下

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>


<session-factory>
<!-- <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.url">jdbc:mysql://localhost:3306/xiaoneiwang</property>
<property name="connection.username">root</property>
<property name="connection.password">888</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="myeclipse.connection.profile">mysql</property>
-->
<!-- 配置hibernate的基本属性 -->
<!-- 1、数据源配置到IOC容器中,所以在此不用配置数据源 -->
<!-- 2、关联.hbm.xml 也在IOC容器配置sessionFactory实例是进行配置  -->
<!-- 3、配置hibernate的基本属性,方言 ,SQL显示及格式,生成表的策略以及二级缓存 -->
<property name="hibernate.dialect">
org.hibernate.dialect.MySQL5InnoDBDialect
</property>


<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>


<property name="hibernate.hbm2ddl.auto">update</property>


<!-- 配置hibernate二级缓存相关的属性 -->

</session-factory>


</hibernate-configuration>

3、创建package,com.spring.hibernate.entities,在myeclipse中打开数据库视图,选择数据库中的表,选择反向工程的方法创建实体类





4、配置applicationContext,代码如下

<?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-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">




<!-- 导入资源文件 -->
<context:property-placeholder location="classpath: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="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>

<property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
</bean>
<!-- 配置hibernate的sessionFactory实例  通过spring提供的LocalSessionFactory配置 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<!-- 配置数据源    ,配置文件的位置及名称,   配置hibernate映射文件的位置及名称 -->
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<property name="mappingLocations" value="classpath:com/spring/hibernate/entity/*.hbm.xml"></property>
</bean>
<!-- 配置spring的声明事务 -->
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置事务属性  需要事务管理器 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager"> 
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<!-- 配置事务切点,并把事务切点和事务属性关联起来 -->
<aop:config>
<aop:pointcut expression="execution(* com.spring.hibernate.service.*.*(..))" id="txPointcut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config>

<!-- 指定IOC容器扫描的包 -->
<context:component-scan base-package="com.spring.hibernate">
</context:component-scan>


</beans>


5、建立单元测试测试数据源是否配置成功

代码如下:package com.spring.hibernate.test;


import static org.junit.Assert.*;


import javax.sql.DataSource;


import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class SpringHibernateTest {


private ApplicationContext ctx = null;
{
ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
}
@Test
public void testDataSource() {
DataSource dataSource = (DataSource) ctx.getBean("dataSource");
try {
System.out.println(dataSource.getConnection());
} catch (Exception e) {
// TODO: handle exception
}
}


}

运行结果如图,表示配置成功。



到此配置文件配置完成,下面就可以加入spring mvc、dao层和service层进行进行项目开发了。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值