使用IDEASpringBoot和JPA整合分页

使用IDEASpringBoot和JPA整合分页

效果展示:

使用IDEASpringBoot和JPA整合分页_第1张图片

步骤一:工程搭建

备注:(web–thymeleaf–JPA–JDBC–MySql)
使用IDEASpringBoot和JPA整合分页_第2张图片
使用IDEASpringBoot和JPA整合分页_第3张图片

步骤二:创建全局配置文件application.yml

spring:
  datasource:
    url: jdbc:mysql://localhost/jpa_onetomany
    username: root
    password: root
    driver-class-name: com.mysql.jdbc.Driver
  jpa:
    hibernate:
      ddl-auto: update
      naming:
      #表中字段命名策略,这里要引入hibernate的核心包,不然这个命名策略会报错
        implicit-strategy: org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
        physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
        strategy: org.hibernate.cfg.ImprovedNamingStrategy
    show-sql: true
  thymeleaf:
    mode: LEGACYHTML5
    cache: false
  mvc:
    date-format: yyyy-MM-dd

server:
  port: 9090

步骤三:pom.xml文件需要引入两个依赖


   net.sourceforge.nekohtml
   nekohtml
   1.9.22



   org.webjars
   jquery
   3.3.1-1


步骤四:创建实体类

@Entity
public class Employee {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer eid;
    @Column
    private String  ename;
    @Column
    private String  gender;
    @Column
    private Integer age;
    @Column
    private  Date    birth;

    public Employee(){
        super();
    }

    public Employee(String ename, String gender, Integer age, Date birth) {
        this.ename = ename;
        this.gender = gender;
        this.age = age;
        this.birth = birth;
    }

    public Integer getEid() {
        return eid;
    }

    public void setEid(Integer eid) {
        this.eid = eid;
    }

    public String getEname() {
        return ename;
    }

    public void setEname(String ename) {
        this.ename = ename;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Date getBirth() {
        return birth;
    }

    public void setBirth(Date birth) {
        this.birth = birth;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "eid=" + eid +
                ", ename='" + ename + '\'' +
                ", gender='" + gender + '\'' +
                ", age=" + age +
                ", birth=" + birth +
                '}';
    }
}

步骤五:编写EmployeeRepository接口

@Repository
public interface EmployeeRepository extends JpaRepository {
}

步骤六:编写EmployeeController

分页实现:
JpaRepository ------Pageable------PageRequest

@Controller
public class EmployeeController {

     @Autowired
     EmployeeRepository employeeRepository;

      @RequestMapping("/user/findAll")
      public String findAll(@RequestParam(value="start",defaultValue = "0") Integer start,
                            @RequestParam(value="limit",defaultValue = "2") Integer limit, Model model){
          //分页
          start=start<0?0:start;
          Pageable pageable=new PageRequest(start,limit);
          Page page = employeeRepository.findAll(pageable);
          model.addAttribute("page",page);
          return "list";
      }

}

步骤七:在template中编写list.html

引入thymeleaf的命名空间:xmlns:th="http://www.thymeleaf.org"



    
    Title


编号 姓名 性别 年龄 生日 操作
修改 删除
首页 上一页 下一页 尾页页/共页 共

你可能感兴趣的:(使用IDEASpringBoot和JPA整合分页)