方法一

使用原生sql查询 或者 为方法名增加 Pageable参数

import  org.springframework.data.domain.Pageable;

public interface BookQueryRepository extends Repository<Book, Long> {

    //原生的sql语句,要使用数据表名
@Query(value = "select * from tb_book b where b.name=?1", nativeQuery = true)
List<Book> findByName(String name); //基础查询 重要说明 JPA规范中的定义JPQL是不能使用星号(*)来查询的
@Query(value = "select name,author,price from Book b where b.price>?1 and b.price<?2")
List<Book> findByPriceRange(long price1, long price2);

   @Query("select t from Book t")
List<Book> findAllBook1();
   @Query("select * from book " , nativeQuery = true)
List<Book> findAllBook2();
//Like表达式
@Query(value = "select name,author,price from Book b where b.name like %:name%")
List<Book> findByNameMatch(@Param("name") String name); //使用@Param注解注入参数
@Query(value = "select name,author,price from Book b where b.name = :name AND b.author=:author AND b.price=:price")
List<Book> findByNamedParam(@Param("name") String name, @Param("author") String author,
@Param("price") long price); //分页查询 原生sql语句
@Query(value = "SELECT * FROM tb_book WHERE name = ?1",
countQuery = "SELECT count(*) FROM tb_book WHERE name = ?1",
nativeQuery = true)
Page<Book> findByName(String name, Pageable pageable); //分页查询
@Query("select name,author,price from Book b where b.name=?")
public List<Book> findByNamePaged(String certNum,Pageable pageable);
}

业务层调用

public class BookService {

    @Autowired
private BookQueryRepository bookQueryDao;
public Page<Book> getBook(int pageNumber,int pageSize){
PageRequest request = this.buildPageRequest(pageNumber,pageSize);
Page<Book> result= bookQueryDao.findByName("123123",request);
return result;
}   //构建PageRequest
private PageRequest buildPageRequest(int pageNumber, int pagzSize) {
return new PageRequest(pageNumber - 1, pagzSize, null);
}
}

方法二

直接使用 PagingAndSortingRepository

import org.springframework.data.repository.PagingAndSortingRepository;

public interface BookQueryRepository extends PagingAndSortingRepository<Book, String> {

}

业务层调用

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest; public class BookService { @Autowired
private BookQueryRepository bookQueryDao;
public Page<Book> getBook(int pageNumber,int pageSize){
PageRequest request = this.buildPageRequest(pageNumber,pageSize);
Page<Book> result= bookQueryDao.findAll(request);
return result;
}   //构建PageRequest
private PageRequest buildPageRequest(int pageNumber, int pagzSize) {
return new PageRequest(pageNumber - 1, pagzSize, null);
}
}

SpringBoot 使用JPA+MySQL+Thymeleaf 总结 二的更多相关文章

  1. SpringBoot 使用JPA+MySQL+Thymeleaf 总结 一

    SpringBoot 使用JPA+MySQL+Thymeleaf 总结 一 SpringBoot 使用JPA+MySQL+Thymeleaf 总结 二 pom引用 <?xml version=& ...

  2. Springboot+Atomikos+Jpa+Mysql实现JTA分布式事务

    1 前言 之前整理了一个spring+jotm实现的分布式事务实现,但是听说spring3.X后不再支持jotm了,jotm也有好几年没更新了,所以今天整理springboot+Atomikos+jp ...

  3. springboot+jpa+mysql+redis+swagger整合步骤

    springboot+jpa+MySQL+swagger框架搭建好之上再整合redis: 在电脑上先安装redis: 一.在pom.xml中引入redis 二.在application.yml里配置r ...

  4. springboot+jpa+mysql+swagger整合

    Springboot+jpa+MySQL+swagger整合 创建一个springboot web项目 <dependencies> <dependency>      < ...

  5. 二、springboot使用jpa

    花了几天时间,好好看了看springboot的jpa部分,总结了常用的形式. 1.通过STS工具添加jpa的依赖项 要连mysql,测试的时候需要web,顺便添加了lombok不写set和get方法了 ...

  6. 【SpringBoot】SpringBoot/MyBatis/MySql/thymeleaf/Log4j整合工程

    工程下载地址:https://files.cnblogs.com/files/xiandedanteng/MMSpringWeb20191027-1.rar 工程目录结构如图: 1.创建工程 有些网文 ...

  7. SpringBoot入门系列:第五篇 JPA mysql(转)

    一,准备工作,建立spring-boot-sample-mysql工程1.http://start.spring.io/ A.Artifact中输入spring-boot-sample-mysql B ...

  8. spring-boot jpa mysql emoji utfmb4 异常处理

    spring-boot jpa mysql utf8mb4 emoji 写入失败 mysql database,table,column 默认为utf8mb4 Caused by: java.sql. ...

  9. IDEA SpringBoot+JPA+MySql+Redis+RabbitMQ 秒杀系统

    先放上github地址:spike-system,可以直接下载完整项目运行测试 SpringBoot+JPA+MySql+Redis+RabbitMQ 秒杀系统 技术栈:SpringBoot, MyS ...

随机推荐

  1. SSH开发环境整合

    第一步:Spring开发环境搭建 1.1: 添加配置文件和相应spring-3.2-core.Jar 核心包 配置文件 <?xml version="1.0" encodin ...

  2. Vue使用 weui picker 弹出框不消失

    前言 最近使用 weui 里面的 datepicker 组件的时候遇到了一个问题: 弹出来 选择年月日的框之后,直接点击导航上的“返回” 按钮,picker 选框不消失,也就是弹出框不消失 weui. ...

  3. rsync快速部署记录

    rsync快速部署记录 安装rsync和使用环境:客户端:10.192.30.59 fudao_db_cluster_002 (将本地文件备份到服务端)服务端:10.192.30.60 fudao_d ...

  4. jsp三种注释方法

    HTML注释(输出注释):指在客户端查看源代码时能看见注释.例如, <!-- this is an html comment.it will show up int the response. ...

  5. 【leetcode】Valid Parenthesis String

    题目: Given a string containing only three types of characters: '(', ')' and '*', write a function to ...

  6. C++调试的骚操作

    打LCT时突然发现的骚操作 举个栗子 正常调试下应该是这样的 然后用光标选中函数名时-- 可以发现函数被运行了一次(每选中一次都会运行) 然而当函数带了变量时就布星了

  7. linux-selinxu---性能 -8

    sed -ri.bk '/^SELINUX=/s/(SELINUX=)(.*)/\1disabled/' /etc/selinuconfig 修改并备份 脚本打开关闭 selinux if [[ &q ...

  8. git windows下换行符问题

    不同操系统下的换行符 CR回车 LF换行 Windows/Dos CRLF \r\n Linux/Unix LF \n MacOS CR \r 1.执行git config --get core.au ...

  9. ICPC — International Collegiate Programming Contest Asia Regional Contest, Yokohama, 2018–12–09 题解

    目录 注意!!此题解存在大量假算法,请各位巨佬明辨! Problem A Digits Are Not Just Characters 题面 题意 思路 代码 Problem B Arithmetic ...

  10. NOIP2018初赛划水记

    尽管初赛好像没什么好写的,但还是以此作为我的第一篇游记吧. 前夜 本来以为初赛水(去年分数线36来着),几周前做了一套普及组的卷子90多分感觉良好就没管了(不明白那些专门花时间看初赛书的人),结果Mr ...