– Start
下面的例子演示了如何与 Spring 整合。
<?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">
<!--
告诉 Spring 解析bean中的注解
注意需要引入 context 命名空间
-->
<context:annotation-config/>
<!--
告诉 Spring 扫描组件的位置
-->
<context:component-scan base-package="shangbo.hibernate.demo035"/>
<!-- 定义 dataSource -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe" />
<property name="username" value="hr" />
<property name="password" value="456789" />
</bean>
<!-- 定义 Hibernate sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 配置实体类: 方法1 -->
<property name="annotatedClasses">
<list>
<value>shangbo.hibernate.demo035.Customer</value>
</list>
</property>
<!-- 配置实体类: 方法2 -->
<!--
<property name="packagesToScan">
<list>
<value>shangbo.hibernate.demo035</value>
</list>
</property>
-->
<!-- 配置实体类: 方法3 -->
<!--
<property name="mappingResources">
<list>
<value>Customer.hbm.xml</value>
</list>
</property>
-->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle12cDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
</bean>
<!-- 定义 Hibernate 事务管理器 -->
<bean id="txManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 开启事务管理 -->
<tx:annotation-driven transaction-manager="txManager"/>
</beans>
package shangbo.hibernate.demo035;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
public static void main(String[] args) {
// 实例化 Spring IoC 容器
ApplicationContext context = new ClassPathXmlApplicationContext("demo.xml", BusinessService.class);
// 从容器中获得 businessService
BusinessService businessService = context.getBean(BusinessService.class);
// 使用对象
Customer c = new Customer("test");
businessService.save(c);
}
}
package shangbo.hibernate.demo035;
import javax.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class BusinessServiceImpl implements BusinessService {
@Autowired
private DataService dataService;
@Override
@Transactional
public void save(Object o) {
dataService.save(o);
}
}
package shangbo.hibernate.demo035;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class DataServiceImpl implements DataService {
@Autowired
private SessionFactory sessionFactory;
@Override
public void save(Object o) {
Session session = sessionFactory.getCurrentSession();
session.save(o);
}
}
– 更多参见:Hibernate 精萃
– 声 明:转载请注明出处
– Last Updated on 2019-07-19
– Written by ShangBo on 2019-07-19
– End