spring- 整合 jpa spring data

Integration

  • spring 整合 spring data jpa
  • In fact, spring data jpa obtains the control right, which is managed by spring data jpa, and does not let spring manage

Configure JPA to delay session closure

  • xml placement
<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>

Configure JPA Factory Bean

  • Introduce the spring data jpa namespace in spring's 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: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, JPA programming core interface EntityManager (similar to Session, add, delete, modify and check) based on jpa for transaction management
<!-- 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>

Thing management


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

Scanning implementation class

  • Scan the DAO interface and automatically generate the implementation class
<!--  扫描DAO接口,自动生成实现类 -->
<jpa:repositories base-package="cn.lwb.bos.dao" />

Because the article was recorded for a long time, it is archived for use

Published 20 original articles · Likes0 · Visits 930

Guess you like

Origin blog.csdn.net/vistaed/article/details/105558423