一、需求分析
传智健康管理系统是应用于健康管理机构的业务系统,实现健康管理机构工作内容可视化、患者管理专业化、健康评估数字化、健康干预流程化、知识库集成化,从而提高健康管理师的工作效率,加强与患者间的互动,增强管理者对健康管理机构运营情况的了解。 系统分为传智健康后台管理系统和移动端应用两部分。其中后台系统提供给健康管理机构内部人员(包括系统管理员、健康管理师等)使用,微信端应用提供给健康管理机构的用户(体检用户)使用。
二、基础环境搭建
2.1 导入预约管理模块数据表
根据项目的pdm文件在PowerDesigner中使用逆向工程导出为SQL脚本,这里直接给出对应的SQL脚本内容。
drop table if exists t_checkgroup;
drop table if exists t_checkgroup_checkitem;
drop table if exists t_checkitem;
drop table if exists t_member;
drop table if exists t_menu;
drop table if exists t_order;
drop table if exists t_ordersetting;
drop table if exists t_permission;
drop table if exists t_role;
drop table if exists t_role_menu;
drop table if exists t_role_permission;
drop table if exists t_setmeal;
drop table if exists t_setmeal_checkgroup;
drop table if exists t_user;
drop table if exists t_user_role;
create table t_checkgroup
(
id int(11) not null auto_increment,
code varchar(32) default NULL,
name varchar(32) default NULL,
helpCode varchar(32) default NULL,
sex char(1) default NULL,
remark varchar(128) default NULL,
attention varchar(128) default NULL,
primary key (id)
);
alter table t_checkgroup comment '体检检查组';
create table t_checkgroup_checkitem
(
checkgroup_id int(11) not null default 0,
checkitem_id int(11) not null default 0,
primary key (checkgroup_id, checkitem_id)
);
create table t_checkitem
(
id int(11) not null auto_increment,
code varchar(16) default NULL,
name varchar(32) default NULL,
sex char(1) default NULL,
age varchar(32) default NULL,
price float default NULL,
type char(1) default NULL comment '查检项类型,分为检查和检验两种',
attention varchar(128) default NULL,
remark varchar(128) default NULL,
primary key (id)
);
alter table t_checkitem comment '体检检查项';
create table t_member
(
id int(11) not null auto_increment,
fileNumber varchar(32) default NULL,
name varchar(32) default NULL,
sex varchar(8) default NULL,
idCard varchar(18) default NULL,
phoneNumber varchar(11) default NULL,
regTime date default NULL,
password varchar(32) default NULL,
email varchar(32) default NULL,
birthday date default NULL,
remark varchar(128) default NULL,
primary key (id)
);
create table t_menu
(
id int not null auto_increment,
name varchar(128),
linkUrl varchar(128),
path varchar(128),
priority int,
description varchar(128),
parentMenuId int,
icon varchar(64),
level int,
primary key (id)
);
create table t_order
(
id int(11) not null auto_increment,
member_id int(11) default NULL comment '员会id',
orderDate date default NULL comment '约预日期',
orderType varchar(8) default NULL comment '约预类型 电话预约/微信预约',
orderStatus varchar(8) default NULL comment '预约状态(是否到诊)',
setmeal_id int(11) default NULL comment '餐套id',
primary key (id)
);
create table t_ordersetting
(
id int(11) not null auto_increment,
orderDate date default NULL comment '约预日期',
number int(11) default NULL comment '可预约人数',
reservations int(11) default NULL comment '已预约人数',
primary key (id)
);
alter table t_ordersetting comment '预约设置';
create table t_permission
(
id int not null auto_increment,
name varchar(32),
keyword varchar(64),
description varchar(128),
primary key (id)
);
create table t_role
(
id int not null auto_increment,
name varchar(32),
keyword varchar(64),
description varchar(128),
primary key (id)
);
create table t_role_menu
(
role_id int not null,
menu_id int not null,
primary key (role_id, menu_id)
);
create table t_role_permission
(
role_id int not null,
permission_id int not null,
primary key (role_id, permission_id)
);
create table t_setmeal
(
id int(11) not null auto_increment,
name varchar(128) default NULL,
code varchar(8) default NULL,
helpCode varchar(16) default NULL,
sex char(1) default NULL,
age varchar(32) default NULL,
price float default NULL,
remark varchar(128) default NULL,
attention varchar(128) default NULL,
img varchar(128) default NULL,
primary key (id)
);
alter table t_setmeal comment '体检套餐';
create table t_setmeal_checkgroup
(
setmeal_id int(11) not null default 0,
checkgroup_id int(11) not null default 0,
primary key (setmeal_id, checkgroup_id)
);
create table t_user
(
id int not null auto_increment,
birthday date,
gender varchar(1),
username varchar(32),
password varchar(36),
remark varchar(32),
station varchar(1),
telephone varchar(11),
primary key (id)
);
create table t_user_role
(
user_id int not null,
role_id int not null,
primary key (user_id, role_id)
);
alter table t_checkgroup_checkitem add constraint FK_Reference_5 foreign key (checkitem_id)
references t_checkitem (id) on delete restrict on update restrict;
alter table t_checkgroup_checkitem add constraint group_id foreign key (checkgroup_id)
references t_checkgroup (id);
alter table t_menu add constraint FK_Reference_13 foreign key (parentMenuId)
references t_menu (id) on delete restrict on update restrict;
alter table t_order add constraint FK_Reference_6 foreign key (setmeal_id)
references t_setmeal (id) on delete restrict on update restrict;
alter table t_order add constraint key_member_id foreign key (member_id)
references t_member (id);
alter table t_role_menu add constraint FK_Reference_10 foreign key (menu_id)
references t_menu (id) on delete restrict on update restrict;
alter table t_role_menu add constraint FK_Reference_9 foreign key (role_id)
references t_role (id) on delete restrict on update restrict;
alter table t_role_permission add constraint FK_Reference_11 foreign key (role_id)
references t_role (id) on delete restrict on update restrict;
alter table t_role_permission add constraint FK_Reference_12 foreign key (permission_id)
references t_permission (id) on delete restrict on update restrict;
alter table t_setmeal_checkgroup add constraint checkgroup_key foreign key (checkgroup_id)
references t_checkgroup (id);
alter table t_setmeal_checkgroup add constraint setmeal_key foreign key (setmeal_id)
references t_setmeal (id);
alter table t_user_role add constraint FK_Reference_7 foreign key (user_id)
references t_user (id) on delete restrict on update restrict;
alter table t_user_role add constraint FK_Reference_8 foreign key (role_id)
references t_role (id) on delete restrict on update restrict;
2.2 导入预约管理模块的实体类
将POJO实体类复制到health_common工程中
2.3 导入项目所需的公共资源
项目开发过程中会提供公共资源,供多个模块或者系统来使用。 今天导入的公共资源有: (1) 返回消息常量类MessageConstant,放到health_common工程中。该消息常量类的功能是,将后台操作的结果信息返回给前台,用于告诉用户操作的结果。
package com.itheima.constant;
public class MessageConstant {
public static final String DELETE_CHECKITEM_FAIL = "删除检查项失败";
public static final String DELETE_CHECKITEM_SUCCESS = "删除检查项成功";
public static final String ADD_CHECKITEM_SUCCESS = "新增检查项成功";
public static final String ADD_CHECKITEM_FAIL = "新增检查项失败";
public static final String EDIT_CHECKITEM_FAIL = "编辑检查项失败";
public static final String EDIT_CHECKITEM_SUCCESS = "编辑检查项成功";
public static final String QUERY_CHECKITEM_SUCCESS = "查询检查项成功";
public static final String QUERY_CHECKITEM_FAIL = "查询检查项失败";
public static final String UPLOAD_SUCCESS = "上传成功";
public static final String ADD_CHECKGROUP_FAIL = "新增检查组失败";
public static final String ADD_CHECKGROUP_SUCCESS = "新增检查组成功";
public static final String DELETE_CHECKGROUP_FAIL = "删除检查组失败";
public static final String DELETE_CHECKGROUP_SUCCESS = "删除检查组成功";
public static final String QUERY_CHECKGROUP_SUCCESS = "查询检查组成功";
public static final String QUERY_CHECKGROUP_FAIL = "查询检查组失败";
public static final String EDIT_CHECKGROUP_FAIL = "编辑检查组失败";
public static final String EDIT_CHECKGROUP_SUCCESS = "编辑检查组成功";
public static final String PIC_UPLOAD_SUCCESS = "图片上传成功";
public static final String PIC_UPLOAD_FAIL = "图片上传失败";
public static final String ADD_SETMEAL_FAIL = "新增套餐失败";
public static final String ADD_SETMEAL_SUCCESS = "新增套餐成功";
public static final String IMPORT_ORDERSETTING_FAIL = "批量导入预约设置数据失败";
public static final String IMPORT_ORDERSETTING_SUCCESS = "批量导入预约设置数据成功";
public static final String GET_ORDERSETTING_SUCCESS = "获取预约设置数据成功";
public static final String GET_ORDERSETTING_FAIL = "获取预约设置数据失败";
public static final String ORDERSETTING_SUCCESS = "预约设置成功";
public static final String ORDERSETTING_FAIL = "预约设置失败";
public static final String ADD_MEMBER_FAIL = "新增会员失败";
public static final String ADD_MEMBER_SUCCESS = "新增会员成功";
public static final String DELETE_MEMBER_FAIL = "删除会员失败";
public static final String DELETE_MEMBER_SUCCESS = "删除会员成功";
public static final String EDIT_MEMBER_FAIL = "编辑会员失败";
public static final String EDIT_MEMBER_SUCCESS = "编辑会员成功";
public static final String TELEPHONE_VALIDATECODE_NOTNULL = "手机号和验证码都不能为空";
public static final String LOGIN_SUCCESS = "登录成功";
public static final String VALIDATECODE_ERROR = "验证码输入错误";
public static final String QUERY_ORDER_SUCCESS = "查询预约信息成功";
public static final String QUERY_ORDER_FAIL = "查询预约信息失败";
public static final String QUERY_SETMEALLIST_SUCCESS = "查询套餐列表数据成功";
public static final String QUERY_SETMEALLIST_FAIL = "查询套餐列表数据失败";
public static final String QUERY_SETMEAL_SUCCESS = "查询套餐数据成功";
public static final String QUERY_SETMEAL_FAIL = "查询套餐数据失败";
public static final String SEND_VALIDATECODE_FAIL = "验证码发送失败";
public static final String SEND_VALIDATECODE_SUCCESS = "验证码发送成功";
public static final String SELECTED_DATE_CANNOT_ORDER = "所选日期不能进行体检预约";
public static final String ORDER_FULL = "预约已满";
public static final String HAS_ORDERED = "已经完成预约,不能重复预约";
public static final String ORDER_SUCCESS = "预约成功";
public static final String GET_USERNAME_SUCCESS = "获取当前登录用户名称成功";
public static final String GET_USERNAME_FAIL = "获取当前登录用户名称失败";
public static final String GET_MENU_SUCCESS = "获取当前登录用户菜单成功";
public static final String GET_MENU_FAIL = "获取当前登录用户菜单失败";
public static final String GET_MEMBER_NUMBER_REPORT_SUCCESS = "获取会员统计数据成功";
public static final String GET_MEMBER_NUMBER_REPORT_FAIL = "获取会员统计数据失败";
public static final String GET_SETMEAL_COUNT_REPORT_SUCCESS = "获取套餐统计数据成功";
public static final String GET_SETMEAL_COUNT_REPORT_FAIL = "获取套餐统计数据失败";
public static final String GET_BUSINESS_REPORT_SUCCESS = "获取运营统计数据成功";
public static final String GET_BUSINESS_REPORT_FAIL = "获取运营统计数据失败";
public static final String GET_SETMEAL_LIST_SUCCESS = "查询套餐列表数据成功";
public static final String GET_SETMEAL_LIST_FAIL = "查询套餐列表数据失败";
}
(2) 返回结果Result和PageResult类,放到health_common工程中 Result:
package com.itheima.entity;
import java.io.Serializable;
public class Result implements Serializable{
private boolean flag;
private String message;
private Object data;
public Result(boolean flag, String message) {
super();
this.flag = flag;
this.message = message;
}
public Result(boolean flag, String message, Object data) {
this.flag = flag;
this.message = message;
this.data = data;
}
public boolean isFlag() {
return flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
}
PageResult:
package com.itheima.entity;
import java.io.Serializable;
import java.util.List;
public class PageResult implements Serializable{
private Long total;
private List rows;
public PageResult(Long total, List rows) {
super();
this.total = total;
this.rows = rows;
}
public Long getTotal() {
return total;
}
public void setTotal(Long total) {
this.total = total;
}
public List getRows() {
return rows;
}
public void setRows(List rows) {
this.rows = rows;
}
}
(3) 封装查询条件的QueryPageBean类,放到health_common工程中
package com.itheima.entity;
import java.io.Serializable;
public class QueryPageBean implements Serializable{
private Integer currentPage;
private Integer pageSize;
private String queryString;
public Integer getCurrentPage() {
return currentPage;
}
public void setCurrentPage(Integer currentPage) {
this.currentPage = currentPage;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
public String getQueryString() {
return queryString;
}
public void setQueryString(String queryString) {
this.queryString = queryString;
}
}
(4) html、js、css、图片等静态资源,放到health_backend工程中,随着后续的开发,还会陆续导入其它的一些公共资源。
三、新增检查项
3.1 完善页面
检查项管理页面对应的是checkitem.html页面,只需在静态页面的基础上,完善前后台交互的前端代码即可。
3.1.1 弹出新增窗口
页面中已经提供了新增窗口,只是处于隐藏状态。只需要将控制展示状态的属性dialogFormVisible改为 true就可以显示出新增窗口。 新建按钮绑定的方法为handleCreate,所以在handleCreate方法中修改dialogFormVisible属性的值为 true即可。同时为了增加用户体验度,需要每次点击新建按钮时清空表单输入项。
handleCreate() {
this.dialogFormVisible = true;
this.resetForm();
}
resetForm() {
this.formData = {};
if (this.$refs['dataAddForm'] != undefined) {
this.$refs['dataAddForm'].resetFields();
}
if (this.$refs['dataEditForm'] != undefined) {
this.$refs['dataEditForm'].resetFields();
}
},
3.1.2 输入校验
rules: {
code: [{ required: true, message: '项目编码为必填项', trigger: 'blur' }],
name: [{ required: true, message: '项目名称为必填项', trigger: 'blur' }]
}
3.1.3 提交表单数据
点击新增窗口中的确定按钮时,触发handleAdd方法,所以需要在handleAdd方法中进行完善。
handleAdd () {
this.$refs['dataAddForm'].validate((valid) => {
if (valid) {
axios.post('/checkitem/add.do', this.formData).then(res => {
if (res.data.flag) {
this.findPage();
this.$message({
message: res.data.message,
type: 'success'
});
this.dialogFormVisible = false;
} else {
this.$message.error(res.data.message);
}
});
} else {
this.$message.error("数据校验失败,请检查你的输入信息是否正确!");
return false;
}
});
}
3.2 后台代码
3.2.1 Controller
在health_backend工程中创建CheckItemController
package com.itheima.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/checkitem")
public class CheckItemController {
@Reference
private CheckItemService checkItemService;
@PostMapping("/add")
public Result add(@RequestBody CheckItem checkItem) {
try {
checkItemService.add(checkItem);
} catch (Exception e) {
e.printStackTrace();
return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
}
return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
}
}
3.2.2 服务接口
在health_interface工程中创建CheckItemService接口
package com.itheima.service;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import java.util.List;
public interface CheckItemService {
public void add(CheckItem checkItem) throws Exception;
}
3.2.3 服务实习类
在health_service_provider工程中创建CheckItemServiceImpl实习类
package com.itheima.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckItemDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService {
@Autowired
private CheckItemDao checkItemDao;
@Override
public void add(CheckItem checkItem) throws Exception {
checkItemDao.add(checkItem);
}
}
3.2.4 Dao接口
在health_service_provider工程中创建CheckItemDao接口,本项目是基于Mybatis的Mapper代理技术实现持久层操作,故只需提供接口和Mapper映射文件,无需提供实习类
package com.itheima.dao;
import com.github.pagehelper.Page;
import com.itheima.pojo.CheckItem;
public interface CheckItemDao {
public void add(CheckItem checkItem) throws Exception;
3.2.5 Mapper映射文件
在health_service_provider工程中创建CheckItemDao.xml映射文件,需要和CheckItemDao接口在同一目录下
<?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="com.itheima.dao.CheckItemDao">
<insert id="add" parameterType="com.itheima.pojo.CheckItem">
insert into t_checkitem(code, name, sex, age, price, type, attention, remark)
values(#{code}, #{name}, #{sex}, #{age}, #{price}, #{type}, #{attention}, #{remark})
</insert>
</mapper>
四、检查项分页
本项目所有分页功能都是基于ajax的异步请求来完成的,请求参数和后台响应数据都使用json数据格式。 请求参数包括页码、每页显示的记录数、查询条件。 请求参数的json格式为:{currentPage:1, pageSize:10, queryString:’’} 后台响应数据包括总记录数,当前页需要展示的数据集合。 响应数据的json格式为:{total:1000, rows:[]} 如下图:其中ajax请求携带请求参数的json格式,响应携带响应数据的json格式
4.1 完善页面
4.1.1 定义分页相关模型数据
pagination: {
currentPage: 1,
pageSize:10,
total:0,
queryString:null
},
dataList: [],
4.1.2 定义分页方法
在页面中提供了findPage方法用于分页查询,为了能够在checkitem.html页面加载后直接可以展示分页数据,可以在VUE提供的钩子函数created中调用findPage方法。
created() {
this.findPage();
}
findPage() {
var param = {
currentPage: this.pagination.currentPage,
pageSize: this.pagination.pageSize,
queryString: this.pagination.queryString
};
axios.post('/checkitem/findPage.do', param).then((res) => {
this.pagination.total = res.data.total;
this.dataList = res.data.rows;
});
},
4.1.3 完善分页方法执行时机
除了在created钩子函数中调用findPage方法查询分页数据之外,当用户点击查询按钮或者点击分页条中的页码时也需要调用findPage方法重新发起查询请求。 为查询按钮绑定单击事件,调用findPage方法
<el-button @click="findPage()" class="dalfBut">查询</el-button>
为分页条组件绑定current-change事件,此事件是分页条组件自己定义的事件,当页码改变时触发,对 应的处理函数为handleCurrentChange
<el-pagination
class="pagiantion"
@current-change="handleCurrentChange"
:current-page="pagination.currentPage"
:page-size="pagination.pageSize"
layout="total, prev, pager, next, jumper"
:total="pagination.total">
</el-pagination>
定义handleCurrentChange方法
handleCurrentChange(currentPage) {
this.pagination.currentPage = currentPage;
this.findPage();
}
4.2 后台代码
4.2.1 Controller
在CheckItemController中增加分页查询方法
@PostMapping("/findPage")
public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
PageResult pageResult = null;
try {
pageResult = checkItemService.findPage(queryPageBean);
} catch (Exception e) {
e.printStackTrace();
return new PageResult(null, null);
}
return pageResult;
}
4.2.2 服务接口
在CheckItemService服务接口中扩展分页方法
public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString);
4.2.3 服务实习类
在CheckItemServiceImpl服务实现类中实现分页查询方法,基于Mybatis分页助手插件实现分页
@Override
public PageResult findPage(QueryPageBean queryPageBean) throws Exception {
Integer currentPage = queryPageBean.getCurrentPage();
Integer pageSize = queryPageBean.getPageSize();
String queryString = queryPageBean.getQueryString();
if (queryString == null || "".equals(queryString)) {
PageHelper.startPage(currentPage, pageSize);
} else {
PageHelper.startPage(1, pageSize);
}
Page<CheckItem> page = checkItemDao.selectByCondition(queryString);
Long total = page.getTotal();
List<CheckItem> rows = page.getResult();
return new PageResult(total, rows);
}
4.2.4 Dao接口
在CheckItemDao接口中扩展分页查询方法
public Page<CheckItem> selectByCondition(String queryString);
4.2.5 Mapper映射文件
在CheckItemDao.xml文件中增加SQL定义
<select id="selectByCondition" parameterType="String" resultType="com.itheima.pojo.CheckItem">
select * from t_checkitem
<if test="value != null and value.length > 0">
where code = #{value} or name = #{value}
</if>
</select>
五、删除检查项
5.1 完善页面
为了防止用户误操作,点击删除按钮时需弹出确认删除的提示,用户点击取消则不做任何操作,用户点击确定按钮再提交删除请求。
5.1.1 绑定单击事件
需要为删除按钮绑定单击事件,并且将当前行数据作为参数传递给处理函数
<el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
handleDelete(row) {
}
5.1.2 弹出确认操作提示
用户点击删除按钮会执行handleDelete方法,此处需要完善handleDelete方法,弹出确认提示信息。 ElementUI提供了$confirm方法来实现确认提示消息弹框效果
this.$confirm("您确定要删除这条数据吗?", "提示", {
type: 'warning'
})
5.1.3 发送请求
如果用户点击确定按钮就需要发送ajax请求,并且将当前检查项的id作为参数提交到后台进行删除操作
handleDelete(row) {
this.$confirm("您确定要删除这条数据吗?", "提示", {
type: 'warning'
}).then(() => {
axios.get('/checkitem/deleteById.do?id=' + row.id).then((res) => {
if (res.data.flag) {
this.$message({
message: res.data.message,
type: 'success'
});
this.findPage();
} else {
this.$message.error(res.data.message);
}
});
}).catch(() => {
this.$message({
message: '操作已取消',
type: 'info'
});
});
}
5.2 后台代码
5.2.1 Controller
在CheckItemController中增加删除方法
@GetMapping("/deleteById")
public Result deleteById(Integer id) {
try {
checkItemService.deleteById(id);
} catch (Exception e) {
e.printStackTrace();
return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL);
}
return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
}
在CheckItemService服务接口中扩展删除方法
public void deleteById(Integer id) throws Exception;
5.2.3 服务实现类
注意:不能直接删除,需要判断当前检查项是否和检查组关联,如果已经和检查组进行了关联则不允许删除
@Override
public void deleteById(Integer id) throws Exception {
Long count = checkItemDao.findCountByCheckItemId(id);
if (count > 0) {
new RuntimeException();
}
checkItemDao.deleteById(id);
}
5.2.4 Dao接口
在CheckItemDao接口中扩展方法findCountByCheckItem和deleteById
public Long findCountByCheckItemId(Integer id) throws Exception;
public void deleteById(Integer id) throws Exception;
5.2.5 Mapper映射文件
在CheckItemDao.xml中扩展SQL语句
<select id="findCountByCheckItemId" parameterType="Integer" resultType="Long">
select count(*) from t_checkgroup_checkitem where checkItem_id = #{checkItem_id}
</select>
<delete id="deleteById" parameterType="Integer">
delete from t_checkitem where id = #{id}
</delete>
六、编辑检查项
6.1 完善页面
用户点击编辑按钮时,需要弹出编辑窗口并且将当前记录的数据进行回显,用户修改完成后点击确定按钮将修改后的数据提交到后台进行数据库操作。
6.1.1 绑定单击事件
需要为编辑按钮绑定单击事件,并且将当前行数据作为参数传递给处理函数
handleUpdate(row) {
}
6.1.2 弹出编辑窗口回显数据
当前页面中的编辑窗口已经提供好了,默认处于隐藏状态。在handleUpdate方法中需要将编辑窗口展 示出来,并且需要发送ajax请求查询当前检查项数据用于回显
handleUpdate(row) {
this.resetForm();
axios.get('/checkitem/findById.do?id=' + row.id).then((res) => {
if (res.data.flag) {
this.formData = res.data.data;
this.dialogFormVisible4Edit = true;
} else {
this.$message.error(res.data.message);
}
});
}
6.1.3 发送请求
在编辑窗口中修改完成后,点击确定按钮需要提交请求,所以需要为确定按钮绑定事件并提供处理函数 handleEdit
<el-button type="primary" @click="handleEdit()">确定</el-button>
handleEdit() {
this.$refs['dataEditForm'].validate((valid) => {
if (valid) {
axios.post('/checkitem/update.do', this.formData).then((res) => {
if (res.data.flag) {
this.dialogFormVisible4Edit = false;
this.$message({
message: res.data.message,
type: 'success'
});
} else {
this.$message.error(res.data.message);
}
}).finally(() => {
this.findPage();
});
} else {
this.$message.error("数据校验失败!");
return false;
}
});
},
6.2 后台代码
6.2.1 Controller
在CheckItemController中增加编辑方法
@Override
public CheckItem findByid(Integer id) throws Exception {
return checkItemDao.findById(id);
}
@PostMapping("/update")
public Result update(@RequestBody CheckItem checkItem) {
try {
checkItemService.update(checkItem);
} catch (Exception e) {
e.printStackTrace();
return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL);
}
return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
}
6.2.2 服务接口
在CheckItemService服务接口中扩展编辑方法
public CheckItem findByid(Integer id) throws Exception;
public void update(CheckItem checkItem) throws Exception;
6.2.3 服务实现类
在CheckItemServiceImpl实现类中实现编辑方法
@Override
public CheckItem findByid(Integer id) throws Exception {
return checkItemDao.findById(id);
}
@Override
public void update(CheckItem checkItem) throws Exception {
checkItemDao.update(checkItem);
}
6.2.4 Dao接口
在CheckItemDao接口中扩展edit方法
public CheckItem findById(Integer id);
public void update(CheckItem checkItem);
6.2.5 Mapper映射文件
在CheckItemDao.xml中扩展SQL语句
<select id="findById" parameterType="Integer" resultType="com.itheima.pojo.CheckItem">
select * from t_checkitem where id = #{id}
</select>
<update id="update" parameterType="com.itheima.pojo.CheckItem">
update t_checkitem
<set>
<if test="code != null">
code = #{code},
</if>
<if test="name != null">
name = #{name},
</if>
<if test="sex != null">
sex = #{sex},
</if>
<if test="age != null">
age = #{age},
</if>
<if test="price != null">
price = #{price},
</if>
<if test="type != null">
type = #{type},
</if>
<if test="attention != null">
attention = #{attention},
</if>
<if test="remark != null">
remark = #{remark}
</if>
</set>
where id = #{id}
</update>
|