在我們平時(shí)的工作中,查詢(xún)列表在我們的系統(tǒng)中基本隨處可見(jiàn),那么我們?nèi)绾问褂胘pa進(jìn)行多條件查詢(xún)以及查詢(xún)列表分頁(yè)呢?下面我將介紹兩種多條件查詢(xún)方式。
創(chuàng)新互聯(lián)專(zhuān)注于企業(yè)全網(wǎng)營(yíng)銷(xiāo)推廣、網(wǎng)站重做改版、陽(yáng)曲網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5場(chǎng)景定制、商城網(wǎng)站開(kāi)發(fā)、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性?xún)r(jià)比高,為陽(yáng)曲等各大城市提供網(wǎng)站開(kāi)發(fā)制作服務(wù)。
1、引入起步依賴(lài)
org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-thymeleaf org.springframework.boot spring-boot-starter-data-jpa
2、對(duì)thymeleaf和jpa進(jìn)行配置
打開(kāi)application.yml,添加以下參數(shù),以下配置在之前的文章中介紹過(guò),此處不做過(guò)多說(shuō)明
spring: thymeleaf: cache: true check-template-location: true content-type: text/html enabled: true encoding: utf-8 mode: HTML5 prefix: classpath:/templates/ suffix: .html excluded-view-names: template-resolver-order: datasource: driver-class-name: com.MySQL.jdbc.Driver url: jdbc:mysql://localhost:3306/restful?useUnicode=true&characterEncoding=UTF-8&useSSL=false username: root password: root initialize: true init-db: true jpa: database: mysql show-sql: true hibernate: ddl-auto: update naming: strategy: org.hibernate.cfg.ImprovedNamingStrategy
3、編寫(xiě)實(shí)體Bean
@Entity @Table(name="book") public class Book { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id", updatable = false) private Long id; @Column(nullable = false,name = "name") private String name; @Column(nullable = false,name = "isbn") private String isbn; @Column(nullable = false,name = "author") private String author; public Book (String name,String isbn,String author){ this.name = name; this.isbn = isbn; this.author = author; } public Book(){ } //此處省去get、set方法 } public class BookQuery { private String name; private String isbn; private String author; //此處省去get、set方法 }
4、編寫(xiě)Repository接口
@Repository("bookRepository") public interface BookRepository extends JpaRepository,JpaSpecificationExecutor { }
此處繼承了兩個(gè)接口,后續(xù)會(huì)介紹為何會(huì)繼承這兩個(gè)接口
5、抽象service層
首先抽象出接口
public interface BookQueryService { PagefindBookNoCriteria(Integer page,Integer size); Page findBookCriteria(Integer page,Integer size,BookQuery bookQuery); }
實(shí)現(xiàn)接口
@Service(value="https://my.oschina.net/wangxincj/blog/bookQueryService") public class BookQueryServiceImpl implements BookQueryService { @Resource BookRepository bookRepository; @Override public PagefindBookNoCriteria(Integer page,Integer size) { Pageable pageable = new PageRequest(page, size, Sort.Direction.ASC, "id"); return bookRepository.findAll(pageable); } @Override public Page findBookCriteria(Integer page, Integer size, final BookQuery bookQuery) { Pageable pageable = new PageRequest(page, size, Sort.Direction.ASC, "id"); Page bookPage = bookRepository.findAll(new Specification (){ @Override public Predicate toPredicate(Root root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) { List list = new ArrayList (); if(null!=bookQuery.getName()&&!"".equals(bookQuery.getName())){ list.add(criteriaBuilder.equal(root.get("name").as(String.class), bookQuery.getName())); } if(null!=bookQuery.getIsbn()&&!"".equals(bookQuery.getIsbn())){ list.add(criteriaBuilder.equal(root.get("isbn").as(String.class), bookQuery.getIsbn())); } if(null!=bookQuery.getAuthor()&&!"".equals(bookQuery.getAuthor())){ list.add(criteriaBuilder.equal(root.get("author").as(String.class), bookQuery.getAuthor())); } Predicate[] p = new Predicate[list.size()]; return criteriaBuilder.and(list.toArray(p)); } },pageable); return bookPage; } }
此處我定義了兩個(gè)接口,findBookNoCriteria是不帶查詢(xún)條件的,findBookCriteria是帶查詢(xún)條件的。在此處介紹一下上面提到的自定義Repository繼承的兩個(gè)接口,如果你的查詢(xún)列表是沒(méi)有查詢(xún)條件,只是列表展示和分頁(yè),只需繼承JpaRepository接口即可,但是如果你的查詢(xún)列表是帶有多個(gè)查詢(xún)條件的話(huà)則需要繼承JpaSpecificationExecutor接口,這個(gè)接口里面定義的多條件查詢(xún)的方法。當(dāng)然不管繼承哪個(gè)接口,當(dāng)你做分頁(yè)查詢(xún)時(shí),都是需要調(diào)用findAll方法的,這個(gè)方法是jap定義好的分頁(yè)查詢(xún)方法。
findBookCriteria方法也可以使用以下方法實(shí)現(xiàn),大家可以自行選擇
@Override public PagefindBookCriteria(Integer page, Integer size, final BookQuery bookQuery) { Pageable pageable = new PageRequest(page, size, Sort.Direction.ASC, "id"); Page bookPage = bookRepository.findAll(new Specification (){ @Override public Predicate toPredicate(Root root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) { Predicate p1 = criteriaBuilder.equal(root.get("name").as(String.class), bookQuery.getName()); Predicate p2 = criteriaBuilder.equal(root.get("isbn").as(String.class), bookQuery.getIsbn()); Predicate p3 = criteriaBuilder.equal(root.get("author").as(String.class), bookQuery.getAuthor()); query.where(criteriaBuilder.and(p1,p2,p3)); return query.getRestriction(); } },pageable); return bookPage; }
6、編寫(xiě)Controller
針對(duì)有查詢(xún)條件和無(wú)查詢(xún)條件,我們分別編寫(xiě)一個(gè)Controller,默認(rèn)每頁(yè)顯示5條,如下
@Controller @RequestMapping(value = "https://my.oschina.net/queryBook") public class BookController { @Autowired BookQueryService bookQueryService; @RequestMapping("/findBookNoQuery") public String findBookNoQuery(ModelMap modelMap,@RequestParam(value = "https://my.oschina.net/wangxincj/blog/page", defaultValue = "https://my.oschina.net/wangxincj/blog/0") Integer page, @RequestParam(value = "https://my.oschina.net/wangxincj/blog/size", defaultValue = "https://my.oschina.net/wangxincj/blog/5") Integer size){ Pagedatas = bookQueryService.findBookNoCriteria(page, size); modelMap.addAttribute("datas", datas); return "index1"; } @RequestMapping(value = "https://my.oschina.net/findBookQuery",method = {RequestMethod.GET,RequestMethod.POST}) public String findBookQuery(ModelMap modelMap, @RequestParam(value = "https://my.oschina.net/wangxincj/blog/page", defaultValue = "https://my.oschina.net/wangxincj/blog/0") Integer page, @RequestParam(value = "https://my.oschina.net/wangxincj/blog/size", defaultValue = "https://my.oschina.net/wangxincj/blog/5") Integer size, BookQuery bookQuery){ Page datas = bookQueryService.findBookCriteria(page, size,bookQuery); modelMap.addAttribute("datas", datas); return "index2"; } }
7、編寫(xiě)頁(yè)面
首先我們編寫(xiě)一個(gè)通用的分頁(yè)頁(yè)面,新建一個(gè)叫page.html的頁(yè)面
針對(duì)無(wú)查詢(xún)條件的接口,創(chuàng)建一個(gè)名為index1.html的頁(yè)面并引入之前寫(xiě)好的分頁(yè)頁(yè)面,如下
Title
ID | name | isbn | author |
---|---|---|---|
${obj.id} | ${obj.name} | ${obj.isbn} | ${obj.author} |
針對(duì)有查詢(xún)條件的接口,創(chuàng)建一個(gè)名為index2.html的頁(yè)面并引入之前寫(xiě)好的分頁(yè)頁(yè)面,如下
Title
ID | name | isbn | author |
---|---|---|---|
${obj.id} | ${obj.name} | ${obj.isbn} | ${obj.author} |
ok!代碼都已經(jīng)完成,我們將項(xiàng)目啟動(dòng)起來(lái),看一下效果。大家可以往數(shù)據(jù)庫(kù)中批量插入一些數(shù)據(jù),訪問(wèn)
http://localhost:8080/queryBook/findBookNoQuery,顯示如下頁(yè)面
訪問(wèn)http://localhost:8080/queryBook/findBookQuery,顯示頁(yè)面如下,可以輸入查詢(xún)條件進(jìn)行帶條件的分頁(yè)查詢(xún):
總結(jié)
以上所述是小編給大家介紹的在Spring Boot中使用Spring-data-jpa實(shí)現(xiàn)分頁(yè)查詢(xún),希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)創(chuàng)新互聯(lián)網(wǎng)站的支持!