页面显示所有医生(科室,头衔)
第一步
mysql取出所有数据,由于需要分类查直接加入参数
mapper包下
接口
加@Mapper注解
public List<Doctor> selectALlDoctors(@Param("officeId") Integer officeId,
@Param("titleId") Integer titleId);//多个参数使用@Param绑定参数
xml文件中
<resultMap id="doctorMap" type="Doctor">
<id property="doctorId" column="doctorId"/>
// 通过officeId返回一个office对象
<association property="office" column="officeId"
select="cn.kgc.mapper.OfficeMapper.selectOfficeById"/>
// 通过titleId返回一个title对象
<association property="title" column="titleId" select="cn.kgc.mapper.TitleMapper.selectTitleById"/>
</resultMap>
<select id="selectALlDoctors" resultMap="doctorMap" >
select * from doctor
<where>
<if test="officeId!=null and officeId!=0">
officeId=#{officeId}
</if>
<if test="titleId!=null and titleId!=0">
and titleId=#{titleId}
</if>
</where>
</select>
officeMapper和TitleMapper
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.kgc.mapper.OfficeMapper">
<select id="selectOfficeById" parameterType="Integer" resultType="Office">
select * from office where officeId = #{officeId}
</select>
<select id="selectAllOffices" resultType="Office">
select * from office
</select>
</mapper>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.kgc.mapper.TitleMapper">
<select id="selectTitleById" resultType="Title" parameterType="Integer">
select * from title where titleId = #{titleId}
</select>
<select id="selectAllTitles" resultType="Title">
select * from title
</select>
</mapper>
第二步
重点 主函数一定要加@EnableCaching注解 @EnableCaching 开启缓存
dao层缓冲区
因为医生,科室,头衔属于不常变化的,所以放入缓存区,页面取值直接从redis里取
加@Cacheable注解 可以吧数据放入缓存区
CacheDaoImpl 加注解@Repository
CacheDaoIMpl
@Repository
public class CacheDaoIMpl implements CacheDao{
@Autowired
private DoctorMapper doctorMapper;
@Autowired
private OfficeMapper officeMapper;
@Autowired
private TitleMapper titleMapper;
//Cacheable:执行此函数前先在缓存中到数据,若缓存中不存在则执行此函数,并将此函数的执行结果缓存起来。
//key为一个表达式,#p0表示第0个参数。redis中的键名为value::key
//string类型
@Override
@Cacheable(value = "doctor-page", key = "#p0+':'+#p1+':'+#p2") //数据库里的键名为 doctor-page::0:0:1 0:0代表所有属于科室(头衔)的医生 , 1 第一页
//需要分页 0:0:1 代表 所有的医生的第一页的数据
//0:0:2就是第二页的数据,1:1:1 就是officeid为1,titleid为1,的所有医生第一页!!!懂了吧
public DataPage<Doctor> getAllDoctor(Integer officeId, Integer titleId, Integer pageNum, Integer pageSize) {
//分页
PageHelper.startPage(pageNum,pageSize);
List<Doctor> doctors = doctorMapper.selectALlDoctors(officeId, titleId);
PageInfo<Doctor> pageInfo = new PageInfo<>(doctors);
//返回数据
DataPage<Doctor> dataPage = new DataPage<>();
dataPage.setDataList(pageInfo.getList());
dataPage.setRowCount((int)pageInfo.getTotal()); //总条数
dataPage.setPageCount(pageInfo.getPages()); //总页数
dataPage.setPageNum(pageNum);//第几页当前页
dataPage.setPageSize(pageSize);
return dataPage;
}
//redis里键名为office::list
@Override
@Cacheable(value = "offices", key = "'list'")
public List<Office> getAllOffice() {
return officeMapper.selectAllOffices();
}
//redis里键名为titles::list
@Override
@Cacheable(value = "titles", key = "'list'")
public List<Title> getAllTitle() {
return titleMapper.selectAllTitles();
}
}
service层
四个方法
1.获取所有医生
2.获取所有科室
3.获取所有头衔
4.获取下一周的日期~
DOctorServiceImpl 加@Service注解
@Autowired //自动装配
private CacheDao cacheDao;
@Override
public DataPage<Doctor> getAllDoctors(Integer officeId, Integer titleId, Date bookingDate, Integer pageNum, Integer pageSize) {
//缓存中取出医生
DataPage<Doctor> doctorPage = cacheDao.getAllDoctor(officeId, titleId, pageNum, pageSize);
return doctorPage;
}
@Override
public List<Office> getAllOffice() {
return cacheDao.getAllOffice();
}
@Override
public List<Title> getAllTitle() {
return cacheDao.getAllTitle();
}
@Override
public List<Date> getNextWeek() {
Calendar instance = Calendar.getInstance(); //获取当前时间
List<Date> list = new ArrayList<>();
for (int o = 0; o < 7; o++) {
instance.add(Calendar.DATE,1); //加一天
list.add(instance.getTime()); //getTime 转换成Date
}
return list;
}
最后一步Controller
获取所有医生~
因为刚进页面,参数是没有的,防止空指针异常,需要给默认值,pagesize默认值是在application.properties配置中写的,需要用 @Value引用
@Autowired
private DoctorService doctorService;
//@value:引用配置文件中的配置变量
@Value("${doctor.page.size}")
private Integer pageSize;
@RequestMapping("all_doctors")
public String getAllDoctors(Integer officeId, Integer titleId, Integer pageNum,
@DateTimeFormat(pattern = "yyyy-MM-dd") Date bookingDate,Model model){
Integer page = pageNum==null? 1 : pageNum;
Integer fid = officeId==null? 0 : officeId;
Integer tid = titleId==null? 0 : titleId;
Date date = bookingDate;
if (date == null) {
//计算明天的日期
Calendar instance = Calendar.getInstance();
instance.add(Calendar.DATE,1); //加一天
date = instance.getTime();
}
//获取所有医生
DataPage<Doctor> doctorPage = doctorService.getAllDoctors(fid, tid, date, page, pageSize);//pae
//获取所有科室,头衔
List<Office> offices = doctorService.getAllOffice();
List<Title> titles = doctorService.getAllTitle();
//获取下一周
List<Date> weekday = doctorService.getNextWeek();
model.addAttribute("doctorPage",doctorPage);
model.addAttribute("offices",offices);
model.addAttribute("titles",titles);
model.addAttribute("weekday",weekday);
//把 值传回页面,方便做回显 ~
model.addAttribute("officeId",fid);
model.addAttribute("titleId",tid);
return "all_doctors";
}
最后再说一下回显的问题
判断一下如果后台传过来的id等于循环的id加selected字符串 添加选中状态就可以了
${(officeId==o.officeId)?string(“selected”,"")}
okok第一步完成啦 现在只有一个功能 就是查询~
最后发一下页面
all_doctors.ftl
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<#--引用路径无需写static-->
<link rel="stylesheet" href="/css/bootstrap.min.css">
<script src="/js/jquery.min.js"></script>
<script src="/js/popper.min.js"></script>
<script src="/js/bootstrap.min.js"></script>
<title>医大附院预约系统</title>
<script type="text/javascript">
<#--function getPage(page) {-->
<#-- location.href="/all_doctors?pageNum="+page+"&officeId=${officeId}&titleId=${titleId}&bookingDate="+$("#booking-date").val();-->
<#--}-->
</script>
</head>
<body>
<div id="login-button" class="text-right pt-3 pr-5">
<#if user??>
<h4>欢迎您:${user.name}</h4>
<#else>
<button type="button" class="btn btn-sm btn-outline-primary" data-toggle="modal" data-target="#loginModal">登录系统</button>
</#if>
</div>
<h1 class="text-center pt-1 pb-5">医大附院预约系统</h1>
<div class="container">
<div class="row">
<div class="col-md-9">
<form action="/all_doctors" method="post" class="form-inline">
<div class="form-group">
<label>科室查询:</label>
<select class="form-control" name="officeId">
<option value="0">全部科室</option>
<#list offices as o>
<option value="${o.officeId}" ${(officeId==o.officeId)?string("selected","")}>${o.officeName}</option>
</#list>
</select>
<label>职称查询:</label>
<select class="form-control" name="titleId">
<option value="0">全部职称</option>
<#list titles as t>
<option value="${t.titleId}" ${(titleId==t.titleId)?string("selected","")}>${t.titleName}</option>
</#list>
</select>
<button type="submit" class="btn btn-outline-secondary">查询</button>
</div>
</form>
</div>
<div class="col-md-3">
<form class="form-inline">
<div class="form-group">
<label>预约日期:</label>
<select class="form-control" id="booking-date" name="bookingDate" onchange="getTotal(this)">
<#list weekday as d>–>
<option value="${d?string("yyyy-MM-dd")}">
${d?string("yyyy-MM-dd")}
</option>
</#list>–>
</select>
</div>
</form>
</div>
</div>
<hr/>
<ul class="list-unstyled">
<#list doctorPage.dataList as d>
<li class="media my-4">
<img src="\images\ico.jpg" width="120px" class="align-self-center mr-3" alt="...">
<div class="media-body">
<h3 class="mt-0 mb-2">${d.name}(${d.title.titleName})</h3>
<div class="row">
<div class="col-md-9">简介:${d.summary}</div>
<div class="col-md-2 text-center">
<h4>余号:<span id="total-${d.doctorId}" style="color:red">${d.total}</span></h4>
<button type="button" onclick="addBooking(${d.doctorId})" class="btn btn-outline-primary">预约挂号</button>
</div>
</div>
</div>
</li>
</#list>
</ul>
<nav class="pb-5 pt-3">
<ul class="pagination justify-content-center">
<#if doctorPage.pageNum == 1>
<li class="page-item disabled">
<a class="page-link" href="javascript:void(0);" tabindex="-1">首页</a>
</li>
<#else>
<li class="page-item">
<a class="page-link" onclick="getPage(1)" href="javascript:void(0)" tabindex="-1">首页</a>
</li>
</#if>
<#--c:foreach begin="1" end="${doctorPage.pageCount}" var="i" -->
<#list 1..doctorPage.pageCount as i>
<#if doctorPage.pageNum == i>
<li class="page-item active">
<a class="page-link" href="javascript:void(0);">${i}</a>
</li>
<#else>
<li class="page-item">
<a class="page-link" onclick="getPage(${i})" href="javascript:void(0)">${i}</a>
</li>
</#if>
</#list>
<#if doctorPage.pageNum == doctorPage.pageCount>
<li class="page-item disabled">
<a class="page-link" href="javascript:void(0);" tabindex="-1">末页</a>
</li>
<#else>
<li class="page-item">
<a class="page-link" onclick="getPage(${doctorPage.pageCount})" href="javascript:void(0)" tabindex="-1">末页</a>
</li>
</#if>
</ul>
</nav>
</div>
<!-- 模态框 -->
<div class="modal fade" id="loginModal">
<div class="modal-dialog">
<form id="login-form">
<div class="modal-content">
<!-- 模态框头部 -->
<div class="modal-header">
<h4 class="modal-title">用户登录</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<!-- 模态框主体 -->
<div class="modal-body">
<div class="form-group">
<input type="text" class="form-control" name="username" placeholder="请输入用户名">
</div>
<div class="form-group">
<input type="password" class="form-control" name="password" placeholder="请输入密码">
</div>
</div>
<!-- 模态框底部 -->
<div class="modal-footer">
<button type="submit" onclick="userLogin()" class="btn btn-primary">登录</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button>
</div>
</div>
</form>
</div>
</div>
</body>
</html>
就这么多,很基础很菜,自己做一下总结,有问题随便问看到第一时间回复