java持久化api_使用Java持久性API

java持久化api

Java Persistence API(JPA)是供应用程序开发人员理解的重要Java功能。 它准确地将Java开发人员如何将对对象的方法调用转换为访问,持久化和管理NoSQL和关系数据库中存储的数据。

本文通过构建自行车借贷服务的教程示例详细研究了JPA。 本示例将使用Spring Boot框架,MongoDB数据库( 不再开源的 )和Maven包管理器为较大的应用程序创建一个创建,读取,更新和删除(CRUD)层。 我还将NetBeans 11用作选择的IDE。

本教程重点介绍Java Persistence API的开源角度,而不是工具,以展示其工作原理。 这全是关于学习编程应用程序的模式的,但是理解软件仍然很聪明。 您可以在我的GitHub存储库中访问完整的代码。

Java:不仅仅是“ beans”

主要版本每六个月发布一次; 由于功能通常存在重大差异,因此在选择版本之前,您可能需要做一些研究。

总而言之,Java拥有悠久的历史。 本教程重点介绍JDK 11 ,它是Java 11的开源实现,因为它是仍处于活动状态的长期支持版本之一。

  • Spring Boot: Spring Boot是来自Pivotal开发的较大Spring框架的模块。 Spring是使用Java的非常流行的框架。 它允许各种架构和配置。 Spring还提供对Web应用程序和安全性的支持。 Spring Boot提供了用于快速引导各种Java项目的基本配置。 本教程使用Spring Boot快速编写控制台应用程序并针对数据库测试功能。
  • Maven: Maven是由Apache开发的项目/包管理器。 Maven允许在其POM.xml文件中管理软件包和各种依赖项。 如果您使用过NPM,则可能熟悉软件包管理器的功能。 Maven还管理构建和报告功能。
  • Lombok: Lombok是一个库,它允许通过对象文件中的注释来创建对象获取器/设置器。 C#等语言中已经存在该功能,Lombok将此功能引入了Java。
  • NetBeans: NetBeans是一种流行的开源IDE,专门用于Java开发。 它的许多工具都为最新的Java SE和EE更新提供了一个实现。

这组工具将用于为虚构的自行车商店创建一个简单的应用程序。 它将实现插入“客户”和“自行车”对象集合的功能。

酿造完美

