typora-root-url: imgtypora-copy-images-to: img

第2章 预约管理-检查项、检查组管理

学习目标:

  • 掌握新增检查项实现过程

  • 掌握检查项分页查询实现过程

  • 掌握删除检查项实现过程

  • 掌握编辑检查项实现过程

  • 掌握新增检查组实现过程

  • 掌握检查组分页查询实现过程

  • 掌握删除检查组实现过程

  • 掌握编辑检查组实现过程

1. 新增检查项

  1. 本章节完成的功能开发是预约管理功能,包括检查项管理(身高,体重)、检查组管理(外科)、体检套餐管理(公司健康体检)、预约设置等(参见产品原型)。预约管理属于系统的基础功能,主要就是管理一些体检的基础数据。

【目标】

新增检查项

【路径】

  1. checkitem.html, 新建按钮绑定事件,弹出新增窗口,填写内容,点击确定绑定事件 提交(前要校验一下,成功后才能提交(axios)) formData,对返回的结果提示信息,如果成功,则要关闭新增窗口,且刷新列表数据
  2. CheckItemController, 用checkitem来接收formData, 调用服务添加add,返回result给页面
  3. CheckItemService与实现 add 方法, 调用dao
  4. CheckItemDao与映射文件 添加add方法,insert into t_checkitem () values(…)

【讲解】

1.1. 前台代码

检查项管理页面对应的是checkitem.html页面,根据产品设计的原型已经完成了页面基本结构的编写,现在需要完善页面动态效果。

1.1.1. 弹出新增窗口

页面中已经提供了新增窗口,只是处于隐藏状态。只需要将控制展示状态的属性dialogFormVisible改为true就可以显示出新增窗口。

新建按钮绑定的方法为handleCreate,所以在handleCreate方法中修改dialogFormVisible属性的值为true即可。同时为了增加用户体验度,需要每次点击新建按钮时清空表单输入项。

  1. // 重置表单
  2. resetForm() {
  3. this.formData = {};
  4. },
  5. // 弹出添加窗口
  6. handleCreate() {
  7. this.resetForm();
  8. this.dialogFormVisible = true;
  9. },

1.1.2. 输入校验

  1. rules: {//校验规则
  2. code: [{ required: true, message: '项目编码为必填项', trigger: 'blur' }],
  3. name: [{ required: true, message: '项目名称为必填项', trigger: 'blur' }]
  4. }

1.1.3. 提交表单数据

点击新增窗口中的确定按钮时,触发handleAdd方法,所以需要在handleAdd方法中进行完善。

  1. //添加
  2. handleAdd () {
  3. //校验表单输入项是否合法
  4. this.$refs['dataAddForm'].validate((valid) => {
  5. if (valid) {
  6. //表单数据校验通过,发送ajax请求将表单数据提交到后台
  7. axios.post("/checkitem/add.do",this.formData).then((res)=> {
  8. //隐藏新增窗口
  9. this.dialogFormVisible = false;
  10. //判断后台返回的flag值,true表示添加操作成功,false为添加操作失败
  11. if(res.data.flag){
  12. this.$message({
  13. message: res.data.message,
  14. type: 'success'
  15. });
  16. }else{
  17. this.$message.error(res.data.message);
  18. }
  19. })
  20. } else {
  21. this.$message.error("表单数据校验失败");
  22. return false;
  23. }
  24. });
  25. },
  26. //分页查询
  27. findPage() {
  28. },

1.2. 后台代码

1.2.1. Controller

在health_web工程中创建CheckItemController

  1. package com.itheima.health.controller;
  2. import com.alibaba.dubbo.config.annotation.Reference;
  3. import com.itheima.health.constant.MessageConstant;
  4. import com.itheima.health.entity.Result;
  5. import com.itheima.health.pojo.CheckItem;
  6. import com.itheima.health.service.CheckItemService;
  7. import org.springframework.web.bind.annotation.RequestBody;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import org.springframework.web.bind.annotation.RestController;
  10. /**
  11. * 体检检查项管理
  12. */
  13. @RestController
  14. @RequestMapping("/checkitem")
  15. public class CheckItemController {
  16. @Reference
  17. private CheckItemService checkItemService;
  18. //新增
  19. @RequestMapping("/add")
  20. public Result add(@RequestBody CheckItem checkItem){
  21. checkItemService.add(checkItem);
  22. return new Result(true,MessageConstant.ADD_CHECKITEM_SUCCESS);
  23. }
  24. }

1.2.2. 服务接口

在health_interface工程中创建CheckItemService接口

  1. package com.itheima.health.service;
  2. import com.itheima.health.pojo.CheckItem;
  3. /**
  4. * 检查项服务接口
  5. */
  6. public interface CheckItemService {
  7. void add(CheckItem checkItem);
  8. }

1.2.3. 服务实现类

在health_service工程中创建CheckItemServiceImpl实现类

  1. package com.itheima.health.service;
  2. import com.alibaba.dubbo.config.annotation.Service;
  3. import com.itheima.health.dao.CheckItemDao;
  4. import com.itheima.health.pojo.CheckItem;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.transaction.annotation.Transactional;
  7. /**
  8. * 检查项服务
  9. */
  10. @Service(interfaceClass = CheckItemService.class)
  11. public class CheckItemServiceImpl implements CheckItemService {
  12. @Autowired
  13. private CheckItemDao checkItemDao;
  14. //新增
  15. public void add(CheckItem checkItem) {
  16. checkItemDao.add(checkItem);
  17. }
  18. }

1.2.4. Dao接口

在health_dao工程中创建CheckItemDao接口,本项目是基于Mybatis的Mapper代理技术实现持久层操作,故只需要提供接口和Mapper映射文件,无须提供实现类

  1. package com.itheima.health.dao;
  2. import com.itheima.health.pojo.CheckItem;
  3. /**
  4. * 持久层Dao接口
  5. */
  6. public interface CheckItemDao {
  7. void add(CheckItem checkItem);
  8. }

1.2.5. Mapper映射文件

