SpringDataJPA的分頁
1.搭建springboot+springdatajpa+lombok的環境
1.1 項目結構
1.2 pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.stormkai</groupId>
<artifactId>element-crud</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>element-crud</name>
<description>element-crud</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
1.3 application.yml
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/csdn?characterEncoding=utf-8&useSSL=false&tinyIntlisBit=false&serverTimezone=GMT
username: root
password: root
jpa:
show-sql: true
hibernate:
ddl-auto: update
devtools:
restart:
enabled: true
additional-paths: src/main/java
server:
port: 8088
1.4 sql
CREATE TABLE IF NOT EXISTS `user_info` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`user_name` varchar(50) NOT NULL COMMENT '用戶名',
`password` varchar(32) NOT NULL COMMENT '密碼,加密存儲',
`phone` varchar(20) DEFAULT NULL COMMENT '註冊手機號',
`email` varchar(50) DEFAULT NULL COMMENT '註冊郵箱',
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '創建時間',
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改時間',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8 COMMENT='用戶表';
INSERT INTO `user_info` (`id`, `user_name`, `password`, `phone`, `email`, `create_time`, `update_time`) VALUES
(2, 'Kevin1', '123456', '13110477888', '[email protected]', '2020-01-08 21:15:27', '2020-04-29 00:13:26'),
(3, 'Lily', '123456', '13110477888', '[email protected]', '2020-01-08 21:16:17', '2020-01-08 21:16:17'),
(4, 'Rose', '123456', '13110477888', '[email protected]', '2020-01-08 21:16:34', '2020-01-08 21:16:34'),
(5, 'Vivian', '123456', '13110477888', '[email protected]', '2020-01-08 21:17:01', '2020-01-08 21:17:01'),
(6, 'Andrew', '123456', '13110477888', '[email protected]', '2020-01-08 21:17:23', '2020-01-08 21:17:23');
1.4 entity
User.java
@Data
@Entity
@Table(name="user_info")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String userName;
private String password;
private String phone;
private String email;
}
1.5 dao
UserRepository.java
@Repository
public interface UserRepository extends JpaRepository<User,Integer> {
}
1.6 service層
接口:UserService.java
public interface UserService {
/**
* 分頁查詢用戶
* @param pageable
* @return
*/
Page<User> findAllUser(Pageable pageable);
}
實現類:UserServiceImpl.java
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserRepository userRepository;
@Override
public Page<User> findAllUser(Pageable pageable) {
return userRepository.findAll(pageable);
}
}
1.7 測試接口 UserServiceImplTest.java
@SpringBootTest
@Slf4j
class UserServiceImplTest {
@Autowired
private UserRepository userRepository;
@Test
void findAllUser() {
Pageable pageable = PageRequest.of(1,2);//page是第幾頁,size每頁顯示多少條
Page<User> userList = userRepository.findAll(pageable);
log.info("共{}頁",userList.getTotalPages());
log.info("共{}條數據",userList.getTotalElements());
log.info("第{}頁",userList.getNumber());
log.info(userList.getContent().toString());
//userList.stream().forEach(user -> log.info(user.toString()));
}
}
測試結果
Hibernate: select user0_.id as id1_0_, user0_.email as email2_0_, user0_.password as password3_0_, user0_.phone as phone4_0_, user0_.user_name as user_nam5_0_ from user_info user0_ limit ?, ?
Hibernate: select count(user0_.id) as col_0_0_ from user_info user0_
2020-04-29 22:14:18.106 INFO 11740 --- [ main] c.s.service.impl.UserServiceImplTest : 共3頁
2020-04-29 22:14:18.106 INFO 11740 --- [ main] c.s.service.impl.UserServiceImplTest : 共5條數據
2020-04-29 22:14:18.106 INFO 11740 --- [ main] c.s.service.impl.UserServiceImplTest : 第1頁
2020-04-29 22:14:18.106 INFO 11740 --- [ main] c.s.service.impl.UserServiceImplTest : [User(id=4, userName=Rose, password=123456, phone=13110477888, email=xxx@126.com), User(id=5, userName=Vivian, password=123456, phone=13110477888, email=xxx@126.com)]