导航到Spring Initializr 。 该网站使您能够生成Spring Boot的基本项目需求以及该项目所需的依赖项。 选择以下选项:

  1. 项目: Maven项目
  2. 语言: Java
  3. Spring Boot: 2.1.8(或最稳定的版本)
  4. 项目元数据:无论您的命名约定是什么(例如com.stephb
    • 您可以将神器保留为“演示”
  5. 依赖项:添加:
    • Spring Data MongoDB
    • Lombok

单击下载,然后在所选的IDE(例如NetBeans)中打开新项目。

型号概要

这些模型表示收集的有关程序中特定对象的信息,这些信息将保留在数据库中。 关注两个对象: CustomerBike 。 首先,在src文件夹中创建一个dto文件夹。 然后,创建两个名为Customer.javaBike.java的 Java类对象。 它们在程序中的结构如下:

顾客。 Java


   
   
  1  package com.stephb.JavaMongo.dto ;
  2 
  3  import lombok.Getter ;
  4  import lombok.Setter ;
  5  import org.springframework.data.annotation.Id ;
  6 
  7  /**
 8  *
 9  * @author stephon
10  */

11 @Getter @Setter
12  public class Customer {
13 
14          private @Id String id ;
15          private String emailAddress ;
16          private String firstName ;
17          private String lastName ;
18          private String address ;
19         
20  }

Bike.java


   
   
  1  package com.stephb.JavaMongo.dto ;
  2 
  3  import lombok.Getter ;
  4  import lombok.Setter ;
  5  import org.springframework.data.annotation.Id ;
  6 
  7  /**
 8  *
 9  * @author stephon
10  */

11 @Getter @Setter
12  public class Bike {
13          private @Id String id ;
14          private String modelNumber ;
15          private String color ;
16          private String description ;
17 
18         @Override
19          public String toString ( ) {
20                  return "This bike model is " + this . modelNumber + " is the color " + this . color + " and is " + description ;
21          }
22  }

如您所见,对象中使用了Lombok批注来生成属性/属性的获取器/设置器。 如果您不希望所有属性都包含该类中的getter / setter,则属性可以专门接收注释。 这两个类将构成将您的数据携带到想要显示信息的任何地方的容器。

建立一个数据库

我使用了Mongo Docker容器进行测试。 如果您的系统上安装了MongoDB,则不必在Docker中运行实例。 您可以通过选择系统信息并按照安装说明从其网站上安装MongoDB。

安装后,您可以通过命令行,GUI(例如MongoDB Compass)或用于连接数据源的IDE驱动程序与新的MongoDB服务器进行交互。 现在,您可以定义数据层以提取,转换和保留数据。 要设置数据库访问属性,请导航至应用程序中的applications.properties文件并提供以下内容:


   
   
  1 spring. data . mongodb . host = localhost
  2 spring. data . mongodb . port = 27017
  3 spring. data . mongodb . database = BikeStore

定义数据访问对象/数据访问层

数据访问层(DAL)中的数据访问对象(DAO)将定义您如何与数据库中的数据进行交互。 关于使用spring-boot-starter的很棒的事情是,查询数据库的大部分工作已经完成。

客户 DAO开始。 在src文件夹中的新dao文件夹中创建一个接口,然后创建另一个名为CustomerRepository.java的 Java类名称。 该类应如下所示:


   
   
  1  package com.stephb.JavaMongo.dao ;
  2 
  3  import com.stephb.JavaMongo.dto.Customer ;
  4  import java.util.List ;
  5  import org.springframework.data.mongodb.repository.MongoRepository ;
  6 
  7  /**
 8  *
 9  * @author stephon
10  */

11  public interface CustomerRepository extends MongoRepository < Customer, String > {
12         @Override
13          public List < Customer > findAll ( ) ;
14          public List < Customer > findByFirstName ( String firstName ) ;
15          public List < Customer > findByLastName ( String lastName ) ;
16  }

此类是使用DTO( Customer.java )和字符串从MongoRepository类扩展或继承的接口,因为它们将用于通过自定义函数进行查询。 因为您从此类继承而来,所以您可以访问许多函数,这些函数允许持久化和查询对象,而无需实现或引用自己的函数。 例如,实例化CustomerRepository对象后,可以立即使用Save函数。 如果需要更多扩展功能,也可以覆盖这些功能。 给定对象的特定元素,我创建了一些自定义查询来搜索我的集合。

Bike对象还具有用于与数据库进行交互的存储库。 实施它与CustomerRepository非常相似。 它应该看起来像:


   
   
  1  package com.stephb.JavaMongo.dao ;
  2 
  3  import com.stephb.JavaMongo.dto.Bike ;
  4  import java.util.List ;
  5  import org.springframework.data.mongodb.repository.MongoRepository ;
  6 
  7  /**
 8  *
 9  * @author stephon
10  */

11  public interface BikeRepository extends MongoRepository < Bike,String > {
12          public Bike findByModelNumber ( String modelNumber ) ;
13         @Override
14          public List < Bike > findAll ( ) ;
15          public List < Bike > findByColor ( String color ) ;
16  }

运行程序

现在,您已经有了一种结构化数据的方式,并且可以对数据进行提取,转换和持久化,然后运行程序!

导航到您的Application.java文件(根据您为应用程序命名的名称,它可能具有不同的名称,但应包含“ application”)。 定义类的位置,然后包括一个实现CommandLineRunner 。 这将允许您实现运行方法来创建命令行应用程序。 覆盖CommandLineRunner界面提供的run方法,并包括以下内容以测试BikeRepository


   
   
  1  package com.stephb.JavaMongo ;
  2 
  3  import com.stephb.JavaMongo.dao.BikeRepository ;
  4  import com.stephb.JavaMongo.dao.CustomerRepository ;
  5  import com.stephb.JavaMongo.dto.Bike ;
  6  import java.util.Scanner ;
  7  import org.springframework.beans.factory.annotation.Autowired ;
  8  import org.springframework.boot.CommandLineRunner ;
  9  import org.springframework.boot.SpringApplication ;
10  import org.springframework.boot.autoconfigure.SpringBootApplication ;
11 
12 
13 @SpringBootApplication
14  public class JavaMongoApplication implements CommandLineRunner {
15                 @Autowired
16                  private BikeRepository bikeRepo ;
17                  private CustomerRepository custRepo ;
18                 
19      public static void main ( String [ ] args ) {
20                         SpringApplication. run ( JavaMongoApplication. class , args ) ;
21      }
22         @Override
23          public void run ( String ... args ) throws Exception {
24                 Scanner scan = new Scanner ( System . in ) ;
25                  String response = "" ;
26                  boolean running = true ;
27                  while ( running ) {
28                          System . out . println ( "What would you like to create? \n C: The Customer \n B: Bike? \n X:Close" ) ;
29                         response = scan. nextLine ( ) ;
30                          if ( "B" . equals ( response. toUpperCase ( ) ) ) {
31                                  String [ ] bikeInformation = new String [ 3 ] ;
32                                  System . out . println ( "Enter the information for the Bike" ) ;
33                                  System . out . println ( "Model Number" ) ;
34                                 bikeInformation [ 0 ] = scan. nextLine ( ) ;
35                                  System . out . println ( "Color" ) ;
36                                 bikeInformation [ 1 ] = scan. nextLine ( ) ;
37                                  System . out . println ( "Description" ) ;
38                                 bikeInformation [ 2 ] = scan. nextLine ( ) ;
39 
40                                 Bike bike = new Bike ( ) ;
41                                 bike. setModelNumber ( bikeInformation [ 0 ] ) ;
42                                 bike. setColor ( bikeInformation [ 1 ] ) ;
43                                 bike. setDescription ( bikeInformation [ 2 ] ) ;
44 
45                                 bike = bikeRepo. save ( bike ) ;
46                                  System . out . println ( bike. toString ( ) ) ;
47 
48 
49                          } else if ( "X" . equals ( response. toUpperCase ( ) ) ) {
50                                  System . out . println ( "Bye" ) ;
51                                 running = false ;
52                          } else {
53                                  System . out . println ( "Sorry nothing else works right now!" ) ;
54                          }
55                  }
56                 
57          }
58  }

@Autowired批注允许自动依赖项注入BikeRepositoryCustomerRepository Bean。 您将使用这些类来持久化并从数据库中收集数据。

你有它! 您已经创建了一个命令行应用程序,该应用程序连接到数据库并能够以最少的代码执行CRUD操作。

结论

从对象和类之类的编程语言概念转换为在数据库中存储,检索或更改数据的调用对于构建应用程序至关重要。 Java Persistence API(JPA)是Java开发人员工具包中解决该挑战的重要工具。 您正在使用Java探索哪些数据库? 请分享评论。

翻译自: https://opensource.com/article/19/10/using-java-persistence-api

java持久化api

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值