一:新增检查项
(1)页面
//添加 handleAdd()方法 发送ajax post请求 携带参数 fromdata 提交到Controller进行处理。
注意事项 :进行表单校验 。检验通过发送ajax请求。 然后调用分页查询方法 重新查询
(2) 后台代码
Controller: CheckitemController:调用service add(Checkitem checkitem) 方法
interface: CheckitemService: 添加 add方法
Service:CheckitemServiceImpl: 注入checkitemDao 属性 实现add方法 调用 checkitemdao.add(Checkitem checkitem)
Dao: CheckitemDao 接口 :添加 add方法
映射文件:CheckitemDao.xml :编写添加sql语句。
二:分页查询/条件查询
(1)页面:
本项目所有分页功能都是基于ajax的异步请求来完成的,请求参数和后台响应数据格式都使用json数据 格式。 请求参数包括页码、每页显示记录数、查询条件。 请求参数的json格式为:{currentPage:1,pageSize:10,queryString:''itcast''} 后台响应数据包括总记录数、当前页需要展示的数据集合。 响应数据的json格式为:{total:1000,rows:[]}
1.首先定义分页查询相关的模型数据
2.定义分页方法
3.完善分页方法执行时机 除了在created钩子函数中调用findPage方法查询分页数据之外,当用户 点击查询按钮或者点击分页条中的页码时也需要调用findPage方法重新发起查询请求。
(2)后台代码:
Controller: CheckitemController:
//检查项分页
@PostMapping("/findPage")
public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
PageResult pageResult = checkItemService.pageQuery(queryPageBean);
return pageResult;
}
interface: CheckitemService:
public PageResult pageQuery(QueryPageBean queryPageBean);
Service:CheckitemServiceImpl: 注入checkitemDao 属性
@Override
//检查项分页查询
public PageResult pageQuery(QueryPageBean queryPageBean) {
Integer currentPage = queryPageBean.getCurrentPage();
Integer pageSize = queryPageBean.getPageSize();
String queryString = queryPageBean.getQueryString();
//完成分页查询,基于mybatis提供的分页助手插件完成;
PageHelper.startPage(currentPage,pageSize);
if(queryString != null && queryString.length()>0){
PageHelper.startPage(1,pageSize);
}else {
PageHelper.startPage(currentPage,pageSize);
}
Page<CheckItem> page = checkItemDao.selectByCondition(queryString);
long total = page.getTotal();
List<CheckItem> rows = page.getResult();
return new PageResult(total,rows);
}
Dao: CheckitemDao 接口 :
//使用mybatis分页助手插件 必须返回page
public Page<CheckItem> selectByCondition(String queryString);
映射文件: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 = #{code} or name = #{name}
</if>
</select>
三:删除检查项 根据id删除
(1)注意事项:删除之前需要考虑 删除的检查项是否关联检查组 如果关联 则不能被删除
1。前端页面:如果用户点击确定按钮就需要发送ajax请求,并且将当前检查项的id作为参数提交到后台进行删除操作
// 删除
handleDelete(row) {
//alert(row.id);
this.$confirm("确认删除吗?","提示",{type:'warning'}).then(()=>{
//点击确定按钮时只需此处代码
//alert('用户点击的是确定按钮');
axios.get("/checkitem/delete.do?id=" + row.id).then((res)=> {
if(!res.data.flag){
//删除失败
this.$message.error(res.data.message);
}else{
//删除成功
this.$message({
message: res.data.message,
type: 'success'
});
//调用分页,获取最新分页数据
this.findPage();
}
});
});
}
(2) 后台代码
Controller: CheckitemController:
//删除
@RequestMapping("/delete")
public Result delete(Integer id){
try {
checkItemService.delete(id);
}catch (RuntimeException e){
return new Result(false,e.getMessage());
}catch (Exception e){
return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL);
}
return new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS);
}
interface: CheckitemService:
public void delete(Integer id);
Service:CheckitemServiceImpl: 注入checkitemDao 属性
注意:不能直接删除,需要判断当前检查项是否和检查组关联,如果已经和检查组进行了关联则不允许 删除
//删除
public void delete(Integer id) throws RuntimeException{
//查询当前检查项是否和检查组关联
long count = checkItemDao.findCountByCheckItemId(id);
if(count > 0){
//当前检查项被引用,不能删除
throw new RuntimeException("当前检查项被引用,不能删除");
}
checkItemDao.deleteById(id);
}
Dao: CheckitemDao 接口 :
public void deleteById(Integer id);
public long findCountByCheckItemId(Integer checkItemId);
映射文件:CheckitemDao.xml :编写添加sql语句。
<!--删除-->
<delete id="deleteById" parameterType="int">
delete from t_checkitem where id = #{id}
</delete>
<!--根据检查项id查询中间关系表-->
<select id="findCountByCheckItemId" resultType="long" parameterType="int">
select count(*) from t_checkgroup_checkitem where checkitem_id = #
{checkitem_id}
</select>
四:编辑/修改检查组 先根据id 进行查询 然后修改
(1)前端页面:
// 弹出编辑窗口
handleUpdate(row) {
//发送请求获取检查项信息
axios.get("/checkitem/findById.do?id=" + row.id).then((res)=>{
if(res.data.flag){
//设置编辑窗口属性,dialogFormVisible4Edit为true表示显示
this.dialogFormVisible4Edit = true;
//为模型数据设置值,基于VUE双向数据绑定回显到页面
this.formData = res.data.data;
}else{
this.$message.error("获取数据失败,请刷新当前页面");
}
});
}
//编辑
handleEdit() {
//表单校验
this.$refs['dataEditForm'].validate((valid)=>{
if(valid){
//表单校验通过,发送请求
axios.post("/checkitem/edit.do",this.formData).then((response)=> {
//隐藏编辑窗口
this.dialogFormVisible4Edit = false;
if(response.data.flag){
//编辑成功,弹出成功提示信息
this.$message({
message: response.data.message,
type: 'success'
});
}else{
//编辑失败,弹出错误提示信息
this.$message.error(response.data.message);
}
}).finally(()=> {
this.findPage();
});
}else{
//表单校验失败
this.$message.error("表单数据校验失败");
return false;
}
});
}
(2):后台代码
Controller: CheckitemController:
//编辑
@RequestMapping("/edit")
public Result edit(@RequestBody CheckItem checkItem){
try {
checkItemService.edit(checkItem);
}catch (Exception e){
return new Result(false,MessageConstant.EDIT_CHECKITEM_FAIL);
}
return new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS);
}
//根据id查找
@RequestMapping("/findById")
public Result findById(Integer id){
try{
CheckItem 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);
}
}
interface: CheckitemService:
public void edit(CheckItem checkItem);
public CheckItem findById(Integer id);
Service:CheckitemServiceImpl: 注入checkitemDao 属性
//编辑
public void edit(CheckItem checkItem) {
checkItemDao.edit(checkItem);
}
public CheckItem findById(Integer id) {
return checkItemDao.findById(id);
}
Dao: CheckitemDao 接口 :
public void edit(CheckItem checkItem);
public CheckItem findById(Integer id);
映射文件:CheckitemDao.xml :编写添加sql语句。
<!--编辑-->
<update id="edit" parameterType="com.itheima.pojo.CheckItem">
update t_checkitem
<set>
<if test="name != null">
name = #{name},
</if>
<if test="sex != null">
sex = #{sex},
</if>
<if test="code != null">
code = #{code},
</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>
//根据id查询 检查项
<select id="findById" parameterType="int"
resultType="com.itheima.pojo.CheckItem">
select * from t_checkitem where id = #{id}
</select>