Spring—Hibernate

1.家jar包

2配置applicationContext与xxx.hbm.xml(根据需要决定是否配置hibernate.hbm.xml)

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



<context:property-placeholder location="classpath:jdbc.properties"/>


<!-- 一、配置数据库连接池 ComboPooledDataSource -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">

<!-- Connection properties -->
<property name="driverClass" value="${driverClass}" />
<property name="jdbcUrl" value="${jdbcUrl}" />
<property name="user" value="${username}" />
<property name="password" value="${password}" />
<!-- Pool properties-->
<property name="minPoolSize" value="2" />
<property name="maxPoolSize" value="10" />
<property name="maxStatements" value="50" />
<property name="idleConnectionTestPeriod" value="3000" />
<property name="loginTimeout" value="300" />

</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>

<!--方式一
直接从外部读取配置文件,此时需要有相应的Hibernate。hbm.xml,xxx.hbm.xml配置,容器找不到的配置会在这个路径去找 -->
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>

<!-- 方式二:
若没有外部Hibernate.hbm.xml配置文件,只能在自身文件查找,但相应的xxx.hbm.xml(bean对应的文件)还需添加。
注意:再配置hibernate其他属性的时候在名字前要加上hibernate,否则配置无效
<property name="mappingResources">
<list>
引入xxx.hbm.xml
<value>com/donghua/jdbc_dao/User.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props >
配置hibernate的其他配置
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
-->
</bean>

<bean id="userDao" class="com.donghua.jdbc_dao.UserDao">
<property name="factory" ref="sessionFactory"></property>
</bean>
</beans>

原文地址:https://www.cnblogs.com/daxiong225/p/4720679.html