- 一:SpringBoot+MyBatis+PageHelper+Layui+thymeleaf+MySQL实现分页
- 核心代码
- 总结
第一步:
在pom文件中引入坐标
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.1.4
com.alibaba
druid
1.1.16
mysql
mysql-connector-java
runtime
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-starter-thymeleaf
org.springframework.boot
spring-boot-starter-web
com.github.pagehelper
pagehelper
3.7.5
第二步: 在springboot启动类中配置PageHelper
@MapperScan("com.tgh.hospitalproject.mapper")
@SpringBootApplication
public class HospitalProjectApplication {
public static void main(String[] args) {
SpringApplication.run(HospitalProjectApplication.class, args);
}
//创建pageHelper,设置其属性,然后将pageHelper对象交给spring容器管理
@Bean
public PageHelper pageHelper() {
PageHelper pageHelper = new PageHelper();
Properties properties = new Properties();
properties.setProperty("offsetAsPageNum", "true");
properties.setProperty("rowBoundsWithCount", "true");
properties.setProperty("reasonable", "true");
properties.setProperty("dialect", "mysql");
pageHelper.setProperties(properties);
return pageHelper;
}
}
properties.setProperty("offsetAsPageNum", "true");开启用页码和页面大小进行分页
properties.setProperty("rowBoundsWithCount", "true");进行count查询
properties.setProperty("reasonable", "true");使页码参数合理,当页码<=0时查询第一页,页码>总页面时查询最后一页。
properties.setProperty("dialect", "mysql");使用mysql方言
第三步: application.yml文件配置
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/hospital?serverTimezone=GMT%2B8&characterEncoding=utf-8
username: root
password: root
type: com.alibaba.druid.pool.DruidDataSource
thymeleaf:
prefix: classpath:/templates/
suffix: .html
cache: false
mode: HTML5
encoding: UTF-8
logging:
level:
com.tgh.hospitalproject.dao : debug
第四步: 后台前台代码准备
前台页面:(完整代码请参考项目)
欢迎进入医疗系统后台管理
-
系统管理
- 医生管理
- 患者管理
- 药品管理
- 科目查询管理
- 疾病管理
- 预约管理
- 病史管理
- 住院信息管理
- 管理员管理
- 欢迎: [[${session.user!=null?session.user.username:'admin'}]]
- 退出登录
医生管理界面
系统管理
医生管理
| 序号 | 昵称 | 是否为专家 | 年纪 | 性别 | 证件号 | 所属部门 | 家庭住址 | 操作 |
|---|---|---|---|---|---|---|---|---|
| [[${doctors.getId()}]] | [[${doctors.getName}]] | [[${doctors.getExpert() == 1? '专家' : '非专家'}]] | [[${doctors.getAge()}]] | [[${doctors.getSex() == 0 ? '男' : '女'}]] | [[${doctors.getCertId()}]] | [[${doctors.getDepartment()}]] | [[${doctors.getAddress()}]] |
后台Controller:(思路:将查询到的医生数据放到model中,前台从model取出数据展现)
//分页显示所有数据
@RequestMapping("/manage")
public String findPageList(@RequestParam(required = false, defaultValue = "1") Integer pageNo,
@RequestParam(required = false, defaultValue = "10") Integer limit,
@RequestParam(required = false) String name,
@RequestParam(required = false) String certId,
Model model){
PageInfo doctorInfo = doctorService.findDoctorInfo(pageNo, limit, name, certId);
model.addAttribute("doctorList",doctorInfo.getList());
model.addAttribute("page",doctorInfo);
model.addAttribute("name",name);
model.addAttribute("certId",certId);
model.addAttribute("path","/admin/doctor/manage");
return "admin/doctorManage";
}
后台Service实现:(思路:在进行mapper操作之前执行PageHelper.startPage(页码,页面大小);)
@Override
public PageInfo findDoctorInfo(Integer pageNo, Integer limit, String name, String cerId) {
//由名字和身份证查询部分用户
if(!StringUtils.isEmpty(name) || !StringUtils.isEmpty(cerId)) {
PageHelper.startPage(pageNo,limit);
List doctorPart = doctorMapper.findByNameAndCerId(name, cerId);
PageInfo pageInfo = new PageInfo<>(doctorPart);
return pageInfo;
} else {
//查询所有用户
PageHelper.startPage(pageNo,limit);
List doctorAll = doctorMapper.findAll();
PageInfo pageInfo = new PageInfo<>(doctorAll);
return pageInfo;
}
}
咱们关注doctorMapper.findAll(),返回的是lb_doctor表中的所有数据
后台Mapper:
@Select({
"select * from lb_doctor"
})
@Results({
@Result(column="id", property="id", jdbcType= JdbcType.INTEGER, id=true),
@Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
@Result(column="age", property="age", jdbcType=JdbcType.INTEGER),
@Result(column="cert_id", property="certId", jdbcType=JdbcType.VARCHAR),
@Result(column="sex", property="sex", jdbcType=JdbcType.INTEGER),
@Result(column="department", property="department", jdbcType=JdbcType.VARCHAR),
@Result(column="address", property="address", jdbcType=JdbcType.VARCHAR),
@Result(column="user_id", property="userId", jdbcType=JdbcType.INTEGER),
@Result(column="text", property="text", jdbcType=JdbcType.VARCHAR),
@Result(column="expert", property="expert", jdbcType=JdbcType.INTEGER),
})
List findAllByPage();
核心代码
使用PageHelper的核心点在与后台Service实现部分
PageHelper.startPage(pageNo,limit); 对紧跟其的第一条语句末尾插入limit语句实现分页
List doctorPart = doctorMapper.findByNameAndCerId(name, cerId);
PageInfo pageInfo = new PageInfo<>(doctorPart); 对查询结果进行封装,包含分页信息和数据信息
查看PageInfo源码,发现是把结果数据封装在List集合里面
public class PageInfoimplements Serializable { private static final long serialVersionUID = 1L; //当前页 private int pageNum; //每页的数量 private int pageSize; //当前页的数量 private int size; //由于startRow和endRow不常用,这里说个具体的用法 //可以在页面中"显示startRow到endRow 共size条数据" //当前页面第一个元素在数据库中的行号 private int startRow; //当前页面最后一个元素在数据库中的行号 private int endRow; //总记录数 private long total; //总页数 private int pages; //结果集 private List list; ...以下省略
固Controller层使用doctorInfo.getList()获取结果数据
页面展示:
PageHelper具体使用参照核心代码即可
本测试中layui主要用到了分页,样式可参考layui分页
本测试中thymeleaf用来从后台Model域中获取数据,用到了th:each 、内联表达式 [[]]等,关于thymeleaf更多特性可参考前端爬坑一、thymeleaf在标签内部获得数据作为js函数的参数