在health_dao工程中创建CheckItemDao.xml映射文件,需要和CheckItemDao接口在同一目录下

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  3. "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
  4. <mapper namespace="com.itheima.health.dao.CheckItemDao">
  5. <!--新增-->
  6. <insert id="add" parameterType="com.itheima.health.pojo.CheckItem">
  7. insert into t_checkitem(code,name,sex,age,price,type,remark,attention)
  8. values
  9. (#{code},#{name},#{sex},#{age},#{price},#{type},#{remark},#{attention})
  10. </insert>
  11. </mapper>

【小结】

  1. 先分析,业务对象是谁,对应表结构在哪里,表中的字段是什么意思

  2. 新增,表中数据从哪里来,从前端, 提供输入formData双向绑定,提交数据formData

  3. 写出操作的步骤,按着步骤完成代码

  4. 套路:
    前端提请求 数据类型->Controller接收数据型->调用service->调用dao->映射文件
    dao->service->controller->页面,提示结果,如果成功怎么样,失败怎么样

2. 检查项分页

【目标】

1:熟悉分页功能中的请求参数

2:熟悉分页功能中的响应数据

3:检查项分页功能实现

【路径】

1:前台代码 checkitem.html

  1. 需要提交的
  2. {
  3. page: 页码
  4. size: 大小
  5. 条件:
  6. }
  7. 响应的结果
  8. {
  9. flag:
  10. message:
  11. data:{ pageResult
  12. total: 100,
  13. rows:[]
  14. }
  15. }
  1. 查询绑定事件,提交查询的条件pagination(页码,大小,查询的条件),对结果进行提示,成功,绑定列表数据dataList=res.data.data.rows,绑定总记录数pagination.total=res.data.data.total。

  2. CheckItemController 用QueryPageBean接收pagination对象, 调用服务查询返回pageResult, 返回Result(flag,message,data: pageResult)

  3. CheckItemService, 实现分页查询 有条件则要模糊查询,调用Dao查询。且返回pageResult
    使用PageHelper, PageHelper.startPage(页码,大小), 紧接着的查询语句会被分页

  4. CheckitemDao 条件查询Page

本项目所有分页功能都是基于ajax的异步请求来完成的,请求参数和后台响应数据格式都使用json数据格式。

1:请求参数包括页码、每页显示记录数、查询条件。

请求参数的json格式为:{currentPage:1,pageSize:10,queryString:’’itcast’’}

2:后台响应数据包括总记录数、当前页需要展示的数据集合。

响应数据的json格式为:{total:1000,rows:[]}

如下图:

传智健康项目讲义(第2章) - 图1

2.1. 前台代码

2.1.1. 定义分页相关模型数据

  1. pagination: {//分页相关模型数据
  2. currentPage: 1,//当前页码
  3. pageSize:10,//每页显示的记录数
  4. total:0,//总记录数
  5. queryString:null//查询条件
  6. },
  7. dataList: [],//当前页要展示的分页列表数据

2.1.2. 定义分页方法

在页面中提供了findPage方法用于分页查询,为了能够在checkitem.html页面加载后直接可以展示分页数据,可以在VUE提供的钩子函数created中调用findPage方法

  1. //钩子函数,VUE对象初始化完成后自动执行
  2. created() {
  3. this.findPage();
  4. },
  1. // 发送查询的请求,提交pagination(currentPage,pageSize)
  2. axios.post('/checkitem/findPage.do',this.pagination).then(res=>{
  3. if(res.data.flag){
  4. // 绑定数据
  5. //res.data => result {flag, message,data}
  6. // result.data = pageResult {total, rows}
  7. this.dataList = res.data.data.rows;
  8. // 总计录数
  9. this.pagination.total=res.data.data.total;
  10. }else{
  11. this.$message.error(res.data.message);
  12. }
  13. })

2.1.3. 完善分页方法执行时机

除了在created钩子函数中调用findPage方法查询分页数据之外,当用户点击查询按钮或者点击分页条中的页码时也需要调用findPage方法重新发起查询请求。

为查询按钮绑定单击事件,调用findPage方法

  1. <el-button @click="handleCurrentChange(1)" class="dalfBut">查询</el-button>

为分页条组件绑定current-change事件,此事件是分页条组件自己定义的事件,当页码改变时触发,对应的处理函数为handleCurrentChange

  1. <div class="pagination-container">
  2. <el-pagination
  3. class="pagiantion"
  4. @current-change="handleCurrentChange"
  5. :current-page="pagination.currentPage"
  6. :page-size="pagination.pageSize"
  7. layout="total, prev, pager, next, jumper"
  8. :total="pagination.total">
  9. </el-pagination>
  10. </div>

定义handleCurrentChange方法

  1. //切换页码
  2. handleCurrentChange(currentPage) {
  3. // currentPage为切换后的页码
  4. this.pagination.currentPage = currentPage;
  5. this.findPage();
  6. },

2.2. 后台代码

2.2.1. Controller

在CheckItemController中增加分页查询方法

  1. /**
  2. * 分页查询
  3. */
  4. @PostMapping("/findPage")
  5. public Result findPage(@RequestBody QueryPageBean queryPageBean){
  6. // 调用业务来分页
  7. PageResult<CheckItem> pageResult = checkItemService.findPage(queryPageBean);
  8. //return pageResult;
  9. // 返回给页面, 包装到Result, 统一风格
  10. return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,pageResult);
  11. }

2.2.2. 服务接口

在CheckItemService服务接口中扩展分页查询方法

  1. /**
  2. *
  3. * @param queryPageBean
  4. * @return
  5. */
  6. PageResult<CheckItem> findPage(QueryPageBean queryPageBean);

2.2.3. 服务实现类

在CheckItemServiceImpl服务实现类中实现分页查询方法,基于Mybatis分页助手插件实现分页

  1. /**
  2. * 分页查询
  3. * @param queryPageBean
  4. * @return
  5. */
  6. @Override
  7. public PageResult<CheckItem> findPage(QueryPageBean queryPageBean) {
  8. //第二种,Mapper接口方式的调用,推荐这种使用方式。
  9. PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
  10. // 模糊查询 拼接 %
  11. // 判断是否有查询条件
  12. if(!StringUtils.isEmpty(queryPageBean.getQueryString())){
  13. // 有查询条件,拼接%
  14. queryPageBean.setQueryString("%" + queryPageBean.getQueryString() + "%");
  15. }
  16. // 紧接着的查询语句会被分页
  17. Page<CheckItem> page = checkItemDao.findByCondition(queryPageBean.getQueryString());
  18. PageResult<CheckItem> pageResult = new PageResult<CheckItem>(page.getTotal(), page.getResult());
  19. return pageResult;
  20. }

正常分页逻辑(即:不使用PageHelper):

  • 写2条sql
  • 第一条sql:select count(0) from tcheckitem where name = ‘传智身高’ —>封装到PageResult中total 第二条sql:select _ from t_checkitem where name = ‘传智身高’ limit ?,? —>封装到PageResult中rows*
  • limit ?,? :第一个问号:表示从第几条开始检索 计算:(currentPage-1)*pageSize 第二个问号:表示当前页最多显示的记录数, 计算:pageSize
  • 问题:1:麻烦;2:不能通用,当前切换数据库的时候改动的代码多。

2.2.4. Dao接口

在CheckItemDao接口中扩展分页查询方法

  1. /**
  2. * 分页条件查询
  3. * @return
  4. */
  5. Page<CheckItem> findByCondition(String queryString);

2.2.5. Mapper映射文件

在CheckItemDao.xml文件中增加SQL定义

  1. <!--条件查询-->
  2. <select id="findByCondition" resultType="Checkitem" parameterType="String">
  3. select * From t_checkitem
  4. <if test="value !=null and value.length > 0">
  5. where code like #{value} or name like #{value}
  6. </if>
  7. </select>
  8. <!-- Mybatis 动态参数赋值 DynamicContext
  9. <if>标签里的变量,如果参数类型是基本数据类型,只能用 value 或 _parameter,这个是由它的底层ognl表达式决定的。如果参数类型是对象类型,则可以填它的属性。另外,使用#的参数可以是形参名也可以是value
  10. -->

如果使用if进行判断,这里需要是value读取值,不能改成其他参数。

【小结】

  1. 提交currentPage, pageSize

  2. 使用PageHelper ```xml 配置它 mybatis的拦截器 PageHelper.startPage(页码,大小) dao的查询语句会被分页

拦截sql语句,拼接查询总计录数的语句 拼接分页的语句 ThreadLocal 线程绑定数据

  1. 3.
  2. 前端接收返回的结果,格式Result(PageResult)
  3. ```json
  4. result={
  5. flag:true,
  6. message:'查询成功',
  7. data:{ // pageResult
  8. total: 100,
  9. rows: [{checkitem}]
  10. }
  11. }
  12. res.data => result
  13. 查询的结果集 res.data.data.rows
  14. 总计录数 res.data.data.total
  1. 分页查询的时机:

    • 进入页面
    • 页码变更时要查询
    • 点击查询按钮时要查,回到第一页
    • 添加成功后要查
    • 修改成功后要查
    • 删除成功后要查

3. 删除检查项

【目标】

删除检查项

【路径】

  1. 绑定删除按钮,获取要删除的检查项id, 弹出询问窗口,确认后就提交请求,把id传给后台,提示返回的结果,成功则刷新列表。
  2. CheckItemCotnroller接收id, 调用服务删除,返回操作结果给页面
  3. CheckItemService

    • 判断检查项是否被检查组使用了 调用dao通过检查项id查询检查组与检查项的关系表,统计个数
    • 个数>0不能删除,报错, 抛出自定义异常
    • =0就可以删除
  4. CheckItemDao

    • 通过检查项id查询检查组与检查项的关系表,统计个数
    • 通过id删除检查项
  5. 做一个全局异常处理 @ControllerAdvice

【讲解】

3.1. 前台代码

为了防止用户误操作,点击删除按钮时需要弹出确认删除的提示,用户点击取消则不做任何操作,用户点击确定按钮再提交删除请求。

