当前位置: 首页 > news >正文

springboot学习-spring-boot-data-jdbc分页/排序/多表查询的例子

上次使用的是JdbcTemplate实现的,是比较老的方式,重新用spring boot data jdbc和jdbc client 实现一遍。也比较一下这几种的编码差异。数据库方面JAVA给了太多选择,反而不好选了。

上次就试图直接用:

public interface UserRepository extends CrudRepository<User, Long> {@Query("SELECT u.username, p.address, p.phoneNumber " +"FROM users u JOIN profiles p ON u.profileId = p.id " +"ORDER BY p.id")Page<UserProfileDTO> findUsersWithProfiles(Pageable pageable);

直接就报错了,才改为了jdbcTemplate.

这次改了:

@Repository
public interface AuthorBookRepository extends CrudRepository<Author, Integer> {@Query("SELECT a.id AS author_id, a.name AS author_name, b.id AS book_id, b.title AS book_title " +"FROM Author a JOIN Book b ON a.id = b.author_id " +"ORDER BY a.id " +"LIMIT :limit OFFSET :offset")List<AuthorBook> findAllAuthorsWithBooks(int limit, int offset);
}

注意有个坑:SQL语法错误引起的,特别是在ORDER BYLIMIT子句中使用了占位符?。在SQL中,ORDER BY子句不能使用占位符来指定列名和排序方向。我们需要在代码中动态构建SQL语句来解决这个问题。--这里就直接写,不用占位符了。

service:

@Service
public class AuthorBookService {@Autowiredprivate AuthorBookRepository authorBookRepository;public List<AuthorBook> getAuthorsWithBooks(int page, int size) {int offset = page * size;return authorBookRepository.findAllAuthorsWithBooks(size, offset);}
}

controller

@GetMapping("/authors-books")public ModelAndView getAuWithBooks(@RequestParam(defaultValue = "1") int page,@RequestParam(defaultValue = "3") int size) {List<AuthorBook> authorsWithBooks = authorBookService.getAuthorsWithBooks(page, size);ModelAndView modelAndView = new ModelAndView("authors-books");modelAndView.addObject("authorBooks", authorsWithBooks);modelAndView.addObject("page", page);modelAndView.addObject("size", size);return modelAndView;}

验证通过,这个方法很好。

JDBC Client应该也行,没有试过。

package dev.zzz.repository;import dev.zzz.model.dto.AuthorBook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.jdbc.core.simple.JdbcClient;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class AuthorBookService {@Autowiredprivate AuthorBookRepository authorBookRepository;private final JdbcClient jdbcClient;public AuthorBookService(JdbcClient jdbcClient) {this.jdbcClient = jdbcClient;}public List<AuthorBook> getAuthorsWithBooks(int page, int size) {int offset = page * size;return authorBookRepository.findAllAuthorsWithBooks(size, offset);}public Page<AuthorBook> getAuthors(Pageable pageable) {int limit = pageable.getPageSize();long offset = pageable.getOffset();String baseSql="SELECT a.id AS author_id, a.name AS author_name, b.id AS book_id, b.title AS book_title FROM Author a JOIN Book b ON a.id = b.author_id ";String orderByClause = " ORDER BY a.id";String sql = baseSql + orderByClause + " LIMIT " + limit + " OFFSET " + offset;List<AuthorBook> authorBooks = jdbcClient.sql(sql).query(AuthorBook.class).list();String countQuery = "SELECT count(*) FROM  Author a JOIN Book b ON a.id = b.author_id";Long total = jdbcClient.sql(countQuery).query(Long.class).single();return new PageImpl<>(authorBooks, pageable, total);}}

相应的controller:

package dev.zzz.controller;import dev.zzz.model.dto.AuthorBook;
import dev.zzz.repository.AuthorBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;@Controller
public class AuthorController {@Autowiredprivate AuthorBookService authorBookService;@GetMapping("/authors-books2")public String getAuthos(@RequestParam(defaultValue = "0") int page,@RequestParam(defaultValue = "10") int size,Model model) {Pageable pageable = PageRequest.of(page, size);Page<AuthorBook> authorsWithBooks = authorBookService.getAuthors(pageable);model.addAttribute("authorBooks",authorsWithBooks.getContent());model.addAttribute("page", page);model.addAttribute("size", size);return "authors-books";}}


http://www.mrgr.cn/news/78677.html

相关文章:

  • CSS——1.优缺点
  • Go语言的 的并发编程(Concurrency)核心知识
  • 数据在内存中的存储【C语言版】
  • 分布式系统架构6:链路追踪
  • Elasticsearch:利用 AutoOps 检测长时间运行的搜索查询
  • 解决npm报错:sill idealTree buildDeps
  • 点云处理中obb算法原理和法向量求解方法
  • PVE中VLAN的设置要点
  • 第十六届蓝桥杯模拟赛第二期题解—Java
  • 11 设计模式之代理模式(送资料案例)
  • 「Mac畅玩鸿蒙与硬件38」UI互动应用篇15 - 猜数字增强版
  • 2024前端面试经验分享
  • 【大模型实战篇】基于大模型GLM的Function Call实践
  • SQL进阶技巧:如何寻找同一批用户 | 断点分组应用【最新面试题】
  • 12 设计模式之工厂方法模式
  • 二叉搜索树讲解
  • 【C++笔记】位图和布隆过滤器
  • 开发一套ERP 第九弹 前后整合
  • burp2
  • 五:OpenStack环境准备-compute node
  • 芯片测试-射频中的单位
  • 腾讯阅文集团Android面试题及参考答案
  • AI - 如何构建一个大模型中的Tool
  • ⽂件操作详解
  • Day1 生信新手笔记
  • 如何估算自然对流传热系数