1.Spring2.5与JPA的整合
1.首先引入jar的时候,由于用的是由hibernate来实现的JPA,所以jar与spring与hibernate整合时的是一样的。
2.工程的整体目录结构:
3.bean.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/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-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:component-scan base-package="com.gaoyuan.service"/>
<!--此处spring和JPA进行整合,JPA用的是hibernate实现的,所以用到的jar包和spring与hibernate整合时的一样-->
<!--JPA中的entityManagerFactory和hibernate中sessionFactory的作用是类似的
spring中的org.springframework.orm.jpa.LocalEntityManagerFactoryBean类可以创建出entityManagerFactory对象,
-->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<!--配置持久化单元 ,此处的value值是:META-INF文件夹下的persistence.xml文件中配置的
<persistence-unit name="itcast" transaction-type="RESOURCE_LOCAL">中的name值
-->
<property name="persistenceUnitName" value="itcast"/>
</bean>
<!-- 配置事务 -->
<bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<tx:annotation-driven transaction-manager="txManager"/>
</beans>
4.persistence.xml文件的配置,文件的名字和文件夹的名字META-INF是死的,必须这样写。
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
<persistence-unit name="xxxxx" transaction-type="RESOURCE_LOCAL">
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value="123456"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8"/>
<property name="hibernate.max_fetch_depth" value="3"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
</persistence>
5.实体bean的定义
6.personServiceImpl的实现:
package com.gaoyuan.service.impl;
import java.util.List;
import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.gaoyuan.bean.Person;
import com.gaoyuan.service.PersonService;
@Service("personService")
@Transactional
public class PersonServiceImpl implements PersonService {
//注入EntityManager对象,JPA中利用的是EntityManager来进行数据的增删改查
@PersistenceContext EntityManager em;
public void save(Person person){
em.persist(person);
}
public void update(Person person){
em.merge(person);
}
//读取操作的时候,将事务关闭,不用再打开事务,这样可以提高性能
@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
public Person getPerson(Integer id){
return em.find(Person.class, id);
}
public void delete(Integer id){
//getReference(),方法和hibernate中的load方法类似。
em.remove(em.getReference(Person.class,id));
}
//读取操作的时候,将事务关闭,不用再打开事务,这样可以提高性能
@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
@SuppressWarnings("unchecked")
public List<Person> getPersons(){
return em.createQuery("select o from Person o").getResultList();
}
}
7.与之对应的接口:PersonService.java
8.测试类和spring与hibernate整合时的一样。
网友评论