3.1.1. 绑定单击事件

需要为删除按钮绑定单击事件,并且将当前行数据作为参数传递给处理函数

  1. <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>

调用的方法

  1. // 删除
  2. handleDelete(row) {
  3. alert(row.id);
  4. }

3.1.2. 弹出确认操作提示

用户点击删除按钮会执行handleDelete方法,此处需要完善handleDelete方法,弹出确认提示信息。ElementUI提供了$confirm方法来实现确认提示信息弹框效果

  1. // 删除
  2. handleDelete(row) {
  3. // alert(row.id);
  4. this.$confirm("确认删除当前选中记录吗?","提示",{type:'warning'}).then(()=>{
  5. //点击确定按钮时只需此处代码
  6. alert('用户点击的是确定按钮');
  7. });
  8. }

传智健康项目讲义(第2章) - 图2

3.1.3. 发送请求

如果用户点击确定按钮就需要发送ajax请求,并且将当前检查项的id作为参数提交到后台进行删除操作

  1. // 删除
  2. handleDelete(row) {
  3. // row行数据,数据库中的一条记录,checkitem实体对象
  4. // 获取删除的id
  5. var id = row.id;
  6. //alert(JSON.stringify(row));
  7. this.$confirm('此操作将【永久删除】该检查项, 是否继续?', '提示', {
  8. confirmButtonText: '确定',
  9. cancelButtonText: '取消',
  10. type: 'warning'
  11. }).then(() => {
  12. // 点击确定后调用
  13. axios.post('/checkitem/deleteById.do?id=' + id).then(res=>{
  14. this.$message({
  15. message: res.data.message,
  16. type: res.data.flag?"success":"error"
  17. })
  18. if(res.data.flag){
  19. // 成功
  20. // 刷新列表数据
  21. this.findPage();
  22. }
  23. })
  24. }).catch(() => {
  25. // 点击取消后调用
  26. // 空着,防止报错
  27. });
  28. }

3.2. 后台代码

3.2.0 自定义异常与处理

在health_common工程中创建

  1. package com.itheima.health.exception;
  2. /**
  3. * Description: 自定义异常
  4. * 友好提示
  5. * 终止已经不符合业务逻辑的代码
  6. * User: Eric
  7. */
  8. public class HealthException extends RuntimeException{
  9. public HealthException(String message){
  10. super(message);
  11. }
  12. }

3.2.1. Controller

在CheckItemController中增加删除方法

  1. /**
  2. * 删除
  3. */
  4. @PostMapping("/deleteById")
  5. public Result deleteById(int id){
  6. // 调用业务服务
  7. //try {
  8. checkItemService.deleteById(id);
  9. //} catch (Exception e) {
  10. // e.printStackTrace();
  11. //}
  12. // 响应结果
  13. return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
  14. }

添加异常处理类

  1. package com.itheima.health.controller;
  2. import com.itheima.health.entity.Result;
  3. import com.itheima.health.exception.HealthException;
  4. import org.springframework.web.bind.annotation.ExceptionHandler;
  5. import org.springframework.web.bind.annotation.RestControllerAdvice;
  6. /**
  7. * Description: No Description
  8. * User: Eric
  9. */
  10. // 与前端约定好的,返回的都是json数据
  11. @RestControllerAdvice
  12. public class HealExceptionAdvice {
  13. /**
  14. * 自定义招出的异常处理
  15. * @param he
  16. * @return
  17. */
  18. @ExceptionHandler(HealthException.class)
  19. public Result handleHealthException(HealthException he){
  20. return new Result(false, he.getMessage());
  21. }
  22. /**
  23. * 所有未知的异常
  24. * @param he
  25. * @return
  26. */
  27. @ExceptionHandler(Exception.class)
  28. public Result handleException(Exception he){
  29. return new Result(false, "发生未知错误,操作失败,请联系管理员");
  30. }
  31. }

3.2.2. 服务接口

在CheckItemService服务接口中扩展删除方法

  1. void delete(Integer id) throws HealthException;

3.2.3. 服务实现类

注意:不能直接删除,需要判断当前检查项是否和检查组关联,如果已经和检查组进行了关联则不允许删除

  1. /**
  2. * 删除
  3. * @param id
  4. */
  5. @Override
  6. public void deleteById(int id) throws HealthException {
  7. //先判断这个检查项是否被检查组使用了
  8. //调用dao查询检查项的id是否在t_checkgroup_checkitem表中存在记录
  9. int cnt = checkItemDao.findCountByCheckItemId(id);
  10. //被使用了则不能删除
  11. if(cnt > 0){
  12. //??? health_web能捕获到这个异常吗?
  13. throw new HealthException(MessageConstant.CHECKITEM_IN_USE);
  14. }
  15. //没使用就可以调用dao删除
  16. checkItemDao.deleteById(id);
  17. }

3.2.4. Dao接口

在CheckItemDao接口中扩展方法findCountByCheckItemId和deleteById

  1. /**
  2. * 检查 检查项是否被检查组使用了
  3. * @param id
  4. * @return
  5. */
  6. int findCountByCheckItemId(int id);
  7. /**
  8. * 通过id删除检查项
  9. * @param id
  10. */
  11. void deleteById(int id);

3.2.5. Mapper映射文件

在CheckItemDao.xml中扩展SQL语句

  1. <!--
  2. 查询优化
  3. select count(1) from t_checkitem; 1
  4. 使用人混淆
  5. select count(100) from t_checkitem; 1
  6. select count('abc1') from t_checkitem; 1
  7. 上面三条语句相对而言,查询速度最快
  8. 相对而言慢一丢丢,原因,要解析列名是否在表中存在
  9. select count(id) from t_checkitem; 2
  10. 最慢,原因,要解析*含义,列表表中所有的列
  11. select count(*) from t_checkitem; 3
  12. -->
  13. <select id="findCountByCheckItemId" parameterType="int" resultType="int">
  14. select count(1) from t_checkgroup_checkitem where checkitem_id=#{id}
  15. </select>
  16. <delete id="deleteById" parameterType="int">
  17. delete from t_checkitem where id=#{id}
  18. </delete>

