jOOQ - DML語句
jOOQ支援各種SQL語句。這些SQL語句由DSLContext物件建立。它將根據查詢型別返回結果。在本教程中,我們將學習DML語句以及如何在jOOQ中建立和執行它們。
什麼是DML?
DML代表資料操縱語言 (Data Manipulation Language)。如果您熟悉SQL,您可能已經瞭解它的命令。DML是一組SQL命令,允許您修改給定資料庫中的資料。最常見的DML操作是
SELECT:此操作將從指定的表中檢索資料。
INSERT:它將新資料插入到給定的表中。
UPDATE:用於修改表中現有資料。
DELETE:它從指定的表中刪除資料。
建立DML語句的jOOQ方法
在jOOQ中,DSL類提供了一組用於建立DML語句的方法,如下表所示:
| 序號 | 方法和描述 |
|---|---|
| 1. | insertInto(Table<Record> table) 此方法用於啟動對給定表的INSERT語句。 |
| 2. | set(Field<T> field, T value) 此方法用於在插入和更新記錄時設定欄位的值。 |
| 3. | values(Object... values) 它用於定義要插入表中的值。 |
| 4. | update(Table<Record> table) 此方法用於啟動對指定表的UPDATE語句。 |
| 5. | deleteFrom(Table<Record> table) 它用於啟動對指定表的DELETE語句。 |
| 6. | select(Field<?>... fields) 此方法用於使用指定的欄位建立SELECT查詢。 |
| 7. | from(Table<?>... tables) 此方法用於指定要從中選擇資料的表。 |
jOOQ中DML語句示例
在此示例中,我們將執行INSERT操作以新增新行。假設現有的表employee儲存以下記錄:
| ID | 姓名 | 職位 |
|---|---|---|
| 1 | Aman | 技術撰寫人 |
| 4 | Vivek | 開發者 |
要使用jOOQ插入新記錄,請在src -> main -> java -> com.example.demo內部建立一個包。com.example.demo資料夾的名稱將取決於您的專案名稱。在這個包內建立一個Java類。我們命名了包service和Java類EmployeeService.java。您可以隨意命名。
將以下程式碼片段複製並貼上到EmployeeService.java檔案中。(此處省略程式碼示例)
package com.example.demo.service;
import org.jooq.*;
import org.jooq.Record;
import org.jooq.impl.DSL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.sql.DataSource;
@Service
public class EmployeeService {
private final DataSource dataSource;
@Autowired
public EmployeeService(DataSource dataSource) {
this.dataSource = dataSource;
}
public void run() {
// Create a DSLContext using the DataSource
DSLContext create = DSL.using(dataSource, SQLDialect.MYSQL);
// Manually define the table and columns
Table<?> employee = DSL.table("employee");
Field<Integer> id = DSL.field("id", Integer.class);
Field<String> name = DSL.field("name", String.class);
Field<String> jobTitle = DSL.field("job_title", String.class);
// Insert a new employee record into the table
create.insertInto(employee, id, name, jobTitle)
.values(2, "Shriansh", "Software Engineer")
.execute();
// Fetch the values from the employee table
Result<Record3<Integer, String, String>> result = create.select(id, name, jobTitle)
.from(employee)
.fetch();
// Print the results
for (Record record : result) {
Integer employeeId = record.get(id);
String employeeName = record.get(name);
String employeeJobTitle = record.get(jobTitle);
System.out.println("ID: " + employeeId + ", Name: " + employeeName + ", Job Title: " + employeeJobTitle);
}
}
}
現在,導航到com.example.demo資料夾內的DemoApplication.java檔案。並編寫以下程式碼:(此處省略程式碼示例)
package com.example.demo;
import com.example.demo.service.EmployeeService;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication implements CommandLineRunner {
private final EmployeeService demoApplication;
public DemoApplication(EmployeeService demoApplication) {
this.demoApplication = demoApplication;
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
demoApplication.run();
}
}
執行此程式碼時,它將列印更新後的表:(此處省略程式碼示例)
ID: 1, Name: Aman, Job Title: Technical Writer ID: 2, Name: Shriansh, Job Title: Software Engineer ID: 4, Name: Vivek, Job Title: Developer