「Java开发指南」如何在MyEclipse中使用JPA和Spring管理事务?(二)

news/2024/7/16 7:46:00 标签: java, myeclipse, spring, IDE

本教程中介绍一些基于JPA/ spring的特性,重点介绍JPA-Spring集成以及如何利用这些功能。您将学习如何:

  • 为JPA和Spring设置一个项目
  • 逆向工程数据库表来生成实体
  • 实现创建、检索、编辑和删除功能
  • 启用容器管理的事务

在上文中(点击这里回顾>>),我们为大家介绍了如何用JPA和Spring Facets创建一个Java项目以及逆向工程,本文将继续介绍如何创建一个应用并启用容器管理的事务等。

MyEclipse v2023.1.2离线版下载

3. 编写应用程序

现在MyEclipse已经生成了所有这些代码,您可以快速地专注于编写“业务逻辑”,或者更具体地说,“实际执行任务的代码”。

JPA教程介绍了每个实体和DAO类的功能,以及运行一个简单场景的主要方法的基本大纲,包括:

  • 创建一个新实体并将其插入数据库
  • 检索实体
  • 更新实体
  • 删除实体

类似地,在本教程中您将看到如何使用Spring获取和使用DAO以及管理事务。

这个演示的起点是RunJPA.java类,看看这个类中的main方法。

java">/* 1. Initialize the transactionManager and DAO */
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
txManager = ((JpaTransactionManager) ctx.getBean("transactionManager"));
dao = ProductlineDAO.getFromApplicationContext(ctx);

/* 2. Create a reference to our ID */
String productlineID = "Men Shoes";

/* 3. Save a new productline to the DB */
saveProductline(productlineID);

/* 4. Load the productline from DB to make sure it worked */
loadProductline(productlineID);

/* 5. Update the productline in the DB and check it */
updateProductline(productlineID);

/* 6. Delete the productline from the DB */
deleteProductline(productlineID);

用蓝色标记的代码部分是Spring调用,您可以在其中从bean配置中检索已配置的bean。请注意,由于您正在手动管理事务,因此还需要从bean配置中检索' transactionManager '。

剩下的项目,#2 - #6,简单地调用每个“做某事”的方法。

3.1 保存实体

第一个有趣的方法是“saveProductline”,此方法的目的是创建一个新实体并将其存储在DB中。

java">/* 1. Create a new Productline instance */
Productline newProductline = new Productline(productlineID,
"Shoes formen.", "<strong>MenShoes</strong>", null);

/* 2. Store our new product line in the DB */
TransactionStatus status = txManager
.getTransaction(new DefaultTransactionDefinition());
dao.save(newProductline);
txManager.commit(status);

首先,用一些基本值创建新的Productline实例。其次使用transactionManager,事务在将实体保存到DB之前开始。在保存实体之后,事务被提交。

手动管理事务的目的是因为作为开发人员,您知道“保存”操作的范围。根据应用程序的编写方式,一些操作的作用域可能包含许多数据库修改,将所有这些都包装在一个事务中是很重要的,以防在工作进行到一半时失败。您肯定不希望让数据处于一种状态,其中一些是正确的,而另一些是过时的。

3.2 检索实体

下一个方法使用分配给实体的ID从DB中检索实体,并显示其值,这确认保存操作成功。

java">/* 1. Now retrieve the new product line, using the ID we created */
Productline loadedProductline = dao.findById(productlineID);

/* 2. Print out the product line information */
System.out.println("*NEW* Product Line [productLine="
+ loadedProductline.getProductline() + ", textDescription="
+ loadedProductline.getTextdescription() + "]");

注意在这段代码中,没有使用任何事务。原因是这段代码只执行读操作而不执行写操作,即使操作失败,DB中的任何数据都不会受到影响。因此,不需要使用事务来保护操作。

3.3 更新实体

现在下一段代码看起来可能更长,但这是因为它输出了新值,并确认在DB中更新了记录。

