文章目录
0.前言
上一篇博客详细的介绍了预约检查项管理的增删改查操作,为了方便共同学习,下面给出相关的完整代码。其中可能包含后续介绍功能的相关代码,但不影响使用。
1.前端页面checkitem.html
<!DOCTYPE html>
<html>
<head>
<!-- 页面meta -->
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>传智健康</title>
<meta name="description" content="传智健康">
<meta name="keywords" content="传智健康">
<meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
<!-- 引入样式 -->
<link rel="stylesheet" href="../plugins/elementui/index.css">
<link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
<link rel="stylesheet" href="../css/style.css">
</head>
<body class="hold-transition">
<div id="app">
<div class="content-header">
<h1>预约管理<small>检查项管理</small></h1>
<el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
<el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
<el-breadcrumb-item>预约管理</el-breadcrumb-item>
<el-breadcrumb-item>检查项管理</el-breadcrumb-item>
</el-breadcrumb>
</div>
<div class="app-container">
<div class="box">
<div class="filter-container">
<el-input placeholder="项目编码/项目名称" v-model="pagination.queryString" style="width: 200px;" class="filter-item"></el-input>
<el-button @click="findPage()" class="dalfBut">查询</el-button>
<el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
</div>
<el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
<el-table-column type="index" align="center" label="序号"></el-table-column>
<el-table-column prop="code" label="项目编码" align="center"></el-table-column>
<el-table-column prop="name" label="项目名称" align="center"></el-table-column>
<el-table-column label="适用性别" align="center">
<template slot-scope="scope">
<span>{{ scope.row.sex == '0' ? '不限' : scope.row.sex == '1' ? '男' : '女'}}</span>
</template>
</el-table-column>
<el-table-column prop="age" label="适用年龄" align="center"></el-table-column>
<el-table-column prop="remark" label="项目说明" align="center"></el-table-column>
<el-table-column label="操作" align="center">
<template slot-scope="scope">
<el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
<el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
</template>
</el-table-column>
</el-table>
<div class="pagination-container">
<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>
</div>
<!-- 新增标签弹层 -->
<div class="add-form">
<el-dialog title="新增检查项" :visible.sync="dialogFormVisible">
<el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right" label-width="100px">
<el-row>
<el-col :span="12">
<el-form-item label="项目编码" prop="code">
<el-input v-model="formData.code"/>
</el-form-item>
</el-col>
<el-col :span="12">
<el-form-item label="项目名称" prop="name">
<el-input v-model="formData.name"/>
</el-form-item>
</el-col>
</el-row>
<el-row>
<el-col :span="12">
<el-form-item label="性别">
<el-select v-model="formData.sex">
<el-option label="不限" value="0"></el-option>
<el-option label="男" value="1"></el-option>
<el-option label="女" value="2"></el-option>
</el-select>
</el-form-item>
</el-col>
<el-col :span="12">
<el-form-item label="适用年龄">
<el-input v-model="formData.age"/>
</el-form-item>
</el-col>
</el-row>
<el-row>
<el-col :span="12">
<el-form-item label="类型">
<el-select v-model="formData.type">
<el-option label="检查" value="1"></el-option>
<el-option label="检验" value="2"></el-option>
</el-select>
</el-form-item>
</el-col>
<el-col :span="12">
<el-form-item label="价格">
<el-input v-model="formData.price"/>
</el-form-item>
</el-col>
</el-row>
<el-row>
<el-col :span="24">
<el-form-item label="项目说明">
<el-input v-model="formData.remark" type="textarea"></el-input>
</el-form-item>
</el-col>
</el-row>
<el-row>
<el-col :span="24">
<el-form-item label="注意事项">
<el-input v-model="formData.attention" type="textarea"></el-input>
</el-form-item>
</el-col>
</el-row>
</el-form>
<div slot="footer" class="dialog-footer">
<el-button @click="dialogFormVisible = false">取消</el-button>
<el-button type="primary" @click="handleAdd()">确定</el-button>
</div>
</el-dialog>
</div>
<!-- 编辑标签弹层 -->
<div class="add-form">
<el-dialog title="编辑检查项" :visible.sync="dialogFormVisible4Edit">
<el-form ref="dataEditForm" :model="formData" :rules="rules" label-position="right" label-width="100px">
<el-row>
<el-col :span="12">
<el-form-item label="项目编码" prop="code">
<el-input v-model="formData.code"/>
</el-form-item>
</el-col>
<el-col :span="12">
<el-form-item label="项目名称" prop="name">
<el-input v-model="formData.name"/>
</el-form-item>
</el-col>
</el-row>
<el-row>
<el-col :span="12">
<el-form-item label="性别">
<el-select v-model="formData.sex">
<el-option label="不限" value="0"></el-option>
<el-option label="男" value="1"></el-option>
<el-option label="女" value="2"></el-option>
</el-select>
</el-form-item>
</el-col>
<el-col :span="12">
<el-form-item label="适用年龄">
<el-input v-model="formData.age"/>
</el-form-item>
</el-col>
</el-row>
<el-row>
<el-col :span="12">
<el-form-item label="类型">
<el-select v-model="formData.type">
<el-option label="检查" value="1"></el-option>
<el-option label="检验" value="2"></el-option>
</el-select>
</el-form-item>
</el-col>
<el-col :span="12">
<el-form-item label="价格">
<el-input v-model="formData.price"/>
</el-form-item>
</el-col>
</el-row>
<el-row>
<el-col :span="24">
<el-form-item label="项目说明">
<el-input v-model="formData.remark" type="textarea"></el-input>
</el-form-item>
</el-col>
</el-row>
<el-row>
<el-col :span="24">
<el-form-item label="注意事项">
<el-input v-model="formData.attention" type="textarea"></el-input>
</el-form-item>
</el-col>
</el-row>
</el-form>
<div slot="footer" class="dialog-footer">
<el-button @click="dialogFormVisible4Edit = false">取消</el-button>
<el-button type="primary" @click="handleEdit()">确定</el-button>
</div>
</el-dialog>
</div>
</div>
</div>
</div>
</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<script>
var vue = new Vue({
el: '#app',
data:{
pagination: {//分页相关模型数据
currentPage: 1,//当前页码
pageSize:10,//每页显示的记录数
total:0,//总记录数
queryString:null//查询条件
},
dataList: [],//当前页要展示的分页列表数据
formData: {},//表单数据
dialogFormVisible: false,//增加表单是否可见
dialogFormVisible4Edit:false,//编辑表单是否可见
rules: {//校验规则
code: [{ required: true, message: '项目编码为必填项', trigger: 'blur' }],
name: [{ required: true, message: '项目名称为必填项', trigger: 'blur' }]
}
},
//钩子函数,VUE对象初始化完成后自动执行
created() {
this.findPage();
},
methods: {
//编辑
handleEdit() {
//校验表单
this.$refs["dataEditForm"].validate((valid) => {
if (valid) {
//关闭弹出框
this.dialogFormVisible4Edit=false;
//发送新增检查项请求 checkItem对象(这时要用post方法),this.formData里面的数据都要提交
axios.post("/checkitem/edit.do",this.formData).then(res=> {
this.$message({
message: res.data.message,
type: res.data.flag ? "success" : "error"
});
this.findPage();
})
}
});
},
//添加
handleAdd () {
this.$refs["dataAddForm"].validate((valid) => {
if (valid) {
//1.关闭弹窗
this.dialogFormVisible=false;
//2.发送新增检查项
axios.post("/checkitem/add.do",this.formData).then(response=>{
//3.成功回显数据
if(response.data.flag){
this.$message({
message:response.data.message,
type:"success"
});
//跳转新查询
this.findPage();
}
//失败错误提示
else{
this.$message.error(response.data.message)
}
})
}
});
},
//分页查询
/*findPage() {
axios.post('/checkitem/findAll.do').then(res=>{
if(res.data.flag){
//提示成功
this.$message({
message:res.data.message,
type:'success'
});
//结果显示到页面:data='dataList'将findPage数据绑定到table组件当中
this.dataList=res.data.data;
}
})
},*/
findPage() {
//定义查询条件
var queryPageBean={
currentPage: this.pagination.currentPage,//当前页码
pageSize: this.pagination.pageSize,//当前显示条数
queryString: this.pagination.queryString//查询条件
}
axios.post("/checkitem/findPage.do",queryPageBean).then((response)=>{
this.pagination.total=response.data.total;
this.dataList=response.data.rows;
})
},
// 重置表单
resetForm() {
this.formData={};
},
// 弹出添加窗口
handleCreate() {
this.dialogFormVisible=true;
//重置窗口
this.resetForm();
},
// 弹出编辑窗口
handleUpdate(row) {
//alert(Json.stringify(row));
// 根据id做回显,然后再提交,后台接收数据
//弹出编辑窗口
this.dialogFormVisible4Edit=true;
//发送查询请求获取数据
axios.get("/checkitem/findById.do?id="+row.id).then(res=>{
this.$message({
message:res.data.message,
type:res.data.flag ? "success":"error"
});
this.formData=res.data.data;
})
},
//切换页码
handleCurrentChange(currentPage) {
//重新设置页码,重新查询
this.pagination.currentPage=currentPage;
this.findPage();
},
showMessage(r){
if(r=="Error: Request failed with status code 403"){
this.$message.error("权限不足,请更改用户")
}else{
this.$message.error("未知错误,请联系管理员")
}
// alert(r);
},
// 删除
handleDelete(row) {
//删除时要先检验并且提醒
// alert(row.id);
this.$confirm("确认删除当前选中记录吗?","提示",{type:'warning'}).then(()=>{
//点击确定按钮时只需此处代码
axios.get("/checkitem/deleteById.do?id="+row.id).then((res)=>{
this.$message({
message:res.data.message,
type:res.data.flag ? "success":"error"
});
this.findPage();
}).catch(error=>{
this.showMessage(error)
})
}).catch(()=>{
this.$message({
type:"info",
message:"已取消删除"
});
})
}
}
})
</script>
</html>
2.CheckitemController.java
@RestController
@RequestMapping(value="/checkitem")
public class CheckItemController {
@Reference
private CheckItemService checkItemService;
@RequestMapping(value = "/findAll")
public Result findAll() {
List<CheckItem> checkItemList = checkItemService.findAll();
return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkItemList);
}
@RequestMapping(value = "/add")
@PreAuthorize("hasAuthority('CHECKITEM_ADD')")
public Result add(@RequestBody CheckItem checkItem) {
try {
checkItemService.add(checkItem);
return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
} catch (Exception e) {
e.printStackTrace();
return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
}
}
@RequestMapping("/findPage")
public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
try {
PageResult pageResult= checkItemService.findPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize(),queryPageBean.getQueryString());
return pageResult;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
@RequestMapping("/deleteById")
@PreAuthorize("hasAuthority('CHECKITEM_DELETE')")
public Result findPage(Integer id) {
try {
checkItemService.deleteById(id);
return new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);
} catch (Exception e) {
e.printStackTrace();
return new Result(false,MessageConstant.DELETE_CHECKITEM_GROUP_FAIL);
}
}
@RequestMapping("/findById")
public Result findById(Integer id) {
CheckItem checkItem=null;
try {
checkItem=checkItemService.findById(id);
return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItem);
} catch (Exception e) {
e.printStackTrace();
return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
}
}
@RequestMapping("/edit")
@PreAuthorize("hasAuthority('CHECKITEM_EDIT')")
public Result edit(@RequestBody CheckItem checkItem) {
try {
checkItemService.edit(checkItem);
return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
} catch (Exception e) {
e.printStackTrace();
return new Result(false,MessageConstant.EDIT_CHECKGROUP_FAIL);
}
}
}
3.CheckitemService.java
public interface CheckItemService {
/**
* 查询所有检查项
* @return
*/
List<CheckItem> findAll();
/**
* 新增检查项
* @param checkItem
*/
void add(CheckItem checkItem);
/**
* 检查项
* @param currentPage
* @param pageSize
* @param queryString
* @return
*/
PageResult findPage(Integer currentPage, Integer pageSize, String queryString);
/**
* 删除检查项
* @param id
*/
void deleteById(Integer id);
/**
* 根据id查询并回显
* @param id
* @return
*/
CheckItem findById(Integer id);
/**
* 编辑检查项
* @param checkItem
*/
void edit(CheckItem checkItem);
}
4.CheckitemServiceImpl.java
@Service(interfaceClass = CheckItemService.class)
//事务注解之后,导致类由spring代理对象 接口目录和消费者不一致,导致消费者无法正常使用
@Transactional
//解决方案:事务控制加上之后报错需要配置service:interfaceClass = CheckItemService.class
//1.配置中使用cglib方式来创建代理 proxy-target-class=true
//2.服务提供者@Service指定接口服务类型
public class CheckItemServiceImpl implements CheckItemService {
@Autowired
private CheckItemDao checkItemDao;
@Override
public List<CheckItem> findAll() {
return checkItemDao.findAll();
}
@Override
public void add(CheckItem checkItem) {
checkItemDao.add(checkItem);
}
/**
* 检查分页项
* @param currentPage
* @param pageSize
* @param queryString
* @return
*/
@Override
public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {
//设置分页参数
PageHelper.startPage(currentPage,pageSize);
//需要分页的语句
Page<CheckItem> checkItemPage= checkItemDao.selectByCondition(queryString);
return new PageResult(checkItemPage.getTotal(),checkItemPage.getResult());
}
/**
* 删除检查项
* @param id
*/
@Override
public void deleteById(Integer id) {
//1.根据检查项id到t_checkgroup_checkitem查询关系是否存在
int count=checkItemDao.findCountByCheckItemId(id);
//2.若存在,抛出异常,当前检查和检查组已经关联,无法删除
if(count>0){
throw new RuntimeException(MessageConstant.DELETE_CHECKITEM_GROUP_FAIL);
}
//3. 不存在,直接删除
checkItemDao.deleteById(id);
}
/**
* 查询
* @param id
* @return
*/
@Override
public CheckItem findById(Integer id) {
return checkItemDao.findById(id);
}
@Override
public void edit(CheckItem checkItem) {
checkItemDao.edit(checkItem);
}
}
5.CheckitemDao.java
/**
* 检查项dao层
*/
public interface CheckItemDao {
/**
* 查询所有检查项
* 方式一:注解方式 @Select("select * from t_checkitem")
* 方式二:xml映射方式
* @return
*/
List<CheckItem> findAll();
/**
* 新增检查项
* @param checkItem
*/
void add(CheckItem checkItem);
/**
* 根据条件查询
* @param queryString
* @return
*/
Page<CheckItem> selectByCondition(String queryString);
/**
* 判断检查项是否和检查组有关联
* @param id
* @return
*/
int findCountByCheckItemId(Integer id);
/**
* 根据检查项id删除检查项
* @param id
*/
void deleteById(Integer id);
/**
* 根据id查询检查项
* @param id
* @return
*/
CheckItem findById(Integer id);
/**
* 更新检查项
* @param checkItem
*/
void edit(CheckItem checkItem);
/**
* findCheckItemListByCheckGroupId
* @param id
* @return
*/
List<CheckItem> findCheckItemListById(Integer id);
List<CheckItem> findCheckItemListByCheckGroupId(Integer id);
}
6.CheckitemDao.xml
<?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">
<!--
namespace="Dao接口的全限定名"
-->
<mapper namespace="com.itheima.dao.CheckItemDao">
<!--查询所有检查项-->
<select id="findAll" resultType="com.itheima.pojo.CheckItem">
select * from t_checkitem;
</select>
<!--新增检查项-->
<insert id="add" parameterType="com.itheima.pojo.CheckItem">
insert into t_checkitem(id,code,name,sex,age,price,type,remark,attention)
values (#{id},#{code},#{name},#{sex},#{age},#{price},#{type},#{remark},#{attention})
</insert>
<!--分页查询-->
<select id="selectByCondition" parameterType="string" resultType="com.itheima.pojo.CheckItem">
select * from t_checkitem
<if test="value !=null and value.length>0">
where name =#{value} or code =#{value}
</if>
</select>
<!--根据检查项id到t_checkgroup_checkitem查询关系是否存在-->
<select id="findCountByCheckItemId" parameterType="int" resultType="int">
select count(*) from t_checkgroup_checkitem where checkitem_id=#{id}
</select>
<!--直接删除检查项-->
<delete id="deleteById" parameterType="int">
delete from t_checkitem where id=#{id}
</delete>
<!--根据id查询检查项-->
<select id="findById" parameterType="int" resultType="com.itheima.pojo.CheckItem">
select * from t_checkitem where id=#{id}
</select>
<!--更新检查项-->
<update id="edit" parameterType="com.itheima.pojo.CheckItem">
update t_checkitem
<set>
<if test="code!=null and code.length>0">
code=#{code},
</if>
<if test="name!=null and name.length>0">
name=#{name},
</if>
<if test="sex!=null and sex.length>0">
sex=#{sex},
</if>
<if test="age!=null and age.length>0">
age=#{age},
</if>
<if test="price!=null">
price=#{price},
</if>
<if test="type!=null and type.length>0">
type=#{type},
</if>
<if test="remark!=null and remark.length>0">
remark=#{remark},
</if>
<if test="attention!=null and attention.length>0">
attention=#{attention},
</if>
</set>
where id = #{id}
</update>
<!--根据检查组id查询检查项信息-->
<select id="findCheckItemListById" resultType="com.itheima.pojo.CheckItem">
select * from t_checkitem where id
in (select checkitem_id from t_checkgroup_checkitem where checkgroup_id=#{id})
</select>
<!--使用检查组的id,查询当前检查组具有的检查项的集合-->
<select id="findCheckItemListByCheckGroupId" parameterType="int" resultType="com.itheima.pojo.CheckItem">
SELECT * FROM t_checkitem WHERE id IN (SELECT checkitem_id FROM t_checkgroup_checkitem WHERE checkgroup_id = #{checkGroupId})
</select>
</mapper>