大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全家桶1年46,售后保障稳定
首先,在做关于JpaTransactionManager之前,先对Jpa做一个简单的了解,他毕竟不如hibernate那么热门,其实二者很相识,只不过后期hibernate和JDO 版本都已经兼容了其Jpa,目前大家用的少了。
package com.spring.jpa;
import com.spring.model.Product;
public interface ProductDao {
public void save(Product p);
}
Jetbrains全家桶1年46,售后保障稳定
2.productDaoImpl.java
package com.spring.jpa;import javax.persistence.EntityManager;import javax.persistence.EntityManagerFactory;import javax.persistence.Persistence;import com.spring.model.Product;public class ProductDaoImpl implements ProductDao { public void save(Product p) { EntityManagerFactory emf = Persistence.createEntityManagerFactory("SimplePU"); EntityManager em = emf.createEntityManager(); em.getTransaction().begin(); em.persist(p); em.getTransaction().commit(); emf.close(); }}
3.ProductManager.java
package com.spring.jpa;import com.spring.model.Product;public interface ProductManager { public void save(Product p);}
4.ProductManagerImpl.java
package com.spring.jpa;import com.spring.model.Product;public class ProductManagerImpl implements ProductManager{ private ProductDao productDao = new ProductDaoImpl(); @Override public void save(Product p) { productDao.save(p); }}
5.persistence.xml
<?xml version="1.0" encoding="UTF-8"?><!-- 切记,该文件一定要放在/WEB-INF/classes/META-INF这里才能生效 --><persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0"> <persistence-unit name="SimplePU" transaction-type="RESOURCE_LOCAL"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <class>com.spring.model.Product</class> <properties> <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" /> <property name="hibernate.connection.url" value="jdbc:mysql://127.0.0.1:3306/jinhonglun" /> <property name="hibernate.connection.username" value="root" /> <property name="hibernate.connection.password" value="root" /> <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" /> <property name="hibernate.show_sql" value="true" /> <property name="hibernate.format_sql" value="true" /> <property name="hibernate.use_sql_comments" value="false" /> <property name="hibernate.hbm2ddl.auto" value="update" /> </properties> </persistence-unit></persistence>
6.SimpleSpringJpaDemo.java
package com.spring.jpa;import com.spring.model.Product;public class SimpleSpringJpaDemo { public static void main(String[] args) { Product p = new Product(); p.setProductTitle("JPA测试"); new ProductManagerImpl().save(p); }}
接下来进行主题介绍JpaTransactionManager:
* 我们引入 Spring,以展示 Spring 框架对 JPA 的支持。业务层接口 ProductManager 保持不变,ProductManagerImpl 中增加了三个注解,
* 以让 Spring 完成依赖注入,因此不再需要使用 new 操作符创建 ProductDaoImpl 对象了。同时我们还使用了 Spring 的声明式事务:
package com.spring.jpaTransactionManager;import javax.persistence.EntityManager;import javax.persistence.PersistenceContext;import org.springframework.stereotype.Repository;import org.springframework.transaction.annotation.Transactional;import com.spring.model.Product;@Repository("productDao")public class ProductDaoImpl implements ProductDao { @PersistenceContext private EntityManager em; @Transactional public void save(Product p) { em.persist(p); }}
2.ProductManagerImpl.java
package com.spring.jpaTransactionManager;import javax.annotation.Resource;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Transactional;import com.spring.model.Product;@Service("productManager")public class ProductManagerImpl implements ProductManager{ @Resource private ProductDao productDao; @Transactional @Override public void save(Product p) { productDao.save(p); }}
3.SimpleSpringJpaDemo.java
package com.spring.jpaTransactionManager;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.spring.model.Product;public class SimpleSpringJpaDemo { public static void main(String[] args) { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:/com/spring/jpaTransactionManager/spring-jpa.xml"); ProductDao productDao = ctx.getBean("productDao", ProductDao.class); Product p = new Product(); p.setProductTitle("JPA Spring与JpaTransactionManager结合测试"); productDao.save(p); }}
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop" 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/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <!-- 自动扫描包,自动将@Repository、@Service、@Controller 和 @Component自动实例化 --> <context:component-scan base-package="com.spring.jpaTransactionManager" /> <!-- Spring 事务配置,声明式事务 --> <tx:annotation-driven transaction-manager="transactionManager" /> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory" /> </bean> <!-- LocalContainerEntityManagerFactoryBean 会自动检测 persistence units , 实际上,就是META-INF/persistence.xml(/WEB-INF/classes/META-INF) 文件和web.xml中的persistence-unit-ref, 以及定义的environment naming --> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> </bean></beans>
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/213515.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...