Spring JpaTransactionManager事务管理

Spring JpaTransactionManager事务管理    首先,在做关于JpaTransactionManager之前,先对Jpa做一个简单的了解,他毕竟不如hibernate那么热门,其实二者很相识,只不过后期hibernate和JDO版本都已经兼容了其Jpa,目前大家用的少了。   JPA全称JavaPersistenceAPI.JPA通过JDK5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全家桶1年46,售后保障稳定

     首先,在做关于JpaTransactionManager之前,先对Jpa做一个简单的了解,他毕竟不如hibernate那么热门,其实二者很相识,只不过后期hibernate和JDO 版本都已经兼容了其Jpa,目前大家用的少了。

 
  JPA全称Java Persistence API.JPA通过JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中。
 
  JPA的宗旨是为POJO提供持久化标准规范,由此可见,经过这几年的实践探索,能够脱离容器独立运行,方便开发和测试的理念已经深入人心了。Hibernate3.2、TopLink 10.1.3以及OpenJPA都提供了JPA的实现。
 
JPA的总体思想和现有Hibernate、TopLink、JDO等ORM框架大体一致。总的来说,JPA包括以下3方面的技术:
 
ORM映射元数据
 
JPA支持XML和JDK5.0注解两种元数据的形式,元数据描述对象和表之间的映射关系,框架据此将实体对象持久化到数据库表中;
 
API
 
用来操作实体对象,执行CRUD操作,框架在后台替我们完成所有的事情,开发者从繁琐的JDBC和SQL代码中解脱出来。
 
查询语言
 
这是持久化操作中很重要的一个方面,通过面向对象而非面向数据库的查询语言查询数据,避免程序的SQL语句紧密耦合。
下面,做一些JPA简单的例子,让大家熟悉一下JPA的配置:
1.productDao.java
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 的声明式事务:

1.ProductDaoImpl.java
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);	}}

 

4.spring-jpa.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: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>

  

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/213515.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Win10 桌面美化

    Win10 桌面美化Win10桌面美化最近发现了几款Win10界面美化的软件,看了看别人家的Win10操作界面,瞬间觉得自己的low了,关键是赏心悦目啊!废话不多说,先看看我原来桌面和美化后的桌面对比图原始桌面美化桌面1.安装RocketDockRocketDock可以提供类似macos的操作系统图标特效,打开安装包进行安装,完毕后启动得到效果如下:可以发现切换效果与mac类似,他默认的主题是C…

    2022年4月25日
    36
  • pve安装docker图形化_怎么安装docker

    pve安装docker图形化_怎么安装docker基本相当于转载,但我光贴图也不行啊,还是把步骤加上去年年底这NUC到手时,就装PVE练手,然而最后做主力挂机的是那会买的个1037U小主机当时就尝试过装LibreELEC,结果是接显示器黑屏不过后来我直接在宿主机上装桌面和Kodi,但是吧那源里的Kodi是17的对于x265是软解。。。怎么装当然是看的值得买上的这个人家是OMV咱这是PVE都是装桌面再装KODI,但是为了保持纯净的PVE所以才想直通…

    2025年6月23日
    1
  • Nginx简单配置转发

    问题分析一台服务器运行多个项目的时候会遇到这样的问题:如果使用同一个tomcat来启动不同项目的话,项目之间会相互影响;如果用多个tomcat运行项目,那么在访问项目的时候又不能都使用80端口,还要加上端口号,显得很麻烦又不美观。考虑用Nginx实现转发,目标是通过访问不同的域名实现对不同tomcat上运行的项目的跳转,例如访问www.a.com跳转到本地的8088端口的项目,访问www…

    2022年4月5日
    117
  • 【21】进大厂必须掌握的面试题-65个SQL面试

    点击上方“全栈程序员社区”,星标公众号 重磅干货,第一时间送达 Q1。SQL和MySQL有什么区别? SQL MySQL SQL是一种标准语言,代表基于英语的结构化查询语言 MyS…

    2021年6月24日
    83
  • 把VueThink整合到已有ThinkPHP 5.0项目中

    把VueThink整合到已有ThinkPHP 5.0项目中

    2021年10月11日
    48
  • 从零开始学习UCOSII操作系统2–UCOSII的内核实现「建议收藏」

    从零开始学习UCOSII操作系统2–UCOSII的内核实现「建议收藏」从零开始学习UCOSII操作系统2–UCOSII的内核实现参考书籍:《嵌入式实时操作系统μCOS-II原理及应用》、《嵌入式实时操作系统uCOS-II邵贝贝(第二版)》1、任务的结构–任务控制块首先这个任务控制块是非常的大的,这里面使用很多的宏定义,估计是可以让使用者使用的时候按需配置。所以这里只是整理一些必须要用到的功能,不常用的不讲,讲了就会变成一本书了。(1)任务的关键 OS_ST…

    2022年6月4日
    43

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号