【小结】

  1. 企业开发分为2种:物理删除(删除表中的数据),逻辑删除(多)(更新表的字段status,state,active 为失效, 代表着这条记录不再使用
  2. 自定义异常 作用
  3. 全局异常处理@RestControllerAdvice, @ExceptionHandler
  4. 删除时要询问提示一下,防止用户操作失误
  5. 在dubbo的架构中,服务端抛出自定义异常要在接口的方法中声明抛出,否则会被dubbo转成RuntimeException

4. 编辑检查项

【目标】

1:编辑检查项(ID查询,回显)

2:编辑检查项(更新保存,执行update)

【路径】

  1. 回显数据

    • 绑定编辑按钮,清空表单,弹出编辑窗口,获取id, 发送请求,获取响应的数据绑定到formData,如果失败要提示
    • CheckItemController, CheckItemService, checkItemDao, 通过id查询检查项
  2. 修改的提交

    • 绑定确定按钮,校验后发送请求提交formData。提示返回的结果,成功则关闭编辑窗口,刷新列表数据
    • CheckItemController, checkItemService, checkItemDao 更新的方法

【讲解】

4.1. 前台代码

用户点击编辑按钮时,需要弹出编辑窗口并且将当前记录的数据进行回显,用户修改完成后点击确定按钮将修改后的数据提交到后台进行数据库操作。

4.1.1. 绑定单击事件

需要为编辑按钮绑定单击事件,并且将当前行数据作为参数传递给处理函数

  1. <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>

处理事件:handleUpdate();

  1. // 弹出编辑窗口
  2. handleUpdate(row) {
  3. alert(row.id);
  4. },

4.1.2. 弹出编辑窗口回显数据

当前页面中的编辑窗口已经提供好了,默认处于隐藏状态。在handleUpdate方法中需要将编辑窗口展示出来,并且需要发送ajax请求查询当前检查项数据用于回显

  1. // 弹出编辑窗口
  2. handleUpdate(row) {
  3. this.resetForm();
  4. var id = row.id;
  5. // 弹出编辑窗口
  6. this.dialogFormVisible4Edit = true;
  7. // 回显 发送请求
  8. axios.get('/checkitem/findById.do?id=' + id).then(res => {
  9. if(res.data.flag){
  10. // 绑定数据回显, 编辑的form表单也绑定了这个formData
  11. this.formData = res.data.data;
  12. }else{
  13. this.$message.error(res.data.message);
  14. }
  15. })
  16. },

4.1.3. 发送请求更改数据

在编辑窗口中修改完成后,点击确定按钮需要提交请求,所以需要为确定按钮绑定事件并提供处理函数handleEdit

  1. <div slot="footer" class="dialog-footer">
  2. <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
  3. <el-button type="primary" @click="handleEdit()">确定</el-button>
  4. </div>

handleEdit()方法

  1. //编辑提交
  2. handleEdit() {
  3. // 提交修改
  4. this.$refs['dataEditForm'].validate((valid) => {
  5. if (valid) {
  6. // 所有检验通过, 提交数据给后台this.formData
  7. axios.post('/checkitem/update.do', this.formData).then(res=>{
  8. this.$message({
  9. message: res.data.message,
  10. type: res.data.flag?"success":"error"
  11. })
  12. if(res.data.flag){
  13. // 成功的处理
  14. // 关闭窗口
  15. this.dialogFormVisible4Edit = false;
  16. // 刷新列表数据
  17. this.findPage();
  18. }
  19. })
  20. } else {
  21. console.log('error submit!!');
  22. return false;
  23. }
  24. });
  25. },

4.2. 后台代码

4.2.1. Controller

在CheckItemController中增加编辑方法

  1. /**
  2. * 通过id查询
  3. */
  4. @GetMapping("/findById")
  5. public Result findById(int id){
  6. CheckItem checkItem = checkItemService.findById(id);
  7. return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItem);
  8. }
  9. /**
  10. * 修改检查项
  11. * @param checkitem
  12. * @return
  13. */
  14. @PostMapping("/update")
  15. public Result update(@RequestBody CheckItem checkitem){
  16. // 调用业务服务
  17. checkItemService.update(checkitem);
  18. // 响应结果给前端
  19. return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
  20. }

4.2.2. 服务接口

在CheckItemService服务接口中扩展编辑方法

  1. /**
  2. * 通过id查询
  3. * @param id
  4. * @return
  5. */
  6. CheckItem findById(int id);
  7. /**
  8. * 更新检查项
  9. * @param checkitem
  10. */
  11. void update(CheckItem checkitem);

4.2.3. 服务实现类

在CheckItemServiceImpl实现类中实现编辑方法

  1. /**
  2. * 通过id查询
  3. * @param id
  4. * @return
  5. */
  6. @Override
  7. public CheckItem findById(int id) {
  8. return checkItemDao.findById(id);
  9. }
  10. /**
  11. * 更新
  12. * @param checkitem
  13. */
  14. @Override
  15. public void update(CheckItem checkitem) {
  16. checkItemDao.update(checkitem);
  17. }

4.2.4. Dao接口

在CheckItemDao接口中扩展edit方法

  1. /**
  2. * 通过id查询
  3. * @param id
  4. * @return
  5. */
  6. CheckItem findById(int id);
  7. /**
  8. * 更新检查项
  9. * @param checkitem
  10. */
  11. void update(CheckItem checkitem);

4.2.5. Mapper映射文件

在CheckItemDao.xml中扩展SQL语句

  1. <select id="findById" parameterType="int" resultType="checkitem">
  2. select * From t_checkitem where id=#{id}
  3. </select>
  4. <update id="update" parameterType="checkitem">
  5. update t_checkitem
  6. set
  7. code=#{code},
  8. name=#{name},
  9. sex=#{sex},
  10. age=#{age},
  11. price=#{price},
  12. type=#{type},
  13. remark=#{remark},
  14. attention=#{attention}
  15. where id=#{id}
  16. </update>

【小结】

  • 数据回显
    通过id查询,绑定到formData

  • 修改后提交
    跟添加类似, 表单校验,提交formData

注意:数据类型要与实体匹配, 价格的输入框的值必须是数字

5. 新增检查组

【需求】

检查组其实就是多个检查项的集合,例如有一个检查组为“一般检查”,这个检查组可以包括多个检查项:身高、体重、收缩压、舒张压等。所以在添加检查组时需要选择这个检查组包括的检查项。

检查组对应的实体类为CheckGroup,对应的数据表为t_checkgroup。检查组和检查项为多对多关系,所以需要中间表t_checkgroup_checkitem进行关联。

【目标】

新增检查组

【路径】

1:前台代码

  • 绑定新建按钮事件,弹出新增窗口,重置表单,请求检查项列表数据,绑定到tableData.
  • 绑定确定按钮,提交formData检查组信息, 选中的检查项checkitemIds集合, 提示操作的信息,成功则关闭新增窗口,刷新列表数据

2:后台代码

  • CheckGroupController 接收formData 用CheckGroup, checkitemIds=>用Integer[] 数组。调用业务添加,返回结果给页面
  • CheckGroupService

    • 调用Dao添加到检查组
    • 获取新添加的检查组id
    • 遍历checkitemIds, 调用dao添加检查组与检查项的关系
    • 添加事务控制
  • CheckGroupDao

    • 添加检查组 selectKey
    • 添加检查组与检查项的关系

【讲解】

5.1. 前台代码

检查组管理页面对应的是checkgroup.html页面,根据产品设计的原型已经完成了页面基本结构的编写,现在需要完善页面动态效果。

传智健康项目讲义(第2章) - 图3

5.1.1. 弹出新增窗口

页面中已经提供了新增窗口,只是出于隐藏状态。只需要将控制展示状态的属性dialogFormVisible改为true即可显示出新增窗口。点击新建按钮时绑定的方法为handleCreate,所以在handleCreate方法中修改dialogFormVisible属性的值为true即可。同时为了增加用户体验度,需要每次点击新建按钮时清空表单输入项。

由于新增检查组时还需要选择此检查组包含的检查项,所以新增检查组窗口分为两部分信息:基本信息和检查项信息,如下图:

传智健康项目讲义(第2章) - 图4

传智健康项目讲义(第2章) - 图5

(1)新建按钮绑定单击事件,对应的处理函数为handleCreate

  1. <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>

(2)handleCreate()方法

  1. // 重置表单
  2. resetForm() {
  3. // 清空表单
  4. this.formData = {};
  5. this.activeName='first'; //选中基本信息标签项
  6. // 清除钩选
  7. this.checkitemIds=[];
  8. },
  9. // 弹出添加窗口
  10. handleCreate() {
  11. //重置表单,弹出窗口
  12. this.resetForm();
  13. this.dialogFormVisible = true;
  14. },

5.1.2. 新增窗口中,动态展示检查项列表

现在虽然已经完成了新增窗口的弹出,但是在检查项信息标签页中需要动态展示所有的检查项信息列表数据,并且可以进行勾选。具体操作步骤如下:

(1)定义模型数据

  1. tableData:[],//新增和编辑表单中对应的检查项列表数据
  2. checkitemIds:[],//新增和编辑表单中检查项对应的复选框,基于双向绑定可以进行回显和数据提交,传递检查项id的数组

(2)动态展示检查项列表数据,数据来源于上面定义的tableData模型数据

  1. <el-tab-pane label="检查项信息" name="second">
  2. <div class="checkScrol">
  3. <table class="datatable">
  4. <thead>
  5. <tr>
  6. <th>选择</th>
  7. <th>项目编码</th>
  8. <th>项目名称</th>
  9. <th>项目说明</th>
  10. </tr>
  11. </thead>
  12. <tbody>
  13. <tr v-for="c in tableData">
  14. <td>
  15. <input :id="c.id" v-model="checkitemIds" type="checkbox" :value="c.id">
  16. </td>
  17. <td><label :for="c.id">{{c.code}}</label></td>
  18. <td><label :for="c.id">{{c.name}}</label></td>
  19. <td><label :for="c.id">{{c.remark}}</label></td>
  20. </tr>
  21. </tbody>
  22. </table>
  23. </div>
  24. </el-tab-pane>

