spring- 整合 jpa spring data

Integración

  • spring 整合 spring data jpa
  • De hecho, spring data jpa obtiene el control correcto, que es administrado por spring data jpa, y no permite que spring administre

Configurar JPA para retrasar el cierre de la sesión

  • colocación 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>

Configurar JPA Factory Bean

  • Introduzca el espacio de nombres jpa de spring data en applicationContext.xml de 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: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> 

  • Configure entityManagerFactory, interfaz central de programación JPA EntityManager (similar a Sesión, agregar, eliminar, modificar y verificar) basado en jpa para la gestión de transacciones
<!-- 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>

Gestión de la cosa


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

Clase de implementación de escaneo

  • Escanee la interfaz DAO y genere automáticamente la clase de implementación
<!--  扫描DAO接口,自动生成实现类 -->
<jpa:repositories base-package="cn.lwb.bos.dao" />

Debido a que el artículo fue grabado durante mucho tiempo, está archivado para su uso

20 artículos originales publicados · Me gusta0 · Visitas 930

Supongo que te gusta

Origin blog.csdn.net/vistaed/article/details/105558423
Recomendado
Clasificación