为什么我的删除模块不管用,科室和科室列表都是不能用
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.4//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.imooc.hospital.dao.CategoryDao">
<resultMap id="category" type="Category">
<id property="id" column="id" javaType="Integer"></id>
<result property="name" column="name" javaType="String"></result>
<result property="createTime" column="create_time" javaType="java.util.Date"></result>
<result property="updateTime" column="update_time" javaType="java.util.Date"></result>
</resultMap>
<insert id="insert" parameterType="Category">
insert into category(name,create_time,update_time) values(#{name},#{createTime},#{updateTime})
</insert>
<update id="update" parameterType="Category">
update category set name=#{name},update_time=#{updateTime} where id=#{id}
</update>
<delete id="delete" parameterType="Integer">
delete from category where id=#{id}
</delete>
<select id="select" parameterType="Integer" resultMap="category">
select*from category where id=#{id}
</select>
<select id="selectAll" resultMap="category">
select*from category
</select>
</mapper>
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.4//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.imooc.hospital.dao.DeptDao">
<resultMap id="dept" type="Dept">
<id property="id" column="id" javaType="Integer"></id>
<result property="categoryId" column="category_id" javaType="Integer"></result>
<result property="createTime" column="create_time" javaType="java.util.Date"></result>
<result property="updateTime" column="update_time" javaType="java.util.Date"></result>
<result property="name" column="name" javaType="String"></result>
<association property="category" column="category_id" javaType="Category">
<id property="id" column="cid" javaType="Integer"></id>
<result property="name" column="cname" javaType="String"></result>
</association>
</resultMap>
<insert id="insert" parameterType="Dept">
insert into dept(category_id,create_time,update_time,name) values(#{categoryId},#{createTime},#{updateTime},#{name})
</insert>
<delete id="delete" parameterType="Integer">
delete from dept where id=#{id}
</delete>
<update id="update" parameterType="Dept">
update dept set category_id=#{categoryId},update_time=#{updateTime},name=#{name} where id=#{id}
</update>
<select id="select" resultMap="dept" parameterType="Integer">
select*from dept where id=#{id}
</select>
<select id="selectAll" resultMap="dept" >
select*from dept
</select>
<select id="selectByCategoryId" resultMap="dept" parameterType="Integer">
select d.*,c.id cid,c.name cname from dept d left join category c on d.category_id=c.id where
d.category_id=#{cid}
</select>
</mapper>
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.4//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.imooc.hospital.dao.UserDao">
<resultMap id="users" type="Users">
<id property="id" column="id" javaType="Integer"></id>
<result property="username" column="username" javaType="String"></result>
<result property="password" column="password" javaType="String"></result>
</resultMap>
<insert id="insert" parameterType="Users">
insert into users(username,password) values(#{username},#{password});
</insert>
<select id="selectByUsername" parameterType="Users" resultMap="users">
select*from users where username=#{username}
</select>
</mapper>
package com.imooc.hospital.service.Impl;
import com.imooc.hospital.dao.CategoryDao;
import com.imooc.hospital.entity.Category;
import com.imooc.hospital.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
@Service("categoryService")
public class CategoryServiceImpl implements CategoryService {
@Autowired
private CategoryDao categoryDao;
@Override
public void add(Category category) {
category.setCreateTime(new Date());
category.setUpdateTime(new Date());
categoryDao.insert(category);
}
@Override
public void delete(Integer id) {
categoryDao.delete(id);
}
@Override
public void update(Category category) {
category.setUpdateTime(new Date());
categoryDao.update(category);
}
@Override
public Category get(Integer id) {
return categoryDao.select(id);
}
@Override
public List<Category> getAll() {
return categoryDao.selectAll();
}
}
package com.imooc.hospital.service.Impl;
import com.imooc.hospital.dao.DeptDao;
import com.imooc.hospital.entity.Dept;
import com.imooc.hospital.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
@Service("deptService")
public class DeptServiceImpl implements DeptService {
@Autowired
private DeptDao deptDao;
@Override
public void add(Dept dept) {
dept.setCreateTime(new Date());
dept.setUpdateTime(new Date());
deptDao.insert(dept);
}
@Override
public void edit(Dept dept) {
dept.setUpdateTime(new Date());
deptDao.update(dept);
}
@Override
public void delete(Integer id) {
deptDao.delete(id);
}
@Override
public Dept get(Integer id) {
return deptDao.select(id);
}
@Override
public List<Dept> getAll() {
return deptDao.selectAll();
}
@Override
public List<Dept> getByCategoryId(Integer id) {
return deptDao.selectByCategoryId(id);
}
}
package com.imooc.hospital.service.Impl;
import com.imooc.hospital.dao.UserDao;
import com.imooc.hospital.entity.Users;
import com.imooc.hospital.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service("userService")
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public Users login(String username, String password) {
Users users=userDao.selectByUsername(username);
if(users!=null&&users.getPassword().equals(password)){
return users;
}
return null;
}
@Override
public Users zhuce(String username, String password) {
Users users2=new Users();
Users users = userDao.selectByUsername(username);
if (users!=null) {
return null;
}
users2.setUsername(username);
users2.setPassword(password);
userDao.insert(users2);
return users2;
}
}
package com.imooc.hospital.controller;
import com.imooc.hospital.entity.Category;
import com.imooc.hospital.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Map;
@Controller("categoryController")
@RequestMapping("/category")
public class CategoryController {
@Autowired
private CategoryService categoryService;
@RequestMapping("/list")
public String list(Map<String,Object> map) {
map.put("categorylist",categoryService.getAll());
return "category_list";
}
@RequestMapping("/to_add")
public String toAdd(Map<String,Object> map){
map.put("categoryAdd",new Category());
return "category_add";
}
@RequestMapping("/add")
public String add(Category category){
categoryService.add(category);
return "redirect:list";
}
@RequestMapping(value = "/to_update",params = "id")
public String toUpdate(Integer id,Map<String,Object> map){
map.put("categoryUpdate",categoryService.get(id));
return "category_update";
}
@RequestMapping("/update")
public String update(Category category){
categoryService.update(category);
return "redirect:list";
}
@RequestMapping("/remove")
public String remove(Integer id){
categoryService.delete(id);
return "redirect:list";
}
}
package com.imooc.hospital.controller;
import com.imooc.hospital.entity.Dept;
import com.imooc.hospital.service.CategoryService;
import com.imooc.hospital.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Map;
@Controller("deptController")
@RequestMapping("/dept")
public class DeptController {
@Autowired
private DeptService deptService;
@Autowired
private CategoryService categoryService;
@RequestMapping(value = "/list",params = "cid")
public String list(Map<String,Object> map,Integer cid){
map.put("deptList",deptService.getByCategoryId(cid));
return "dept_list";
}
@RequestMapping("/to_add")
public String toAdd(Map<String,Object> map){
map.put("deptAdd",new Dept());
map.put("categoryAdd",categoryService.getAll());
return "dept_add";
}
@RequestMapping("/add")
public String add(Dept dept){
deptService.add(dept);
return "redirect:list_all";
}
@RequestMapping(value = "/to_update",params = "id")
public String toUpdate(Map<String,Object> map,Integer id){
map.put("deptUpdate",deptService.get(id));
map.put("categoryUpdate",categoryService.getAll());
return "dept_update";
}
@RequestMapping( "/update")
public String update(Dept dept){
deptService.edit(dept);
return "redirect:list_all";
}
@RequestMapping( "/remove")
public String remove(Integer id){
deptService.delete(id);
return "redirect:list_all";
}
@RequestMapping("/detail")
public String detail(){
return "detail";
}
@RequestMapping( "/list_all")
public String listAll(Map<String,Object> map){
map.put("deptListAll",deptService.getAll());
return "dept_list_all";
}
}
package com.imooc.hospital.controller;
import com.imooc.hospital.entity.Users;
import com.imooc.hospital.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpSession;
@Controller("loginController")
@RequestMapping("/login")
public class LoginController {
@Autowired
private UserService userService;
@RequestMapping("/to_zhuce")
public String toZhuce(){
return "zhuce";
}
@RequestMapping("/zhuce")
public String zhuce(@RequestParam String username, @RequestParam String password, HttpSession session){
Users users=userService.zhuce(username,password);
if(users==null){
return "to_zhuce";
}
session.setAttribute("users",users);
return "redirect:to_login";
}
@RequestMapping("/to_login")
public String toLogin(){
return "login";
}
@RequestMapping("/login")
public String login(@RequestParam String username, @RequestParam String password, HttpSession session){
Users users=userService.login(username,password);
if(users==null){
return "redirect:to_login";
}
session.setAttribute("users",users);
return "redirect:/category/list";
}
}
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<html>
<head>
<meta charset="UTF-8">
<title>修改</title>
<link rel="stylesheet" href="/css/bootstrap.min.css">
<link rel="stylesheet" href="/css/add.css">
</head>
<body>
<nav class="navbar navbar-default">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="/dept/list_all">
慕课网科室管理
</a>
</div>
</div>
</nav>
<div class="container">
<div class="jumbotron">
<h1>Hello, 朋友!</h1>
<p>请小心的修改科室记录,要是修改了一个错误的就不好了。。。</p>
</div>
<div class="page-header">
<h3><small>修改</small></h3>
</div>
<form:form class="form-horizontal" modelAttribute="categoryUpdate" action="/category/update">
<div class="form-group">
<label for="name" class="col-sm-2 control-label" >名称 :</label>
<div class="col-sm-8">
<form:input path="name" cssClass="form-control" ></form:input>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary">保存</button>
</div>
</div>
</form:form>
</div>德
<footer class="text-center" >
copy@imooc
</footer>
</body>
</html>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>修改</title>
<link rel="stylesheet" href="/css/bootstrap.min.css">
<link rel="stylesheet" href="/css/add.css">
</head>
<body>
<nav class="navbar navbar-default">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="/dept/list_all">
慕课网科室管理
</a>
</div>
</div>
</nav>
<div class="container">
<div class="jumbotron">
<h1>Hello, 沙雕!</h1>
<p>请小心的修改科室记录,要是改了一个错误的就不好了。。。</p>
</div>
<div class="page-header">
<h3><small>修改</small></h3>
</div>
<form:form class="form-horizontal" action="/dept/update" modelAttribute="deptUpdate">
<div class="form-group">
<label for="name" class="col-sm-2 control-label">名称 :</label>
<div class="col-sm-8">
<form:input path="name" cssClass="form-control" />
</div>
</div>
<div class="form-group">
<label for="categoryId" class="col-sm-2 control-label">分类 :</label>
<select id="categoryId" name="categoryId" class="col-sm-2 form-control" style="width: auto">
<c:forEach items="${categoryUpdate}" var="cda">
<option value="${cda.id}">${cda.name}</option>
</c:forEach>
</select>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary">保存</button>
</div>
</div>
</form:form>
</div>
<footer class="text-center" >
copy@imooc
</footer>
</body>
</html>
其余的都没问题就是删除模块删除也没报错,数据库也没反应,一点删除直接跳转了
15
收起
正在回答
2回答
同学你好,是因为同学前台中没有传入id值,所以后台在修改时获取的id为null,导致修改不成功,所以在页面上需要一个隐藏的输入框来接收id
同学可以在两个修改的页面上添加如下语句
如下
祝学习愉快~
4. SSM到Spring Boot入门与综合实战
- 参与学习 人
- 提交作业 323 份
- 解答问题 8263 个
本阶段将带你学习主流框架SSM,以及SpringBoot ,打通成为Java工程师的最后一公里!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星