标签:jpa
Spring 整合 JPA
三种整合方式:
-LocalEntityManagerFactoryBean:适用于那些仅使用 JPA 进行数据访问的项目,该 FactoryBean 将根据JPA PersistenceProvider 自动检测配置文件进行工作,一般从“META-INF/persistence.xml”读取配置信息,这种方式最简单,但不能设置 Spring 中定义的DataSource,且不支持 Spring 管理的全局事务
-从JNDI中获取:用于从 Java EE 服务器获取指定的EntityManagerFactory,这种方式在进行 Spring 事务管理时一般要使用 JTA 事务管理
-LocalContainerEntityManagerFactoryBean:适用于所有环境的 FactoryBean,能全面控制 EntityManagerFactory 配置,如指定 Spring 定义的 DataSource 等等。
目录结构如图
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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" 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-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> <!-- 启用注解注入 --> <!-- <context:annotation-config /> --> <!-- 组件扫描 --> <context:component-scan base-package="com.jpa"></context:component-scan> <!-- 导入资源文件 --> <!-- <context:property-placeholder location="classpath:db.properties" /> --> <!-- 配置C3P0数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="user" value="root"></property> <property name="password" value=""></property> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql:///test"></property> <property name="initialPoolSize" value="5"></property> <property name="maxPoolSize" value="10"></property> </bean> <!-- 配置EntityManagerFactory --> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dataSource"></property> <!-- 配置 JPA 提供商的适配器. 可以通过内部 bean 的方式来配置 --> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"></bean> </property> <!-- 配置实体类所在的包 --> <property name="packagesToScan" value="com.jpa.entities"></property> <!-- 配置 JPA 的基本属性. 例如 JPA 实现产品的属性 --> <property name="jpaProperties"> <props> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> </bean> <!-- 配置 JPA 使用的事务管理器 --> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory"></property> </bean> <!-- 配置支持基于注解是事务配置 --> <tx:annotation-driven transaction-manager="transactionManager"/> </beans>
Person.java实体类
@Table(name = "jpa_persons") @Entity public class Person { @GeneratedValue @Id private Integer id; @Column(name = "last_name") private String lastName; private String email; private int age; //getter setter }
PersonDao
@Repository public class PersonDao { //如何获取到和当前事务关联的EntityManager对象? //通过@PersistenceContext注解来标记成员变量 @PersistenceContext private EntityManager entityManager; public void save(Person person) { entityManager.persist(person); } }
PersonService
@Service public class PersonService { @Autowired private PersonDao personDao; @Transactional public void save(Person p1, Person p2) { personDao.save(p1); int i = 10 / 0;//抛异常,测试事务 personDao.save(p2); } }
测试
public class JPATest { private PersonService personService = null; private ApplicationContext applicationContext = null; { applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); personService = applicationContext.getBean(PersonService.class); } @Test public void testDataSource() { DataSource dataSource = applicationContext.getBean(DataSource.class); try { System.out.println(dataSource.getConnection()); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Test public void testPersonService() { Person p1 = new Person(); p1.setAge(21); p1.setEmail("umgsai@126.com"); p1.setLastName("SS"); Person p2 = new Person(); p2.setAge(24); p2.setEmail("umgsai@163.com"); p2.setLastName("DD"); System.out.println(personService.getClass().getName()); personService.save(p1, p2); } }
源码:http://yunpan.cn/cFzde9uLeFLJB 访问密码 1aeb
本文出自 “阿凡达” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/1708072
标签:jpa
原文地址:http://shamrock.blog.51cto.com/2079212/1708072