使用Java持久化API
我們通過為自行車商店構(gòu)建示例應(yīng)用程序來學(xué)習(xí)如何使用 JPA。
對應(yīng)用開發(fā)者來說,Java 持久化 API(JPA)是一項重要的 java 功能,需要透徹理解。它為 Java 開發(fā)人員定義了如何將對象的方法調(diào)用轉(zhuǎn)換為訪問、持久化及管理存儲在 NoSQL 和關(guān)系型數(shù)據(jù)庫中的數(shù)據(jù)的方案。
本文通過構(gòu)建自行車借貸服務(wù)的教程示例來詳細(xì)研究 JPA。此示例會使用 Spring Boot 框架、MongoDB 數(shù)據(jù)庫(已經(jīng)不開源)和 Maven 包管理來構(gòu)建一個大型應(yīng)用程序,并且構(gòu)建一個創(chuàng)建、讀取、更新和刪除(CRUD)層。這兒我選擇 NetBeans 11 作為我的 IDE。
此教程僅從開源的角度來介紹 Java 持久化 API 的工作原理,不涉及其作為工具的使用說明。這全是關(guān)于編寫應(yīng)用程序模式的學(xué)習(xí),但對于理解具體的軟件實現(xiàn)也很益處??梢詮奈业?GitHub 倉庫來獲取相關(guān)代碼。
Java: 不僅僅是“豆子”
Java 是一門面向?qū)ο蟮木幊陶Z言,自 1996 年發(fā)布第一版 Java 開發(fā)工具(JDK)起,已經(jīng)變化了很多很多。要了解其各種發(fā)展及其虛擬機本身就是一堂歷史課。簡而言之,和 Linux 內(nèi)核很相似,自發(fā)布以來,該語言已經(jīng)向多個方向分支發(fā)展。有對社區(qū)免費的標(biāo)準(zhǔn)版本、有針對企業(yè)的企業(yè)版本及由多家供應(yīng)商提供的開源替代品。主要版本每六個月發(fā)布一次,其功能往往差異很大,所以確認(rèn)選用版本前得先做些研究。
總而言之,Java 的歷史很悠久。本教程重點介紹 Java 11 的開源實現(xiàn) JDK 11。因其是仍然有效的長期支持版本之一。
- Spring Boot 是由 Pivotal 公司開發(fā)的大型 Spring 框架的一個模塊。Spring 是 Java 開發(fā)中一個非常流行的框架。它支持各種框架和配置,也為 WEB 應(yīng)用程序及安全提供了保障。Spring Boot 為快速構(gòu)建各種類型的 Java 項目提供了基本的配置。本教程使用 Spring Boot 來快速編寫控制臺應(yīng)用程序并針對數(shù)據(jù)庫編寫測試用例。
- Maven 是由 Apache 開發(fā)的項目/包管理工具。Maven 通過
POM.xml
文件來管理包及其依賴項。如果你使用過 NPM 的話,可能會非常熟悉包管理器的功能。此外 Maven 也用來進(jìn)行項目構(gòu)建及生成功能報告。 - Lombok 是一個庫,它通過在對象文件里面添加注解來自動創(chuàng)建 getters/setters 方法。像 C# 這些語言已經(jīng)實現(xiàn)了此功能,Lombok 只是把此功能引入 Java 語言而已。
- NetBeans 是一款很流行的開源 IDE,專門用于 Java 開發(fā)。它的許多工具都隨著 Java SE 和 EE 的版本更新而更新。
我們會用這組工具為一個虛構(gòu)自行車商店創(chuàng)建一個簡單的應(yīng)用程序。會實現(xiàn)對 Customer
和 Bike
對象集合的的插入操作。
釀造完美
導(dǎo)航到 Spring Initializr 頁面。該網(wǎng)站可以生成基于 Spring Boot 和其依賴項的基本項目。選擇以下選項:
- 項目: Maven 工程
- 語言: Java
- Spring Boot: 2.1.8(或最穩(wěn)定版本)
- 項目元數(shù)據(jù): 無論你使用什么名字,其命名約定都是像
com.stephb
這樣的。- 你可以保留 Artifact 名字為 “Demo”。
- 依賴項: 添加:
- Spring Data MongoDB
- Lombok
點擊 下載,然后用你的 IDE(例如 NetBeans) 打開此新項目。
模型層概要
在項目里面,模型代表從數(shù)據(jù)庫里取出的信息的具體對象。我們關(guān)注兩個對象:Customer
和 Bike
。首先,在 src
目錄創(chuàng)建 dto
目錄;然后,創(chuàng)建兩個名為 Customer.java
和 Bike.java
的 Java 類對象文件。其結(jié)構(gòu)如下示:
package com.stephb.JavaMongo.dto;
import lombok.Getter;
import lombok.Setter;
import org.springframework.data.annotation.Id;
/**
*
* @author stephon
*/
@Getter @Setter
public class Customer {
private @Id String id;
private String emailAddress;
private String firstName;
private String lastName;
private String address;
}
Customer.Java
package com.stephb.JavaMongo.dto;
import lombok.Getter;
import lombok.Setter;
import org.springframework.data.annotation.Id;
/**
*
* @author stephon
*/
@Getter @Setter
public class Bike {
private @Id String id;
private String modelNumber;
private String color;
private String description;
@Override
public String toString() {
return "This bike model is " + this.modelNumber + " is the color " + this.color + " and is " + description;
}
}
Bike.java
如你所見,對象中使用 Lombok 注解來為定義的屬性/特性生成 getters/setters 方法。如果你不想對該類的所有特性都生成 getters/setters 方法,可以在屬性上專門定義這些注解。這兩個類會變成容器,里面攜帶有數(shù)據(jù),無論在何處想顯示信息都可以使用。
配置數(shù)據(jù)庫
我使用 Mongo Docker 容器來進(jìn)行此次測試。如果你的系統(tǒng)上已經(jīng)安裝了 MongoDB,則不必運行 Docker 實例。你也可以登錄其官網(wǎng),選擇系統(tǒng)信息,然后按照安裝說明來安裝 MongoDB。
安裝后,就可以使用命令行、GUI(例如 MongoDB Compass)或用于連接數(shù)據(jù)源的 IDE 驅(qū)動程序來與新的 MongoDB 服務(wù)器進(jìn)行交互。到目前為止,可以開始定義數(shù)據(jù)層了,用來拉取、轉(zhuǎn)換和持久化數(shù)據(jù)。需要設(shè)置數(shù)據(jù)庫訪問屬性,請導(dǎo)航到程序中的 applications.properties
文件,然后添加如下內(nèi)容:
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=BikeStore
定義數(shù)據(jù)訪問對象/數(shù)據(jù)訪問層
數(shù)據(jù)訪問層(DAL)中的數(shù)據(jù)訪問對象(DAO)定義了與數(shù)據(jù)庫中的數(shù)據(jù)的交互過程。令人驚嘆的就是在使用 spring-boot-starter
后,查詢數(shù)據(jù)庫的大部分工作已經(jīng)完成。
讓我們從 Customer
DAO 開始。在 src
下的新目錄 dao
中創(chuàng)建一個接口文件,然后再創(chuàng)建一個名為 CustomerRepository.java
的 Java 類文件,其內(nèi)容如下示:
package com.stephb.JavaMongo.dao;
import com.stephb.JavaMongo.dto.Customer;
import java.util.List;
import org.springframework.data.mongodb.repository.MongoRepository;
/**
*
* @author stephon
*/
public interface CustomerRepository extends MongoRepository<Customer, String>{
@Override
public List<Customer> findAll();
public List<Customer> findByFirstName(String firstName);
public List<Customer> findByLastName(String lastName);
}
這個類是一個接口,擴展或繼承于 MongoRepository
類,而 MongoRepository
類依賴于 DTO (Customer.java
)和一個字符串,它們用來實現(xiàn)自定義函數(shù)查詢功能。因為你已繼承自此類,所以你可以訪問許多方法函數(shù),這些函數(shù)允許持久化和查詢對象,而無需實現(xiàn)或引用自己定義的方法函數(shù)。例如,在實例化 CustomerRepository
對象后,你就可以直接使用 Save
函數(shù)。如果你需要擴展更多的功能,也可以重寫這些函數(shù)。我創(chuàng)建了一些自定義查詢來搜索我的集合,這些集合對象是我自定義的元素。
Bike
對象也有一個存儲源負(fù)責(zé)與數(shù)據(jù)庫交互。與 CustomerRepository
的實現(xiàn)非常類似。其實現(xiàn)如下所示:
package com.stephb.JavaMongo.dao;
import com.stephb.JavaMongo.dto.Bike;
import java.util.List;
import org.springframework.data.mongodb.repository.MongoRepository;
/**
*
* @author stephon
*/
public interface BikeRepository extends MongoRepository<Bike,String>{
public Bike findByModelNumber(String modelNumber);
@Override
public List<Bike> findAll();
public List<Bike> findByColor(String color);
}
運行程序
現(xiàn)在,你已經(jīng)有了一種結(jié)構(gòu)化數(shù)據(jù)的方式,可以對數(shù)據(jù)進(jìn)行提取、轉(zhuǎn)換和持久化,然后運行這個程序。
找到 Application.java
文件(有可能不是此名稱,具體取決于你的應(yīng)用程序名稱,但都會包含有 “application” )。在定義此類的地方,在后面加上 implements CommandLineRunner
。這將允許你實現(xiàn) run
方法來創(chuàng)建命令行應(yīng)用程序。重寫 CommandLineRunner
接口提供的 run
方法,并包含如下內(nèi)容用來測試 BikeRepository
:
package com.stephb.JavaMongo;
import com.stephb.JavaMongo.dao.BikeRepository;
import com.stephb.JavaMongo.dao.CustomerRepository;
import com.stephb.JavaMongo.dto.Bike;
import java.util.Scanner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class JavaMongoApplication implements CommandLineRunner {
@Autowired
private BikeRepository bikeRepo;
private CustomerRepository custRepo;
public static void main(String[] args) {
SpringApplication.run(JavaMongoApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
Scanner scan = new Scanner(System.in);
String response = "";
boolean running = true;
while(running){
System.out.println("What would you like to create? \n C: The Customer \n B: Bike? \n X:Close");
response = scan.nextLine();
if ("B".equals(response.toUpperCase())) {
String[] bikeInformation = new String[3];
System.out.println("Enter the information for the Bike");
System.out.println("Model Number");
bikeInformation[0] = scan.nextLine();
System.out.println("Color");
bikeInformation[1] = scan.nextLine();
System.out.println("Description");
bikeInformation[2] = scan.nextLine();
Bike bike = new Bike();
bike.setModelNumber(bikeInformation[0]);
bike.setColor(bikeInformation[1]);
bike.setDescription(bikeInformation[2]);
bike = bikeRepo.save(bike);
System.out.println(bike.toString());
} else if ("X".equals(response.toUpperCase())) {
System.out.println("Bye");
running = false;
} else {
System.out.println("Sorry nothing else works right now!");
}
}
}
}
其中的 @Autowired
注解會自動依賴注入 BikeRepository
和 CustomerRepository
Bean。我們將使用這些類來從數(shù)據(jù)庫持久化和采集數(shù)據(jù)。
已經(jīng)好了。你已經(jīng)創(chuàng)建了一個命令行應(yīng)用程序。該應(yīng)用程序連接到數(shù)據(jù)庫,并且能夠以最少的代碼執(zhí)行 CRUD 操作
結(jié)論
從諸如對象和類之類的編程語言概念轉(zhuǎn)換為用于在數(shù)據(jù)庫中存儲、檢索或更改數(shù)據(jù)的調(diào)用對于構(gòu)建應(yīng)用程序至關(guān)重要。Java 持久化 API(JPA)正是為 Java 開發(fā)人員解決這一難題的重要工具。你正在使用 Java 操縱哪些數(shù)據(jù)庫呢?請在評論中分享。