SpringBoot分页查询
- 前期准备
- 依赖添加
- 统一json返回值类型
- springBoot配置文件
- springBoot启动类
- 核心代码
- 设置分页返回值的数据类型
- pojo层
- dao层
- service层
- controller层
前期准备
依赖添加
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>1.5.3.RELEASEversion>
<relativePath/>
parent>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
<repositories>
<repository>
<id>spring-snapshotsid>
<name>Spring Snapshotsname>
<url>https://repo.spring.io/snapshoturl>
<snapshots>
<enabled>trueenabled>
snapshots>
repository>
<repository>
<id>spring-milestonesid>
<name>Spring Milestonesname>
<url>https://repo.spring.io/milestoneurl>
<snapshots>
<enabled>falseenabled>
snapshots>
repository>
repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshotsid>
<name>Spring Snapshotsname>
<url>https://repo.spring.io/snapshoturl>
<snapshots>
<enabled>trueenabled>
snapshots>
pluginRepository>
<pluginRepository>
<id>spring-milestonesid>
<name>Spring Milestonesname>
<url>https://repo.spring.io/milestoneurl>
<snapshots>
<enabled>falseenabled>
snapshots>
pluginRepository>
pluginRepositories>
统一json返回值类型
- 设置json返回值类型
@Data
public class Result {
private int code;
private String message;
private boolean flag;
private Object data;
}
- 自定义状态码
public class StatusCode {
public static final int OK=20000;
public static final int ERROR =20001;
public static final int LOGINERROR =20002;
public static final int ACCESSERROR =20003;
public static final int REMOTEERROR =20004;
public static final int REPERROR =20005;
}
springBoot配置文件
server:
port: 9001
spring:
application:
name: tensquare-base
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://192.168.200.157:3306/tensquare_base?characterEncoding=utf-8
username: root
password: cqrjxk39
jpa:
database: mysql
show-sql: true
springBoot启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import util.IdWorker;
@SpringBootApplication
public class BaseApplication {
public static void main(String[] args) {
SpringApplication.run(BaseApplication.class);
}
@Bean
public IdWorker idWorker(){
return new IdWorker();
}
}
核心代码
设置分页返回值的数据类型
@Data
public class PageResult<T> {
private long totle;
private List<T> rows;
}
pojo层
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "tb_label")
@Data
public class Label {
@Id
private String id;
private String labelname;
private String state;
private Long count;
private String recommend;
private Long fans;
}
dao层
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
public interface LabelDao extends JpaRepository<Label,String>, JpaSpecificationExecutor<Label> {
}
service层
import com.tensquare.search.dao.ArticleDao;
import com.tensquare.search.pojo.Article;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional
public class ArticleService {
@Autowired
private ArticleDao articleDao;
public void addArticle(Article article){
articleDao.save(article);
}
public Page<Article> findByTitleOrContentLike(String title,String content,int page,int size){
PageRequest of = PageRequest.of(page - 1, size);
Page<Article> byTitleOrContentLike = articleDao.findByTitleOrContentLike(title, content, of);
return byTitleOrContentLike;
}
}
controller层
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
@RestController
@CrossOrigin
@RequestMapping("/article")
public class ArticleController {
@Autowired
private ArticleService articleService;
@RequestMapping(value = "/{key}/{page}/{size}",method =RequestMethod.GET)
public Result findByTitleOrContentLike(@PathVariable String key,@PathVariable int page,@PathVariable int size){
Page<Article> byTitleOrContentLike = articleService.findByTitleOrContentLike(key, key, page, size);
return new Result(true,StatusCode.OK,"查询成功",new PageResult<Article>(byTitleOrContentLike.getTotalElements(),byTitleOrContentLike.getContent()));
}
}