一灰灰blog 一灰灰blog
首页
  • InfluxDB
  • MongoDB
  • MySql
  • 基础系列
  • DB系列
  • 搜索系列
  • MQ系列
  • WEB系列
  • 中间件
  • 运维
  • SpringSecurity
  • SpringCloud
  • QuickAlarm
  • QuickCrawer
  • QuickFix
  • QuickMedia
  • QuickSpi
  • QuickTask
  • 高可用
  • 分类
  • 标签
  • 归档
  • 收藏
  • 关于
GitHub (opens new window)

一灰灰blog

资深搬运工
首页
  • InfluxDB
  • MongoDB
  • MySql
  • 基础系列
  • DB系列
  • 搜索系列
  • MQ系列
  • WEB系列
  • 中间件
  • 运维
  • SpringSecurity
  • SpringCloud
  • QuickAlarm
  • QuickCrawer
  • QuickFix
  • QuickMedia
  • QuickSpi
  • QuickTask
  • 高可用
  • 分类
  • 标签
  • 归档
  • 收藏
  • 关于
GitHub (opens new window)
  • 基础系列

  • DB系列

    • H2Database

    • JPA

    • JdbcTemplate

    • Jooq

      • 【DB系列】Jooq初体验
      • 【DB系列】Jooq代码自动生成
      • 【DB系列】Jooq之新增记录使用姿势
      • 【DB系列】Jooq之记录更新与删除
        • I. 项目搭建
          • 1. 项目依赖
          • 2. 数据库初始化
          • 3. 配置文件
          • 4. 数据准备
        • II. 记录更新
          • 1. 类sql方式更新
          • 2. 更新计算
          • 3. UpdateQuery更新
          • 4. Entity更新
          • 5. executeUpdate更新
          • 6. 批量更新
        • III. 记录删除
          • 1. 类sql写法
          • 2. Entity删除
          • 3. dsl.executeDelete
          • 4. deleteQuery
        • II. 其他
          • 0. 项目
          • 1. 一灰灰Blog
      • 【DB系列】Jooq批量写入采坑记录
      • 【DB系列】Jooq之记录查询基础篇
      • 【DB系列】Jooq之聚合查询
      • 【DB系列】Jooq之常用函数使用姿势
      • 【DB系列】Jooq之多表联合查询
      • 【DB系列】Jooq之事务
    • Mybatis

    • 事务

    • MongoDB

    • Redis

    • 实例

  • 搜索系列

  • MQ系列

  • WEB系列

  • 中间件

  • 运维

  • SpringSecurity

  • SpringCloud

  • Spring系列
  • DB系列
  • Jooq
一灰灰
2020-09-30

【DB系列】Jooq之记录更新与删除

前面一篇介绍了Jooq的记录新增,除了利用自动生成的代码来保存数据之外,也可以借助DSL.table() + DSL.field()来实现类sql的写法;本文将介绍curd中的删除和更新的用法

# I. 项目搭建

本项目借助SpringBoot 2.2.1.RELEASE + maven 3.5.3 + IDEA进行开发

# 1. 项目依赖

关于如何创建一个SpringBoot的项目工程,不再本文的描述范围内,如有兴趣可以到文末的个人站点获取

在这个示例工程中,我们的选用h2dabase作为数据库(方便有兴趣的小伙伴直接获取工程源码之后,直接测试体验),因此对应的pom核心依赖如下

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jooq</artifactId>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
    </dependency>
</dependencies>
1
2
3
4
5
6
7
8
9
10
11
12
13
14

# 2. 数据库初始化

我们借助jooq-codegen-maven插件来自动生成数据库相关的代码,对这一段逻辑感兴趣的小伙伴可以参考博文:【DB系列】Jooq代码自动生成 (opens new window)

后文中使用的表结构如下

DROP TABLE IF EXISTS poet;

CREATE TABLE poet (
  `id` int NOT NULL,
  `name` varchar(20) NOT NULL default '',
  CONSTRAINT pk_t_poet PRIMARY KEY (ID)
);

DROP TABLE IF EXISTS poetry;
CREATE TABLE poetry (
  `id` int NOT NULL,
  `poet_id` int NOT NULL default '0',
  `title` varchar(128) not null default '',
  `content` varchar(128) not null default '',
  CONSTRAINT pk_t_poetry PRIMARY KEY (ID)
);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

# 3. 配置文件

h2database的连接配置如 application.properties

#Database Configuration
spring.datasource.url=jdbc:h2:~/h2-jooq-poet
spring.datasource.username=test
spring.datasource.password=
spring.datasource.driverClassName=org.h2.Driver


#jOOQ Configuration
spring.jooq.sql-dialect=H2


spring.datasource.initialization-mode=never
spring.datasource.continueOnError=true


##h2 web console设置
spring.datasource.platform=h2
#进行该配置后,h2 web consloe就可以在远程访问了。否则只能在本机访问。
spring.h2.console.settings.web-allow-others=true
#进行该配置,你就可以通过YOUR_URL/h2访问h2 web consloe
spring.h2.console.path=/h2
#进行该配置,程序开启时就会启动h2 web consloe
spring.h2.console.enabled=true
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

# 4. 数据准备

后文中的测试数据,主要借助的是前面一篇新增的记录,db中记录如下

# II. 记录更新

# 1. 类sql方式更新

下面这种链式写法和sql极为相似

private static final PoetTB table = PoetTB.POET;

@Autowired
private DSLContext dsl;

private boolean updateName(int id, String name) {
    // ==> update poet set `name`=xx where id=xxx
    return dsl.update(table).set(table.NAME, name).where(table.ID.eq(id)).execute() > 0;
}
1
2
3
4
5
6
7
8
9