(3)完善handleCreate方法,发送ajax请求查询所有检查项数据并将结果赋值给tableData模型数据用于页面表格展示

  1. // 弹出添加窗口
  2. handleCreate() {
  3. //重置表单,弹出窗口
  4. this.resetForm();
  5. this.dialogFormVisible = true;
  6. //发送请求后台获取所有检查项数据,得到后绑定tableData(检查项列表)
  7. axios.get('/checkitem/findAll.do').then(res => {
  8. if(res.data.flag){
  9. this.tableData = res.data.data;
  10. }else{
  11. this.$message.error(res.data.message);
  12. }
  13. })
  14. },

(4)分别在CheckItemController、CheckItemService、CheckItemServiceImpl、CheckItemDao、CheckItemDao.xml中扩展方法查询所有检查项数据

【1】:CheckItemController:

  1. @GetMapping("/findAll")
  2. public Result findAll(){
  3. // 调用服务查询所有的检查项
  4. List<CheckItem> list = checkItemService.findAll();
  5. // 封装返回的结果
  6. return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,list);
  7. }

【2】:CheckItemService:

  1. List<CheckItem> findAll();

【3】:CheckItemServiceImpl:

  1. public List<CheckItem> findAll() {
  2. return checkItemDao.findAll();
  3. }

【4】:CheckItemDao:

  1. List<CheckItem> findAll();

【5】:CheckItemDao.xml:

  1. <select id="findAll" resultType="CheckItem">
  2. select * from t_checkitem
  3. </select>

5.1.3. 提交请求,执行保存

当用户点击新增窗口中的确定按钮时发送ajax请求将数据提交到后台进行数据库操作。提交到后台的数据分为两部分:检查组基本信息(对应的模型数据为formData)和检查项id数组(对应的模型数据为checkitemIds)。

(1)为确定按钮绑定单击事件,对应的处理函数为handleAdd

  1. <el-button type="primary" @click="handleAdd()">确定</el-button>

(2)完善handleAdd方法

  1. //添加
  2. handleAdd () {
  3. //提交检查组信息this.formData, 选中的检查项id this.checkitemIds
  4. axios.post('/checkgroup/add.do?checkitemIds=' + this.checkitemIds, this.formData).then(res => {
  5. this.$message({
  6. message: res.data.message,
  7. type: res.data.flag?"success":"error"
  8. })
  9. if(res.data.flag){
  10. // 关闭窗口
  11. this.dialogFormVisible = false;
  12. // 刷新列表数据
  13. this.findPage();
  14. }
  15. })
  16. },

5.2. 后台代码

2.2.1. Controller

在health_web工程中创建CheckGroupController

  1. package com.itheima.health.controller;
  2. import com.alibaba.dubbo.config.annotation.Reference;
  3. import com.itheima.health.constant.MessageConstant;
  4. import com.itheima.health.entity.PageResult;
  5. import com.itheima.health.entity.QueryPageBean;
  6. import com.itheima.health.entity.Result;
  7. import com.itheima.health.pojo.CheckGroup;
  8. import com.itheima.health.service.CheckGroupService;
  9. import org.springframework.web.bind.annotation.PostMapping;
  10. import org.springframework.web.bind.annotation.RequestBody;
  11. import org.springframework.web.bind.annotation.RequestMapping;
  12. import org.springframework.web.bind.annotation.RestController;
  13. /**
  14. * Description: No Description
  15. * User: Eric
  16. */
  17. @RestController
  18. @RequestMapping("/checkgroup")
  19. public class CheckGroupController {
  20. @Reference
  21. private CheckGroupService checkGroupService;
  22. /**
  23. * 添加检查组
  24. * @param checkGroup
  25. * @param checkitemIds
  26. * @return
  27. */
  28. @PostMapping("/add")
  29. public Result add(@RequestBody CheckGroup checkGroup, Integer[] checkitemIds){
  30. // 调用业务服务
  31. checkGroupService.add(checkGroup, checkitemIds);
  32. // 响应结果
  33. return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
  34. }
  35. }

5.2.2. 服务接口

在health_interface工程中创建CheckGroupService接口

  1. package com.itheima.health.service;
  2. import com.itheima.health.entity.PageResult;
  3. import com.itheima.health.entity.QueryPageBean;
  4. import com.itheima.health.pojo.CheckGroup;
  5. /**
  6. * Description: No Description
  7. * User: Eric
  8. */
  9. public interface CheckGroupService {
  10. /**
  11. * 添加检查组
  12. * @param checkGroup
  13. * @param checkitemIds
  14. */
  15. void add(CheckGroup checkGroup, Integer[] checkitemIds);
  16. }

5.2.3. 服务实现类

在health_service工程中创建CheckGroupServiceImpl实现类

  1. package com.itheima.health.service.impl;
  2. import com.alibaba.dubbo.config.annotation.Service;
  3. import com.github.pagehelper.Page;
  4. import com.github.pagehelper.PageHelper;
  5. import com.itheima.health.dao.CheckGroupDao;
  6. import com.itheima.health.entity.PageResult;
  7. import com.itheima.health.entity.QueryPageBean;
  8. import com.itheima.health.pojo.CheckGroup;
  9. import com.itheima.health.service.CheckGroupService;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.transaction.annotation.Transactional;
  12. import org.springframework.util.StringUtils;
  13. /**
  14. * Description: No Description
  15. * User: Eric
  16. */
  17. @Service(interfaceClass = CheckGroupService.class)
  18. public class CheckGroupServiceImpl implements CheckGroupService {
  19. @Autowired
  20. private CheckGroupDao checkGroupDao;
  21. /**
  22. * 添加
  23. * @param checkGroup
  24. * @param checkitemIds
  25. */
  26. @Override
  27. @Transactional
  28. public void add(CheckGroup checkGroup, Integer[] checkitemIds) {
  29. // 添加检查组
  30. checkGroupDao.add(checkGroup);
  31. // 获取检查组的id
  32. Integer checkGroupId = checkGroup.getId();
  33. // 遍历检查项id, 添加检查组与检查项的关系
  34. if(null != checkitemIds){
  35. // 有钩选
  36. for (Integer checkitemId : checkitemIds) {
  37. //添加检查组与检查项的关系
  38. checkGroupDao.addCheckGroupCheckItem(checkGroupId, checkitemId);
  39. }
  40. }
  41. }
  42. }

2.4. Dao接口

在health_dao工程中创建CheckGroupDao接口

  1. package com.itheima.health.dao;
  2. import com.github.pagehelper.Page;
  3. import com.itheima.health.pojo.CheckGroup;
  4. import org.apache.ibatis.annotations.Param;
  5. /**
  6. * Description: No Description
  7. * User: Eric
  8. */
  9. public interface CheckGroupDao {
  10. /**
  11. * 添加检查组
  12. * @param checkGroup
  13. */
  14. void add(CheckGroup checkGroup);
  15. /**
  16. * 添加检查组与检查项的关系
  17. * @param checkGroupId 注意要取别名,类型相同
  18. * @param checkitemId
  19. */
  20. void addCheckGroupCheckItem(@Param("checkGroupId") Integer checkGroupId, @Param("checkitemId") Integer checkitemId);
  21. }

上面我们使用@Param区别两个参数类型相同的参数

5.2.5. Mapper映射文件

