创建springboot项目在pom.xm
添加依赖
<?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.0modelVersion> <parent> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-parentartifactId> <version>2.2.2.RELEASEversion> <relativePath/> parent> <groupId>com.wxy97.shopgroupId> <artifactId>springboot_shopartifactId> <version>0.0.1-SNAPSHOTversion> <name>springboot_shopname> <description>Demo project for Spring Bootdescription> <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.projectlombokgroupId> <artifactId>lombokartifactId> <optional>trueoptional> dependency> <dependency> <groupId>mysqlgroupId> <artifactId>mysql-connector-javaartifactId> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-devtoolsartifactId> <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>
进行相关配置application.yml
server: port: 1106spring: datasource: type: com.zaxxer.hikari.HikariDataSource driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/shop?characterEncoding=utf-8 password: shop username: shop hikari: auto-commit: true jpa: hibernate: #启动项目时会自动根据bean在数据库中生成表 ddl-auto: update
bean包 User.java
package com.wxy97.shop.pojo;import lombok.Data;import javax.persistence.*;import java.io.Serializable;/** * @Author: wxySmile * @Date 20-1-2 下午12:19 */@Entity@Table(name = "s_user")@Datapublic class User implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; private String username; private String password; private boolean mg_state; private String name; private String email; private String tel;}
dao包 UserRepository.java
package com.wxy97.shop.dao;import com.wxy97.shop.pojo.User;import org.springframework.data.jpa.repository.JpaRepository;import java.util.List;/** * @Author: wxySmile * @Date 20-1-2 下午1:09 */public interface UserRepository extends JpaRepository<User,Integer> { @Override PagefindAll(Pageable pageable);}
启动项目后数据库会自动生成表,在数据库中插入数据
INSERT INTO `s_user` (`id`, `email`, `mg_state`, `name`, `password`, `tel`, `username`) VALUES(1, 'wxyrrcj@163.com', b'1', '若如初见', '3579c5bed0f11113ebfbbcfb9371eeb5', '15666666666', 'wxy'),(2, '123@love.com', b'1', '李四', '456', '88888888888', NULL),(3, '123@love.com', b'1', '张三', '123', '123456789', '123'),(4, '123@qq.com', b'0', '王五', '55555', '666666666', '555555'),(5, '456@qq.com', b'0', '赵', '6666', '666666666', '6666'),(6, 'aaa@qq.com', b'1', '我是第二页', '222', '66666666666', '2'),(7, '啊啊啊啊啊啊啊', b'0', '啊啊啊啊啊啊啊啊啊啊', '啊啊啊啊啊啊啊啊啊', '啊啊啊啊啊啊啊啊', '啊啊啊啊啊啊啊啊啊啊');
测试类
package com.wxy97.shop;import com.wxy97.shop.dao.UserRepository;import com.wxy97.shop.pojo.User;import org.junit.jupiter.api.Test;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.data.domain.Page;import org.springframework.data.domain.PageRequest;import org.springframework.data.domain.Pageable;import javax.annotation.Resource;@SpringBootTestclass SpringbootShopApplicationTests { @Resource UserRepository userRepository; @Test void contextLoads() { int pagenum=1; //页码 int pagesize=3; //每页条数 Pageable pageable = PageRequest.of(pagenum-1,pagesize); Page users = userRepository.findAll(pageable); for (User user : users) { System.out.println(user.getName()); } }}