jOOQ - DDL 语句
什么是 DDL 语句?
DDL 代表数据定义语言。虽然 DML 语句用于查询给定数据库表中的数据,但 DDL 语句用于定义、修改或删除数据库对象,例如表、视图、索引和模式。最常见的 DDL 操作是
CREATE:它创建一个新的表、视图或索引。
ALTER:此操作修改现有表。
DROP:它用于删除数据库对象,例如表、视图或索引。
TRUNCATE:删除表中的所有行。
jOOQ 中的 DDL 语句
在 jOOQ 中,使用 DSL 类的以下方法创建 DDL 语句:
| 序号 | 方法和描述 |
|---|---|
| 1. | createTable(String name) 它开始创建具有指定名称的表。 |
| 2. | column(String name, DataType<?> type) 它用于在表中定义具有指定名称和数据类型的列。 |
| 3. | alterTable(String tableName) 开始修改现有表的过程。 |
| 4. | addColumn(String columnName, DataType<?> type) 此方法向现有表添加新列。 |
| 5. | dropColumn(String columnName) 此方法用于从现有表中删除列。 |
| 6. | renameColumn(String oldName, String newName) 重命名现有列。 |
| 7. | dropTable(String name) 从数据库中删除现有表。 |
| 8. | truncateTable(String name) 删除表中的所有行,而不会删除表本身。 |
jOOQ 中 DDL 语句示例
在本例中,我们将执行ALTER TABLE操作以将“ID”重命名为“Rank”。我们将使用以下表进行此操作:
| ID | 姓名 | 职位 |
|---|---|---|
| 1 | Aman | 技术作家 |
| 2 | Shriansh | 软件工程师 |
| 4 | Vivek | 开发人员 |
对于 ALTER TABLE 操作,在src -> main -> java -> com.example.demo内创建一个包。com.example.demo文件夹的名称将取决于项目的名称。在此包内创建一个 Java 类。
我们将包命名为service,Java 类命名为EmployeeService.java。您可以根据自己的选择命名。
在 jOOQ 中,我们使用alterTable()方法修改表结构,并使用renameColumn()方法重命名列。将以下代码片段复制并粘贴到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);
// Rename the column 'id' to 'rank'
create.alterTable("employee")
.renameColumn("id").to("rank")
.execute();
// Manually define the table and columns after renaming
Table<?> employee = DSL.table("employee");
Field<Integer> rank = DSL.field("rank", Integer.class);
Field<String> name = DSL.field("name", String.class);
Field<String> jobTitle = DSL.field("job_title", String.class);
// Fetch the values from the employee table
Result<Record3<Integer, String, String>> result = create.select(rank, name, jobTitle)
.from(employee)
.fetch();
// Print the results
for (Record record : result) {
Integer employeeRank = record.get(rank);
String employeeName = record.get(name);
String employeeJobTitle = record.get(jobTitle);
System.out.println("Rank: " + employeeRank + ", 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”列更改为“Rank”,如下所示:
Rank: 1, Name: Aman, Job Title: Technical Writer Rank: 2, Name: Shriansh, Job Title: Software Engineer Rank: 4, Name: Vivek, Job Title: Developer
广告