jOOQ - DML 语句

jOOQ 支持各种 SQL 语句。这些 SQL 语句是从 DSLContext 对象创建的。它将根据查询类型返回结果。在本教程中,我们将学习 DML 语句以及如何在 jOOQ 中创建和执行它们。

什么是 DML?

DML 代表数据操作语言。如果您熟悉 SQL,您可能已经知道它的命令。DML 是一组 SQL 命令,允许您修改给定数据库中的数据。最常见的 DML 操作是:

  • SELECT:此操作将从指定的表中检索数据。

  • INSERT:它将在给定的表中插入新数据。

  • UPDATE:它用于修改表中的现有数据。

  • DELETE:它从指定的表中删除数据。

用于创建 DML 语句的 jOOQ 方法

在 jOOQ 中,DSL 类提供了一组方法来创建 DML 语句,这些语句列在下表中 −

S.No 方法& 说明
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 Name Job Title
1 Aman Technical Writer
4 Vivek Developer

要使用 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() {
       // 使用 DataSource 创建 DSLContext
       DSLContext create = DSL.using(dataSource, SQLDialect.MYSQL);

       // 手动定义表和列
       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();
	   // 从员工表中获取值
       Result<Record3<Integer, String, String>> result = create.select(id, name, jobTitle)
          .from(employee)
          .fetch();

       // 打印结果
       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