# 2. 更新计算

上面的更新方式属于最基本的直接设置,某些场景下我们可能需要借助之前的column value,如下concat方法的更新方式,注意这个方法有Field提供

private boolean updateName2(int id, String name) {
    // concat 修改
    // 等同于 ==> update poet set `name`=concat(`name`, xxx) where id=xxx
    return dsl.update(table).set(table.NAME, table.NAME.concat(name)).where(table.ID.eq(id)).execute() > 0;
}
1
2
3
4
5

# 3. UpdateQuery更新

除了上面的链式更新方式,还可以借助UpdateQuery来处理

private boolean updateName3(int id, String name) {
    // update query方式
    UpdateQuery updateQuery = dsl.updateQuery(table);
    // 这个表示需要更新的value
    updateQuery.addValue(table.NAME, name);
    // 这个表示where条件
    updateQuery.addConditions(table.ID.eq(id));
    // 最终通过execute执行更新操作
    return updateQuery.execute() > 0;
}
1
2
3
4
5
6
7
8
9
10

# 4. Entity更新

直接借助代码自动生成的Record类,本文中对应的是PO结尾的类

/**
 * 使用Entity进行更新
 *
 * @param id
 * @param name
 * @return
 */
private boolean updateName4(int id, String name) {
    // 请注意po对象由dsl生成,不能直接new一个对象
    PoetPO poetPO = dsl.newRecord(table);
    poetPO.setId(id);
    poetPO.setName(name);
    return poetPO.update() > 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

# 5. executeUpdate更新

同样是借助Record类,与上面的区别在于这个实体类直接new出来,借助dsl.executeUpdate执行更新

private boolean updateName5(int id, String name) {
    PoetPO po = new PoetPO();
    po.setName(name);
    return dsl.executeUpdate(po, table.ID.eq(id)) > 0;
}
1
2
3
4
5

# 6. 批量更新

请注意这里说的批量更新不是指一条sql更新多条record记录,更像是多个更新sql的一次提交执行

下面主要是借助dsl.batchUpdate来实现

/**
 * 批量更新
 *
 * @param list
 * @return
 */
private boolean batchUpdate(List<PoetBO> list) {
    List<PoetPO> poList = list.stream().map(this::bo2po).collect(Collectors.toList());
    int[] ans = dsl.batchUpdate(poList).execute();
    System.out.println(JSONObject.toJSONString(ans));
    return true;
}

private PoetPO bo2po(PoetBO bo) {
    PoetPO po = dsl.newRecord(table);
    po.setId(bo.getId());
    po.setName(bo.getName());
    return po;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

# III. 记录删除

在实际的业务开发中,一般不建议直接删除记录,最好是通过一个column来标记逻辑删除,当然我们下面介绍的依然是物理删除...

# 1. 类sql写法

/**
 * 指定主键删除
 *
 * @param id
 * @return
 */
private boolean remove(int id) {
    return dsl.delete(table).where(table.ID.eq(id)).execute() > 0;
}
1
2
3
4
5
6
7
8
9

# 2. Entity删除

直接借助自动生成的实体类来执行删除

private boolean remove2(int id) {
    PoetPO po = dsl.newRecord(table);
    po.setId(id);
    return po.delete() > 0;
}
1
2
3
4
5

# 3. dsl.executeDelete

借助dsl.executeDelete来删除实体类,与上面的区别在于这个实体类是直接new出来的

private boolean remove3(int id) {
    PoetPO po = new PoetPO();
    po.setId(id);
    return dsl.executeDelete(po) > 0;
}
1
2
3
4
5

# 4. deleteQuery

private boolean remove4(int id) {
    DeleteQuery query = dsl.deleteQuery(table);
    query.addConditions(table.ID.ge(id));
    return query.execute() > 0;
}
1
2
3
4
5

# II. 其他

# 0. 项目

系列博文

  • 【SpringBoot DB系列】Jooq之新增记录使用姿势 (opens new window)
  • 【SpringBoot DB系列】Jooq代码自动生成 (opens new window)
  • 【SpringBoot DB系列】Jooq初体验 (opens new window)

项目源码

  • 工程:https://github.com/liuyueyi/spring-boot-demo (opens new window)
  • 项目源码: https://github.com/liuyueyi/spring-boot-demo/tree/master/spring-boot/108-jooq-curd (opens new window)

# 1. 一灰灰Blog

尽信书则不如,以上内容,纯属一家之言,因个人能力有限,难免有疏漏和错误之处,如发现bug或者有更好的建议,欢迎批评指正,不吝感激

下面一灰灰的个人博客,记录所有学习和工作中的博文,欢迎大家前去逛逛

  • 一灰灰Blog个人博客 https://blog.hhui.top (opens new window)
  • 一灰灰Blog-Spring专题博客 http://spring.hhui.top (opens new window)

一灰灰blog

编辑 (opens new window)
#Jooq
上次更新: 2021/10/15, 19:56:22
【DB系列】Jooq之新增记录使用姿势
【DB系列】Jooq批量写入采坑记录

← 【DB系列】Jooq之新增记录使用姿势 【DB系列】Jooq批量写入采坑记录→

最近更新
01
【基础系列】基于maven多环境配置
04-25
02
【WEB系列】内嵌Tomcat配置Accesslog日志文件生成位置源码探索
04-24
03
【搜索系列】ES查询常用实例演示
04-18
更多文章>
Theme by Vdoing | Copyright © 2017-2022 一灰灰Blog
MIT License | 鄂ICP备18017282号 |
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
×