在health_dao工程中创建CheckGroupDao.xml映射文件,需要和CheckGroupDao接口在同一目录下

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.itheima.health.dao.CheckGroupDao">
  6. <insert id="add" parameterType="checkgroup">
  7. <selectKey resultType="int" order="AFTER" keyProperty="id">
  8. select last_insert_id()
  9. </selectKey>
  10. insert into t_checkgroup (code,name,helpCode,sex,remark,attention)
  11. values (#{code},#{name},#{helpCode},#{sex},#{remark},#{attention})
  12. </insert>
  13. <insert id="addCheckGroupCheckItem" parameterType="int">
  14. insert into t_checkgroup_checkitem (checkgroup_id, checkitem_id)
  15. values (#{checkGroupId}, #{checkitemId})
  16. </insert>
  17. </mapper>

【小结】

1:前台代码

(1)弹出新增窗口 this.dialogFormVisable=ture

  • 使用选项卡
  • 选项卡一:检查组信息 放到form保存及展示 检查组信息
  • 选项卡二:检查项列表,并提供复选框 供给用户来选择

(2)新增窗口中,动态展示检查项列表

  • 查询所有检查项,弹出 窗口时,加载检查项列表数据 绑定到this.tableData

(3)提交请求(检查组信息this.formData, 选中的检查项id this.checkitemIds),执行保存, 关闭窗口且刷新列表

2:后台代码

  1. Controller用@Requestbody(只能有一个)前端传过来form表单数据=>CheckGroup
  2. Integer[] 数组来接收选中的检查项id数组 (提交是字符串,后台拿到的是数组,springMVC)
  3. 业务服务中,对多数据操作,开启事务(注意)
  4. 插入数据后获取id使用 selectKey select last_insert_id()

优化: 添加检查组与检查项关系使用的for循环,效率比较,使用批量的方式提交

知识点:关系维护

  1. 基本信息,列表来钩选(关系), 只提交选中的id

6. 检查组查询、分页

【目标】

检查组查询、分页

【路径】

1:前台代码

(1)定义分页相关模型数据

(2)定义分页方法

(3)完善分页方法执行时机(点击“查询”,点击“分页”)

2:后台代码

执行

  • 检查组分页查询

(1)CheckGroupController.java(Controller)

(2)CheckGroupService.java(服务接口)

(3)CheckGroupServiceImpl.java(服务实现类)

(4)CheckGroupDao.java(Dao接口)

(5)CheckGroupDao.xml(Mapper映射文件)

【讲解】

6.1. 前台代码

6.1.1. 定义分页相关模型数据

  1. pagination: {//分页相关模型数据
  2. currentPage: 1,//当前页码
  3. pageSize:10,//每页显示的记录数
  4. total:0,//总记录数
  5. queryString:null//查询条件
  6. },
  7. dataList: [],//当前页要展示的分页列表数据

6.1.2. 定义分页方法

在页面中提供了findPage方法用于分页查询,为了能够在checkgroup.html页面加载后直接可以展示分页数据,可以在VUE提供的钩子函数created中调用findPage方法

  1. //钩子函数,VUE对象初始化完成后自动执行
  2. created() {
  3. this.findPage();
  4. },

findPage()方法。

  1. //分页查询
  2. findPage() {
  3. axios.post('/checkgroup/findPage.do',this.pagination).then(res => {
  4. if(res.data.flag){
  5. this.dataList = res.data.data.rows;
  6. this.pagination.total = res.data.data.total;
  7. } else{
  8. this.$message.error(res.data.message);
  9. }
  10. });
  11. },

6.1.3. 完善分页方法执行时机

除了在created钩子函数中调用findPage方法查询分页数据之外,当用户点击查询按钮或者点击分页条中的页码时也需要调用findPage方法重新发起查询请求。

(1)为查询按钮绑定单击事件,调用findPage方法

  1. <el-button @click="handleCurrentChange(1)" class="dalfBut">查询</el-button>

(2)为分页条组件绑定current-change事件,此事件是分页条组件自己定义的事件,当页码改变时触发,对应的处理函数为handleCurrentChange

  1. <div class="pagination-container">
  2. <el-pagination
  3. class="pagiantion"
  4. @current-change="handleCurrentChange"
  5. :current-page="pagination.currentPage"
  6. :page-size="pagination.pageSize"
  7. layout="total, prev, pager, next, jumper"
  8. :total="pagination.total">
  9. </el-pagination>
  10. </div>

(3)定义handleCurrentChange方法

  1. //切换页码
  2. handleCurrentChange(currentPage) {
  3. //currentPage为切换后的页码
  4. this.pagination.currentPage = currentPage;
  5. this.findPage();
  6. },

6.2. 后台代码

6.2.1. Controller

在CheckGroupController中增加分页查询方法

  1. /**
  2. * 分页条件查询
  3. */
  4. @PostMapping("/findPage")
  5. public Result findPage(@RequestBody QueryPageBean queryPageBean){
  6. // 调用业务查询
  7. PageResult<CheckGroup> pageResult = checkGroupService.findPage(queryPageBean);
  8. return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, pageResult);
  9. }

6.2.2. 服务接口

在CheckGroupService服务接口中扩展分页查询方法

  1. /**
  2. * 分页条件查询
  3. * @param queryPageBean
  4. * @return
  5. */
  6. PageResult<CheckGroup> findPage(QueryPageBean queryPageBean);

6.2.3. 服务实现类

在CheckGroupServiceImpl服务实现类中实现分页查询方法,基于Mybatis分页助手插件实现分页

  1. /**
  2. * 分页条件查询
  3. * @param queryPageBean
  4. * @return
  5. */
  6. @Override
  7. public PageResult<CheckGroup> findPage(QueryPageBean queryPageBean) {
  8. // 使用PageHelper.startPage
  9. PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
  10. // 有查询条件的处理, 模糊查询
  11. if(!StringUtils.isEmpty(queryPageBean.getQueryString())){
  12. // 拼接%
  13. queryPageBean.setQueryString("%" + queryPageBean.getQueryString()+ "%");
  14. }
  15. // 紧接着的查询会被分页
  16. Page<CheckGroup> page = checkGroupDao.findByCondition(queryPageBean.getQueryString());
  17. return new PageResult<CheckGroup>(page.getTotal(), page.getResult());
  18. }

6.2.4. Dao接口

在CheckGroupDao接口中扩展分页查询方法

  1. Page<CheckGroup> findByCondition(String queryString);

6.2.5. Mapper映射文件

在CheckGroupDao.xml文件中增加SQL定义

  1. <select id="findByCondition" resultType="checkgroup" parameterType="String">
  2. select * From t_checkgroup
  3. <if test="value !=null and value.length > 0">
  4. where code like #{value} or name like #{value} or helpCode like #{value}
  5. </if>
  6. </select>

【小结】

1:前台代码

(1)定义分页相关模型数据

(2)定义分页方法

findPage()方法。

  1. //分页查询
  2. findPage() {
  3. axios.post('/checkgroup/findPage.do',this.pagination).then(res => {
  4. if(res.data.flag){
  5. this.dataList = res.data.data.rows;
  6. this.pagination.total = res.data.data.total;
  7. } else{
  8. this.$message.error(res.data.message);
  9. }
  10. });
  11. },

(3)完善分页方法执行时机(点击“查询”,点击“分页”)

2:后台代码

执行

  • 检查组分页查询

(1)CheckGroupController.java(Controller)

(2)CheckGroupService.java(服务接口)

(3)CheckGroupServiceImpl.java(服务实现类)

(4)CheckGroupDao.java(Dao接口)

(5)CheckGroupDao.xml(Mapper映射文件)

7. 编辑检查组

【目标】

编辑检查组

【路径】

  1. 回显

    • 【编辑】按钮,弹出编辑窗口,表单初始化,获取编辑的id,发送请求获取检查组信息,返回的检查组绑formData
    • 获取检查项列表,绑定到tableData, 发送请求获取选中的检查项ids 绑定到checkitemIds,钩选中
    • CheckGroupController

      • 提交findById
      • 通过检查组id查询选中的检查项id集合 List
    • CheckGroupService

      • findById
      • 通过检查组id查询选中的检查项id集合 List
    • CheckGroupDao

      • findById
      • 通过检查组id查询选中的检查项id集合 List
  2. 提交修改

    • 【确定】发送请求提交formData, checkitemIds。提示操作结果,成功则要关闭编辑窗口,刷新列表数据
    • CheckGroupController CheckGroup接收formData, Integer[] 接收checkItemIds,调用服务修改,返回结果给页面
    • CheckGroupService

      • 更新检查组
      • 先删除旧关系
      • 再添加新关系
      • 事务控制
  • CheckGroupDao与映射

    • update
    • deleteCheckGroupCheckItem t_checkgroup_checkitem

【讲解】

7.1. 前台页面

用户点击编辑按钮时,需要弹出编辑窗口并且将当前记录的数据进行回显,用户修改完成后点击确定按钮将修改后的数据提交到后台进行数据库操作。此处进行数据回显的时候,除了需要检查组基本信息的回显之外,还需要回显当前检查组包含的检查项(以复选框勾选的形式回显)。

7.1.1. 绑定单击事件

(1)需要为编辑按钮绑定单击事件,并且将当前行数据作为参数传递给处理函数

  1. <el-table-column label="操作" align="center">
  2. <template slot-scope="scope">
  3. <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
  4. <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
  5. </template>
  6. </el-table-column>

(2)handleUpdate事件

  1. // 弹出编辑窗口
  2. handleUpdate(row) {
  3. alert(row.id);
  4. },

7.1.2. 弹出编辑窗口回显数据

当前页面的编辑窗口已经提供好了,默认处于隐藏状态。在handleUpdate方法中需要将编辑窗口展示出来,并且需要发送多个ajax请求分别查询当前检查组数据、所有检查项数据、当前检查组包含的检查项id用于基本数据回显

  1. // 重置表单
  2. resetForm() {
  3. // 清空表单
  4. this.formData = {};
  5. this.activeName='first'; //选中基本信息标签项
  6. // 清除钩选
  7. this.checkitemIds=[];
  8. },
  1. // 弹出编辑窗口
  2. handleUpdate(row) {
  3. this.resetForm();
  4. // 弹出编辑窗口
  5. this.dialogFormVisible4Edit = true;
  6. // 获取检查组的id
  7. var checkGroupId = row.id;
  8. axios.get("/checkgroup/findById.do?checkGroupId=" + checkGroupId).then(res =>{
  9. if(res.data.flag){
  10. // 成功绑定数据
  11. this.formData = res.data.data;
  12. //发送请求后台获取所有检查项数据,得到后绑定tableData(检查项列表)
  13. axios.get('/checkitem/findAll.do').then(resp => {
  14. if(resp.data.flag){
  15. this.tableData = resp.data.data;
  16. // 获取选中的检查项id, 回显数据时,检查项列表要钩选中
  17. axios.get("/checkgroup/findCheckItemIdsByCheckGroupId.do?checkGroupId=" + checkGroupId).then(response=>{
  18. // 后台要返回data必须为List
  19. if(response.data.flag){
  20. this.checkitemIds = response.data.data;
  21. }else{
  22. this.$message.error(response.data.message);
  23. }
  24. })
  25. }else{
  26. this.$message.error(resp.data.message);
  27. }
  28. })
  29. }else{
  30. this.$message.error(res.data.message);
  31. }
  32. })
  33. },

7.1.3. 发送请求,编辑保存检查组

(1)在编辑窗口中修改完成后,点击确定按钮需要提交请求,所以需要为确定按钮绑定事件并提供处理函数handleEdit

  1. <el-button type="primary" @click="handleEdit()">确定</el-button>

(2)handleEdit()方法

  1. //编辑 修改后提交
  2. handleEdit() {
  3. //提交检查组信息this.formData, 选中的检查项id this.checkitemIds
  4. axios.post('/checkgroup/update.do?checkitemIds=' + this.checkitemIds, this.formData).then(res => {
  5. this.$message({
  6. message: res.data.message,
  7. type: res.data.flag?"success":"error"
  8. })
  9. if(res.data.flag){
  10. // 关闭编辑窗口
  11. this.dialogFormVisible4Edit = false;
  12. // 刷新列表数据
  13. this.findPage();
  14. }
  15. })
  16. },

7.2. 后台代码

7.2.1. Controller

在CheckGroupController中增加方法

  1. /**
  2. * 通过id获取检查组
  3. */
  4. @GetMapping("/findById")
  5. public Result findById(int checkGroupId){
  6. // 调用业务服务
  7. CheckGroup checkGroup = checkGroupService.findById(checkGroupId);
  8. return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroup);
  9. }
  10. /**
  11. * 通过检查组id查询选中的检查项id
  12. */
  13. @GetMapping("/findCheckItemIdsByCheckGroupId")
  14. public Result findCheckItemIdsByCheckGroupId(int checkGroupId){
  15. // 调用服务查询
  16. List<Integer> checkItemIds = checkGroupService.findCheckItemIdsByCheckGroupId(checkGroupId);
  17. return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItemIds);
  18. }
  19. /**
  20. * 修改提交
  21. */
  22. @PostMapping("/update")
  23. public Result update(@RequestBody CheckGroup checkGroup, Integer[] checkitemIds){
  24. // 调用业务服务
  25. checkGroupService.update(checkGroup, checkitemIds);
  26. // 响应结果
  27. return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
  28. }

