spring boot中JPA实现分页

项目结构:

spring boot中JPA实现分页_第1张图片

application.yml文件内容:

spring:
  datasource:
    url: jdbc:mysql:///boot_db?serverTimezone=UTC
    username: root
    password: root
  jpa:
    database: mysql
    show-sql: true
    hibernate:
      ddl-auto: update    

实体类:


@Entity
@Table(name = "w_student")
public class Student implements Serializable {

	/**
	* 
	*/
	private static final long serialVersionUID = -2066566554047350642L;
	private Integer sid;
	private String name;
	private Integer comscore = 100;
	private ClassRoom room;

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	public Integer getSid() {
		return sid;
	}

	@ManyToOne
	@JoinColumn(name = "gid")
	public ClassRoom getRoom() {
		return room;
	}
    //省去get 和set...
}

@Entity
@Table(name="w_room")
public class ClassRoom implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = -1882166817374498700L;
	private Integer gid;
	private String gname;
	
	@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
	public Integer getGid() {
		return gid;
	}
   //省去get和set...
}

dao层:

package bw.swn.boot.dao;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;

import bw.swn.boot.entity.Student;

public interface StudentDao extends JpaRepository {

	 Page  getStudentListByNameContaining(String mohu,Pageable pageable );
}

控制器:


@Controller
public class StudentController {

	 @Resource
	 private IStudentService studentService;
	 
	 @RequestMapping("/")
	 public String pagelist(@RequestParam(defaultValue="0")int offset,@RequestParam(required=false,name="mohu",defaultValue="") String mohu,Model model) {
		 PageRequest pageRequest=PageRequest.of(offset, 3);
		 Page page=this.studentService.pagelist(mohu, pageRequest);
		 model.addAttribute("mohu", mohu);
		 model.addAttribute("page", page);
		 return "pagelist";
	 }
   //.....
}

Thymeleaf:

	



编号 姓名 综合积分 班级
页,当前是第页。

 

你可能感兴趣的:(java)