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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 最大似然估计,最大后验估计,贝叶斯估计联系与区别

    最大似然估计,最大后验估计,贝叶斯估计联系与区别1.什么是参数在机器学习中,我们经常使用一个模型来描述生成观察数据的过程。例如,我们可以使用一个随机森林模型来分类客户是否会取消订阅服务(称为流失建模),或者我们可以用线性模型根据公司的广告支出来预测公司的收入(这是一个线性回归的例子)。每个模型都包含自己的一组参数,这些参数最终定义了模型本身。我们可以把线性模型写成y=mx+c的形式。在广告预测收入的例子中,x可以表示广告支…

    2022年10月19日
    0
  • 用Attributes.Add添加事件

    用Attributes.Add添加事件1、(textbox2.Visible=true)JS的寫法:    textbox2.Attributes.Add(“onfocus”,”document.getElementById(‘textbox2’).style.display=”);2、(textbox2.Visible=false)JS的寫法:    textbox2.Attributes.Add(“onfocus”,”document.getElementById(‘textbox2’).style.display=’no

    2022年9月26日
    0
  • Python获取int最大值和float最大值

    Python获取int最大值和float最大值计算机所能表示的最大值 根据你的计算机的位数决定 有机计算机是 64 位 有的是 32 位 因此具体情况各不相同 本人的电脑是 64 位的 1 获得 int 型的最大值 importsysMAX INT sys maxsizeprint MAX INT 2 获得 float 型的最大值灰常简单 max float float inf 是的 你没有看错 最大的浮点数就是这个 inf

    2025年6月10日
    0
  • 理解 sudo 和 sudoers[通俗易懂]

    理解 sudo 和 sudoers[通俗易懂]在Linux上,只有root用户可以执行任何命令,其他用户必须使用sudo才可执行特殊的命令.sudo是通过sudoers进行配置的.默认配置/etc/sudoers:##ThisfileMUSTbeeditedwiththe’visudo’commandasroot.##Pleaseconsideraddinglocalcontentin/etc/sudoers.d/insteadof#directlymodifying

    2022年6月20日
    29
  • smtp服务器组件,本机搭建虚拟SMTP服务器教程[通俗易懂]

    smtp服务器组件,本机搭建虚拟SMTP服务器教程[通俗易懂]该楼层疑似违规已被系统折叠隐藏此楼查看此楼Windows2000用户安装设置服务端WindowsXP和2000本身就拥有构件SMTP服务器的功能,只是一般还没有安装。选择“控制面板→添加/删除程序→添加/删除Windows组件”,弹出“Windows组件向导”对话框,在其中双击“Internet信息服务(IIS)”项,就会打开详细选择项,选中“SMTPService”,按“确定”,插入…

    2022年10月3日
    0
  • Android微信支付订单支付失败的问题

    Android微信支付订单支付失败的问题Android开发使用微信支付,如果说SDK集成正确,然后订单信息配置无误,就是调不起来支付页面,那就要考虑一下微信缓存的问题。当我们的APP需要更换签名,或者说替换Ping++的SDK,就要考虑微信缓存导致新版本调不起来支付页面。我们只要将微信退出一次就OK了。最奇葩的是我从服务器获取订单信息的接口从本地替换成正式的,就调不起来微信支付页面了,还好尝试了一下退出微信一次,就能够成功地调起支付…

    2022年6月5日
    61

发表回复

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

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