7.2.2. 服务接口

在CheckGroupService服务接口中扩展方法

  1. /**
  2. * 通过检查组id查询选中的检查项id
  3. * @param checkGroupId
  4. * @return
  5. */
  6. List<Integer> findCheckItemIdsByCheckGroupId(int checkGroupId);
  7. /**
  8. * 通过id获取检查组
  9. * @param checkGroupId
  10. * @return
  11. */
  12. CheckGroup findById(int checkGroupId);
  13. /**
  14. * 修改检查组
  15. * @param checkGroup
  16. * @param checkitemIds
  17. */
  18. void update(CheckGroup checkGroup, Integer[] checkitemIds);

7.2.3. 服务实现类

在CheckGroupServiceImpl实现类中实现编辑方法

  1. /**
  2. * 通过检查组id查询选中的检查项id
  3. * @param checkGroupId
  4. * @return
  5. */
  6. @Override
  7. public List<Integer> findCheckItemIdsByCheckGroupId(int checkGroupId) {
  8. return checkGroupDao.findCheckItemIdsByCheckGroupId(checkGroupId);
  9. }
  10. /**
  11. * 通过id获取检查组
  12. * @param checkGroupId
  13. * @return
  14. */
  15. @Override
  16. public CheckGroup findById(int checkGroupId) {
  17. return checkGroupDao.findById(checkGroupId);
  18. }
  19. /**
  20. * 修改检查组
  21. * @param checkGroup
  22. * @param checkitemIds
  23. */
  24. @Override
  25. @Transactional
  26. public void update(CheckGroup checkGroup, Integer[] checkitemIds) {
  27. // 先更新检查组
  28. checkGroupDao.update(checkGroup);
  29. // 删除旧关系
  30. checkGroupDao.deleteCheckGroupCheckItem(checkGroup.getId());
  31. // 建立新关系
  32. if(null != checkitemIds){
  33. for (Integer checkitemId : checkitemIds) {
  34. checkGroupDao.addCheckGroupCheckItem(checkGroup.getId(), checkitemId);
  35. }
  36. }
  37. }

7.2.4. Dao接口

在CheckGroupDao接口中扩展方法

  1. /**
  2. * 通过检查组id查询选中的检查项id
  3. * @param checkGroupId
  4. * @return
  5. */
  6. List<Integer> findCheckItemIdsByCheckGroupId(int checkGroupId);
  7. /**
  8. * 通过id获取检查组
  9. * @param checkGroupId
  10. * @return
  11. */
  12. CheckGroup findById(int checkGroupId);
  13. /**
  14. * 更新检查组
  15. * @param checkGroup
  16. */
  17. void update(CheckGroup checkGroup);
  18. /**
  19. * 删除检查组与检查项的关系
  20. * @param id
  21. */
  22. void deleteCheckGroupCheckItem(Integer id);

7.2.5. Mapper映射文件

