<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.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.2.6.RELEASEversion>
<relativePath/>
parent>
<groupId>com.stormkaigroupId>
<artifactId>element-crudartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>element-crudname>
<description>element-cruddescription>
<properties>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-jpaartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<scope>runtimescope>
<optional>trueoptional>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<scope>runtimescope>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
<exclusions>
<exclusion>
<groupId>org.junit.vintagegroupId>
<artifactId>junit-vintage-engineartifactId>
exclusion>
exclusions>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
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
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');
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;
}
UserRepository.java
@Repository
public interface UserRepository extends JpaRepository<User,Integer> {
}
接口: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);
}
}
@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)]