项目结构:
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<Student, Integer> {
Page<Student> 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<Student> page=this.studentService.pagelist(mohu, pageRequest);
model.addAttribute("mohu", mohu);
model.addAttribute("page", page);
return "pagelist";
}
//.....
}
Thymeleaf:
<div class="container">
<br>
<br>
<form>
<input type="text" name="mohu" th:value="${mohu}"> <input
type="hidden" id="offset" name="offset" th:value="${page.number}">
<button type="submit" class="btn btn-success">模糊搜索</button>
</form>
<hr>
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>编号</th>
<th>姓名</th>
<th>综合积分</th>
<th>班级</th>
<th>
<button class="btn btn-success">添加</button>
</th>
</tr>
</thead>
<tbody>
<tr th:each="stu:${page.getContent()}">
<td th:text="${stu.sid}"></td>
<td th:text="${stu.name}"></td>
<td th:text="${stu.comscore}"></td>
<td th:text="${stu.room.gname}"></td>
<td>
<button class="btn btn-warning" th:value="${stu.sid}">删除</button>
<button class="btn btn-primary" th:value="${stu.sid}">修改</button>
</td>
</tr>
<tr>
<td colspan="20" align="center">共<span
th:text="${page.totalPages}"></span>页,当前是第<span
th:text="${page.number+1}"></span>页。
<button class="btn btn-info" value="0">首页</button>
<button class="btn btn-info" th:if="${not page.isFirst()}"
th:value="${page.number-1}">上页</button>
<button class="btn btn-info" th:if="${not page.isLast()}"
th:value="${page.number+1}">下页</button>
<button class="btn btn-info" th:value="${page.totalPages-1}">末页</button>
</td>
</tr>
</tbody>
</table>
</div>