Skip to content
On this page

链式查询

simplest-jpa 中,内置了 queryChain 和 updateChain 用于对数据进行链式查询操作和链式数据操作(修改和删除)。

  • queryChain:链式查询
  • updateChain:链式更新

queryChain 示列

java
@Test
void selectQueryChain() {
    QCategory qCategory = QCategory.category;
    List<String> categoryList = categoryService.queryChain()
            .select(qCategory.title)
            .from(qCategory)
            .fetch();
    log.info("返回条数{}", categoryList.size());
}

条件查询

java
@Test
void selectQueryChainWhere() {
    QCategory qCategory = QCategory.category;
    List<String> categoryList=  categoryService.queryChain()
            .select(qCategory.title)
            .from(qCategory)
            .where(qCategory.id.eq(1l))
            .fetch();
        log.info("返回条数{}", categoryList.size());
}

分页查询

java
@Test
void selectQueryChainWhere() {
    QCategory qCategory = QCategory.category;
    List<String> categoryList = categoryService.queryChain()
            .select(qCategory.title)
            .from(qCategory)
            .where(qCategory.id.eq(1l))
            .limit(1)
            .fetch();
    log.info("返回条数{}", categoryList.size());
}

updateChain 示例

java
@Test
@Rollback(value = false)
@Transactional
void updateChain() {
    QCategory qCategory = QCategory.category;
    categoryService.updateChain(qCategory)
            .set(qCategory.title, "测试jpa")
            .where(qCategory.id.eq(6l)).execute();
}

queryChain 的方法

  • fetch() 获取多条数据 懒加载模式
  • fetchAll() 获取多条数据 忽略懒加载
  • fetchOne() 获取一条数据 多条会报错
  • fetchFirst() 查询第一条数据
  • fetchCount() 查询数据条数