在CheckGroupDao.xml中扩展SQL语句

  1. <select id="findCheckItemIdsByCheckGroupId" parameterType="int" resultType="int">
  2. select checkitem_id from t_checkgroup_checkitem where checkgroup_id=#{checkGroupId}
  3. </select>
  4. <select id="findById" parameterType="int" resultType="checkgroup">
  5. select * From t_checkgroup where id=#{checkGroupId}
  6. </select>
  7. <update id="update" parameterType="checkgroup">
  8. update t_checkgroup
  9. set
  10. code=#{code},
  11. name=#{name},
  12. helpCode=#{helpCode},
  13. sex=#{sex},
  14. remark=#{remark},
  15. attention=#{attention}
  16. where id=#{id}
  17. </update>
  18. <delete id="deleteCheckGroupCheckItem" parameterType="int">
  19. delete from t_checkgroup_checkitem where checkgroup_id=#{id}
  20. </delete>

【小结】

  1. 回显数据,除了要回显检查组信息外,还要去查检查项列表,查询选中的检查项id集合
    默认钩选, 把id的集合绑定this.checkitemIds

  2. 关系维护前端

    1. <table class="datatable">
    2. <thead>
    3. <tr>
    4. <th>选择</th>
    5. <th>项目编码</th>
    6. <th>项目名称</th>
    7. <th>项目说明</th>
    8. </tr>
    9. </thead>
    10. <tbody>
    11. <tr v-for="c in tableData">
    12. <td>
    13. <input :id="c.id" v-model="checkitemIds" type="checkbox" :value="c.id">
    14. </td>
    15. <td><label :for="c.id">{{c.code}}</label></td>
    16. <td><label :for="c.id">{{c.name}}</label></td>
    17. <td><label :for="c.id">{{c.remark}}</label></td>
    18. </tr>
    19. </tbody>
    20. </table>
  1. 提交,对已经关系维护(多对多),先删除再添加新关系

  2. 业务层如果多种修改数据库的操作,记得要加事务管理@Transactional打在方法上

8. 删除检查组

【目标】

删除检查组

【路径】

1:前台代码

  • 【删除】编辑单击事件,弹出询问窗口,确定后提交检查组id,提示操作结果,如果成功则刷新列表数据

2:后台代码

  • CheckGroupController接收id,调用服务删除,返回结果给页面

  • CheckGroupService 事务

    • 判断检查组是否被套餐使用了
    • 被使用, 报错
    • 没被使用

      • 先解散,删除检查组与检查项的关系
      • 再来删除检查组
  • CheckGroupDao

    • 通过检查组id查询套餐与检查组的关系的个数
    • 通过检查组id删除检查组与检查项的关系
    • 删除检查组

【讲解】

3.1. 前台代码

为了防止用户误操作,点击删除按钮时需要弹出确认删除的提示,用户点击取消则不做任何操作,用户点击确定按钮再提交删除请求。

3.1.1. 绑定单击事件

需要为删除按钮绑定单击事件,并且将当前行数据作为参数传递给处理函数

  1. <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>

调用的方法

  1. // 删除
  2. handleDelete(row) {
  3. alert(row.id);
  4. }

3.1.2. 弹出确认操作 发送删除请求

用户点击删除按钮会执行handleDelete方法,此处需要完善handleDelete方法,弹出确认提示信息。ElementUI提供了$confirm方法来实现确认提示信息弹框效果,如果用户点击确定按钮就需要发送ajax请求,并且将当前检查组的id作为参数提交到后台进行删除操作

  1. // 删除
  2. handleDelete(row) {
  3. // 获取删除的id
  4. var id = row.id;
  5. //alert(JSON.stringify(row));
  6. this.$confirm('此操作将【永久删除】该检查组, 是否继续?', '提示', {
  7. confirmButtonText: '确定',
  8. cancelButtonText: '取消',
  9. type: 'warning'
  10. }).then(() => {
  11. // 点击确定后调用
  12. axios.post('/checkgroup/deleteById.do?id=' + id).then(res=>{
  13. this.$message({
  14. message: res.data.message,
  15. type: res.data.flag?"success":"error"
  16. })
  17. if(res.data.flag){
  18. // 成功
  19. // 刷新列表数据
  20. this.findPage();
  21. }
  22. })
  23. }).catch(() => {
  24. // 点击取消后调用
  25. // 空着,防止报错
  26. });
  27. }

3.2. 后台代码

3.2.1. Controller

在CheckGroupController中增加删除方法

  1. /**
  2. * 删除检查组
  3. * @param id
  4. * @return
  5. */
  6. @PostMapping("/deleteById")
  7. public Result deleteById(int id){
  8. //调用业务服务删除
  9. checkGroupService.deleteById(id);
  10. return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
  11. }

3.2.2. 服务接口

在CheckGroupService服务接口中扩展删除方法

  1. /**
  2. * 删除检查组
  3. * @param id
  4. */
  5. void deleteById(int id) throws HealthException;

3.2.3. 服务实现类

注意:不能直接删除,需要判断当前检查组是否和检查项、套餐关联,如果已经和检查项、套餐进行了关联则不允许删除

  1. /**
  2. * 删除检查组
  3. * @param id
  4. */
  5. @Override
  6. @Transactional
  7. public void deleteById(int id) {
  8. // 检查 这个检查组是否被套餐使用了
  9. int count = checkGroupDao.findSetmealCountByCheckGroupId(id);
  10. if(count > 0){
  11. // 被使用了
  12. throw new HealthException(MessageConstant.CHECKGROUP_IN_USE);
  13. }
  14. // 没有被套餐使用,就可以删除数据
  15. // 先删除检查组与检查项的关系
  16. checkGroupDao.deleteCheckGroupCheckItem(id);
  17. // 删除检查组
  18. checkGroupDao.deleteById(id);
  19. }

3.2.4. Dao接口

在CheckGroupDao接口中扩展方法findSetmealAndCheckGroupCountByCheckGroupId和deleteById

  1. /**
  2. * 通过检查组id查询是否被套餐使用了
  3. * @param id
  4. * @return
  5. */
  6. int findSetmealCountByCheckGroupId(int id);
  7. /**
  8. * 删除检查组
  9. * @param id
  10. */
  11. void deleteById(int id);

3.2.5. Mapper映射文件

在CheckGroupDao.xml中扩展SQL语句

  1. <select id="findSetmealCountByCheckGroupId" parameterType="int" resultType="int">
  2. select count(1) from t_setmeal_checkgroup where checkgroup_id=#{id}
  3. </select>
  4. <delete id="deleteById" parameterType="int">
  5. delete from t_checkgroup where id=#{id}
  6. </delete>

【小结】

  1. 删除前要判断是否被套餐使用了,业务关系使然,业务: 1个套餐下多个检查组,1个检查组下多个检查项
  2. 删除主表数据(t_checkgroup),就先删除从表(t_checkgroup_checkitem)数据
  3. 注意事务,抛出的异常一定要在接口中声明,异常类名称不要写成HandlerException

事务问题

AnnotationBean.postProcessAfterInitialization 在dubbo包下的

  1. 开启事务控制的注解支持
  2. 注意:此处必须加入proxy-target-class="true"
  3. 需要进行事务控制,会由Spring框架产生代理对象,
  4. Dubbo需要将Service发布为服务,要求必须使用cglib创建代理对象。
  5. 如果没 proxy-target-class="true", 业务实现类/方法加上@Transaction,类创建的方式为jdk动态代理,发布服务时,类名与接口包名不一致,所以不发布服务
  6. 加上proxy-target-class="true" 业务实现类/方法加上@Transaction 类的创建方式为SpringProxy(CGLIB), @Service,看有接口申明(interfaceClass=)
  7. 如果有接口声明,则发布的服务接口为声明的接口,
  8. 如果没有的情况下则发布的服务接口为org.springframework.aop.SpringProxy
  9. 那么服务的消费者(controller 找的是业务服务接口)没有提供者
  10. 解决事务导致找不到服务的问题
  11. proxy-target-class="true" 同时service实现类上加上@Service(interfaceClass=接口类字节码)

传智健康项目讲义(第2章) - 图6

传智健康项目讲义(第2章) - 图7