java">/* 1. Now retrieve the new product line, using the ID we created */
Productline loadedProductline = dao.findById(productlineID);

/*
* 2. Now let's change same value on the product line, and save the
* change
*/
loadedProductline.setTextdescription("Product line for men's shoes.");

TransactionStatus status = txManager
.getTransaction(new DefaultTransactionDefinition());
dao.update(loadedProductline);
txManager.commit(status);

/*
* 3. Now let's load the product line from the DB again, and make sure
* its text description changed
*/
Productline secondLoadedProductline = dao.findById(productlineID);

System.out.println("*REVISED* Product Line [" + "productLine="
+ secondLoadedProductline.getProductline()
+ ", textDescription="
+ secondLoadedProductline.getTextdescription() + "]");

请注意更新调用被封装在事务中,因为它必须向数据库写入一些内容,并且需要防止失败。

在上面的第3节中,产品线在更新后立即从数据库加载,并通过打印从数据库返回的值来确认更新。

3.4 删除实体

删除实体几乎等同于保存和更新实体,工作被封装在另一个事务中,然后DAO被告知去做这项工作。

java">/* 1. Now retrieve the new product line,
using the ID we created */
TransactionStatus status = txManager
.getTransaction(new DefaultTransactionDefinition());
Productline loadedProductline = dao.findById(productlineID);

/* 2. Now let's delete the product line from
the DB */
dao.delete(loadedProductline);
txManager.commit(status);

/*
* 3. To confirm the deletion, try and load it again and make sure it
* fails
*/
Productline deletedProductline = dao.findById(productlineID);

/*
* 4. We use a simple inline IF clause to test for null and print
* SUCCESSFUL/FAILED
*/
System.out.println("Productline deletion: "
+ (deletedProductline == null ? "SUCCESSFUL" : "FAILED"));

与上面的“updateProductline”实现类似,您会注意到一个事务用于封装“delete”调用,然后代码尝试从DB加载实体并确认操作应该失败。

注意:事务必须封装'findById '和'delete '方法调用的原因是因为JPA管理的对象必须是同一事务的一部分,要擦除已加载的对象,它必须处于加载它并尝试擦除它的同一个事务中。

3.5 运行程序

运行此命令的输出如下所示:

如何在MyEclipse中使用JPA和Spring管理事务?

输出

红色文本是可以忽略的默认日志消息(如果希望控制日志记录,您可以设置自定义log4j.properties文件),在日志警告下面,您可以看到来自TopLink (JPA实现库)的两条消息,以及来自实现的另外三条消息。

第一个消息打印出添加的新产品线信息,第二个消息更新产品线信息并打印新信息,最后一个消息从DB中删除产品线信息并打印确认消息。

4. 启用Spring容器管理的事务

除了用户管理的事务之外,Spring还通过@Transactional属性支持容器管理的事务。对于容器管理的事务支持,您必须在添加facet时启用它,这是您在本教程前面所做的。

如何在MyEclipse中使用JPA和Spring管理事务?

启用对@Transactional注释的支持

启用此功能将向bean配置文件添加以下事务元素,您还应该添加一个JPAServiceBean,它用于使用容器管理的事务删除实体。请参阅下面的实现:

如何在MyEclipse中使用JPA和Spring管理事务?

注解驱动的配置元素

JPAServiceBean实现如下所示;注意'deleteProductLine '方法上的'@Transactional '注释和没有任何用户管理的事务语句。

java">public class JPAServiceBean
{ private IProductlineDAO dao; @Transactional public void deleteProductLine(String productlineID)

{ /* 1. Now retrieve the new product line, using the ID we created */Productline loadedProductline = dao.findById(productlineID);

/* 2. Now let's delete the product line from the DB */
dao.delete(loadedProductline);

/*
* 3. To confirm the deletion, try and load it again and make sure it * fails
*/
Productline deletedProductline = dao.findById(productlineID);

/*
* 4. We use a simple inline IF clause to test for null and print
* SUCCESSFUL/FAILED
*/
System.out.println("Productline deletion: " + (deletedProductline == null ? "SUCCESSFUL" : "FAILED"));}

public void setProductLineDAO(IProductlineDAO dao) { this.dao = dao; }
}

