spring-整合spring data jpa

整合

  • spring整合spring data jpa
  • 其实就是spring data jpa 把控制权获取到,由spring data jpa来管理,不让spring管理

配置JPA延迟session关闭

  • xml配置
<filter>
	<filter-name>openEntityManagerInViewFilter</filter-name>
	<filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
</filter>
<filter-mapping>
	<filter-name>openEntityManagerInViewFilter</filter-name>
	<url-pattern>/*</url-pattern>
</filter-mapping>

配置JPA工厂Bean

  • 在spring的applicationContext.xml引入spring data jpa名称空间
<?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:aop="http://www.springframework.org/schema/aop" 
		xmlns:context="http://www.springframework.org/schema/context"
		xmlns:jdbc="http://www.springframework.org/schema/jdbc"
		xmlns:tx="http://www.springframework.org/schema/tx" 
		xmlns:jpa="http://www.springframework.org/schema/data/jpa" 
		xmlns:task="http://www.springframework.org/schema/task"
		xsi:schemaLocation="
						http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
						http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
						http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
						http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
						http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
						http://www.springframework.org/schema/data/jpa 
						http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
</beans> 

  • 配置entityManagerFactory ,JPA编程核心接口 EntityManager (类似Session, 进行增删改查 )基于jpa进行事务管理
<!-- spring整合JPA -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
	<property name="dataSource" ref="dataSource" />
	<property name="packagesToScan" value="cn.lwb.bos.domain" />
	
	<property name="persistenceProvider">
		<bean class="org.hibernate.ejb.HibernatePersistence" />
	</property>
	<property name="jpaVendorAdapter">
		<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
			<!-- 自动建表 -->
			<property name="generateDdl" value="true" />
			<property name="database" value="ORACLE" />
			<property name="databasePlatform" value="org.hibernate.dialect.Oracle10gDialect" />
			<property name="showSql" value="true" />
		</bean>
	</property>
	<property name="jpaDialect">
		<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
	</property>
</bean>

事物管理


<!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
	<!-- 需要注入 entityManagerFactory-->
      <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

扫描实现类

  • 扫描DAO接口,自动生成实现类
<!--  扫描DAO接口,自动生成实现类 -->
<jpa:repositories base-package="cn.lwb.bos.dao" />

由于文章是记录了很久的文章了故作存档使用

发布了20 篇原创文章 · 获赞 0 · 访问量 930

猜你喜欢

转载自blog.csdn.net/vistaed/article/details/105558423