使用IDEASpringBoot和JPA整合分页
2020-03-05 17:21
1381 查看
使用IDEASpringBoot和JPA整合分页
效果展示:
步骤一:工程搭建
备注:(web–thymeleaf–JPA–JDBC–MySql)
步骤二:创建全局配置文件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文件需要引入两个依赖
<dependency> <groupId>net.sourceforge.nekohtml</groupId> <artifactId>nekohtml</artifactId> <version>1.9.22</version> </dependency> <!—通过webjars导入依赖--> <dependency> <groupId>org.webjars</groupId> <artifactId>jquery</artifactId> <version>3.3.1-1</version> </dependency>
步骤四:创建实体类
@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<Employee,Integer> { }
步骤六:编写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<Employee> page = employeeRepository.findAll(pageable); model.addAttribute("page",page); return "list"; } }
步骤七:在template中编写list.html
引入thymeleaf的命名空间:
xmlns:th="http://www.thymeleaf.org"
<html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <table border="1"> <tr> <td>编号</td> <td>姓名</td> <td>性别</td> <td>年龄</td> <td>生日</td> <td>操作</td> </tr> <tr th:each="emp:${page}"> <td th:text="${emp.eid}"></td> <td th:text="${emp.ename}"></td> <td th:text="${emp.gender}"></td> <td th:text="${emp.age}"></td> <td th:text="${#dates.format(emp.birth,'yyyy-MM-dd')}"></td> <td>修改 删除</td> </tr> </table> <a th:href="@{/user/findAll(start=0)}">首页</a> <a th:if="${not page.isFirst()}" th:href="@{/user/findAll(start=${page.number-1})}">上一页</a> <a th:if="${not page.isLast()}" th:href="@{/user/findAll(start=${page.number+1})}">下一页</a> <a th:href="@{/user/findAll(start=${page.totalPages-1})}">尾页</a> 第<span th:text="${page.number+1}"></span>页/共<span th:text="${page.totalPages}" ></span>页 共<span th:text="${page.totalElements}" ></span>条 </body>
- 点赞 2
- 收藏
- 分享
- 文章举报
相关文章推荐
- 使用 SpringBoot 之 JPA 整合 Redis 实现缓存
- 在Spring Boot中使用Spring-data-jpa实现分页查询
- 在Spring Boot中使用Spring-data-jpa实现分页查询
- springboot项目,使用idea整合oracle,导入依赖之后不能连接数据库
- 在Spring Boot中使用Spring-data-jpa实现分页查询
- 使用IDEA搭建SpringBoot项目整合Mybatis踩的坑。。。。
- springboot项目,使用idea整合oracle,导入依赖之后不能连接数据库
- SpringBoot整合Spring Data JPA、MySQL、Druid并使用Mockito实现单元测试
- Springboot 之 使用JPA进行分页操作
- idea中使用springboot整合redis,并测试
- 使用IDEA搭建SpringBoot项目且整合mongoDB和mysql
- 在IDEA中使用spring-boot,mySql,JPA
- 在idea里使用SpringBoot整合MyBatis时遇到的Mapper扫描不到的问题
- IDEA+maven+SpringBoot+JPA+Thymeleaf实现Crud及分页
- 在Spring Boot中使用Spring-data-jpa实现分页查询
- Idea下Spring Boot、thymeleaf、Jpa项目整合实例
- 在Spring Boot中使用Spring-data-jpa实现分页查询
- SpringBoot-使用JPA实现完整的CRUD和分页
- Springboot整合Mybatis分页使用Druid监控SQL日志
- 使用spring boot 整合spring jpa做基于restful风格的增删改查(maven ,mysql)