从应用程序上下文中获取JPAServiceBean实例,并按如下方式使用它:

java">JPAServiceBean bean = (JPAServiceBean) ctx.getBean("JPAServiceBean");
bean.deleteProductLine(productlineID);


http://www.niftyadmin.cn/n/5120266.html

相关文章

基于樽海鞘群算法的无人机航迹规划-附代码

基于樽海鞘群算法的无人机航迹规划 文章目录 基于樽海鞘群算法的无人机航迹规划1.樽海鞘群搜索算法2.无人机飞行环境建模3.无人机航迹规划建模4.实验结果4.1地图创建4.2 航迹规划 5.参考文献6.Matlab代码 摘要&#xff1a;本文主要介绍利用樽海鞘群算法来优化无人机航迹规划。 …

项目开发过程中常用到的免费api接口

IP应用场景-IPv4&#xff0c;IPv4应用场景是获取IP场景属性的在线调用接口&#xff0c;具备识别IP真人度&#xff0c;提升风控和反欺诈等业务能力。IP应用场景基于地理和网络特征的IP场景划分技术&#xff0c;将IP划分为含数据中心、交换中心、家庭宽带、CDN、云网络等共计18类…

IDEA常用的一些插件

1、CodeGlance 代码迷你缩放图插件&#xff0c;可以快速拖动代码&#xff0c;和VScode一样 2、Codota 代码提示工具&#xff0c;扫描你的代码后&#xff0c;根据你的敲击完美提示。 Codota基于数百万个开源Java程序和您的上下文来完成代码行&#xff0c;从而帮助您以更少的…

VB.NET 三层登录系统实战:从设计到部署全流程详解

目录 前言&#xff1a; 什么是三层 为什么要用到三层: 饭店→软件 理解: 过程: 1.三层包图: 2.数据库 3.三层项目 4.用户界面 5.添加引用 代码实现: Entity层 BLL层 DAL层 UI层 总结: 前言&#xff1a; 什么是三层 三层就是把各个功能模块划分为表示层&#…

电子日历-参数

PW75R_C1 产品参数 产品型号 PW75R_C1 尺寸(mm) 180*130*13mm 显示技术 3色墨水屏显示技术 显示区域(mm) 163.2(H) * 97.92(V) 分辨率(像素) 800*480 像素尺寸(mm) 0.204*0.204 显示颜色 黑/白/红 视觉角度 180 工作温度 0 - 40℃ 产品重量 200g 电池容…

ETHERNET/IP从站转CANOPEN主站连接AB系统的配置方法

你还在为配置网关的ETHERNET/IP从站和CANOPEN主站发愁吗&#xff1f;今天来教你解决办法&#xff01; 一&#xff0c;首先&#xff0c;配置网关的ETHERNET/IP从站&#xff0c;需要使用AB系统的配置方法&#xff0c;具体步骤如下 1&#xff0c;使用 AB 系统配置网关的 ETHERNET/…

嵌入式实时操作系统的设计与开发(消息)

消息 从概念上讲&#xff0c;消息机制和邮箱机制很类似&#xff0c;区别在于邮箱一般只能容纳一条消息&#xff0c;而消息则会包含一系列的消息。 系统定义了一个全局变量g_msgctr_header&#xff0c;通过它可以查找到任一已创建的消息容器。 每一个消息容器都可以根据其参数…

Nginx的请求时间限制(如周一到周五可以访问)

方案一&#xff1a;简答修改nginx配置文件&#xff0c;不支持复杂逻辑 方案二&#xff1a;使用Lua脚本执行拦截&#xff0c;使用过程比较复杂&#xff0c;&#xff08;还未处理&#xff09; 修改nginx的配置文件 方案一 因为Nginx中只支持简单的if语句,所有只写if # 在 loc…