Day27SSM之CRUD开发版本1的部门增删改查

   日期:2020-10-20     浏览:89    评论:0    
核心提示:学习目标(1)CRUD-不包含ajax(2)CRUD-包含ajax(3)CRUD-包含ajax-bootstrap项目简介-(1)项目的经典模块-CRUD功能(2)什么是CRUDCRUD:Create( 创 建 ) Retrieve(查询) Update(更新) Delete(删除)(3)使用什么技术ssm:SpringMVC+Spring+MyBatis(4)版本V1 : ssmV2 : ssm+AjaxV3 : ssm+Ajax+bootstr

学习目标

  • (1)CRUD-不包含ajax
  • (2)CRUD-包含ajax
  • (3)CRUD-包含ajax-bootstrap

项目简介

-(1)项目的经典模块-CRUD功能

  • (2)什么是CRUD
    CRUD:Create( 创 建 ) Retrieve(查询) Update(更新) Delete(删除)




  • (3)使用什么技术
    ssm:SpringMVC+Spring+MyBatis
  • (4)版本
    V1 : ssm
    V2 : ssm+Ajax
    V3 : ssm+Ajax+bootstrap

sql数据库的设计

create database crud;
use crud;

create table department(
	did int primary key auto_increment,
	dname varchar(20)
)
insert into department values(null,'java');
insert into department values(null,'测试');
insert into department values(null,'需求');

create table employee(
	eid int primary key auto_increment,
	ename varchar(20),
	gender varchar(20),
	did int 
)
insert into employee values(null,'jack','1',1);
insert into employee values(null,'rose','1',1);
insert into employee values(null,'tony','1',2);

ssm搭建

》复制搭建好的SSM为SSM_CRUD
》修改SSM_CRUD pom.xml

  <artifactId>ssm02_crud</artifactId>
	...

  <name>ssm02_crud</name>

》修改Project pom.xml

  <modules>
    <module>ssm01</module>
    <module>ssm02_crud</module>
  </modules>

执行一下以上两个pom.xml

查询后台代码:保证数据正确

TestDepartmentService

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestDepartmentService { 
private static final Logger l = LoggerFactory.getLogger(TestDepartmentService.class);
   @Autowired
   IDepartmentService service;

    @Test
    public void test01() { //
        List<Department> list = service.findAllDepartments();
        l.info("test01 list=" + list);
    }

}

Department

public class Department { 
    private Integer did;
    private String dname;

    public Department() { 
    }

    public Department(String dname) { 
        this.dname = dname;
    }

    @Override
    public String toString() { 
        return "Department{" +
                "did=" + did +
                ", dname='" + dname + '\'' +
                '}';
    }

DepartmentServiceImpl

@Service
public class DempartmentServiceImpl implements IDepartmentService { 
    @Autowired
    private IDepartmentDao dao;
    @Override
    public List<Department> findAllDepartments() { 
        List<Department> list = dao.findAll();
        return list;
    }
}

IDepartmentDao

public interface IDepartmentDao { 
    //select * from department order by did asc;
    List<Department> findAll();
}

IDepartmentDao.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">
<mapper namespace="com.wzx.dao.IDepartmentDao">

    <select id="findAll" resultType="department">
        select * from department order by did asc;
    </select>

</mapper>

查询前台代码:显示

DepartmentController

@RequestMapping("/dept")
public class DepartmentController { 
    private static  final Logger l = LoggerFactory.getLogger(DepartmentController.class);
    @Autowired
    private IDepartmentService iDepartmentService;
    @RequestMapping(path = "/list",method = RequestMethod.GET)
    public String list(Model model){ 
        List<Department> depts = iDepartmentService.findAllDepartments();
        l.info("list depts="+depts);
        //数据添加到页面
        model.addAttribute("depts",depts);
        return "list_depts";
    }
}

list_depts.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
        <html>
<head>
    <title>Title</title>
</head>
<body>
<a href="">新增</a>
<table border="1px" width="100%">
    <tr>
        <td>编号</td>
        <td>部门名称</td>
        <td>管理</td>

    </tr>

    <c:forEach items="${depts}" var="dept">
         <tr>
             <td>${dept.did}</td>
             <td>${dept.dname}</td>
             <td><a href="">删除</a><a href="">修改</a></td>
         </tr>
    </c:forEach>
</table>
</body>
</html>

添加后台代码

TestDepartmentService

    @Test
    public void test02() { //
        Department dept = new Department("UI设计部门");
        service.saveDepartment(dept);
    }

IDepartmentService

   //添加一个新的部门
   void saveDepartment(Department dept);

DepartmentServiceImpl

   @Override
    public void saveDepartment(Department dept) { 
        dao.save(dept);
    }

IDepartmentDao

    //insert into department values(null,'UI');
    void save(Department dept);

IDepartmentDao.xml

    <insert id="save" parameterType="department">
        insert into department values(null,#{ dname});
    </insert>

添加前台代码

DepartmentController

 @RequestMapping(path ="/save",method = RequestMethod.POST)
    public String save(Department dept,Model model){ 
        if (dept.getDname() != null && !"".equals(dept.getDname())) { 
            iDepartmentService.saveDepartment(dept);
            l.info("save dept="+dept);
            return "redirect:/dept/list";
        }else{ 
            model.addAttribute("error_msg","部门名称不能为空");
            return "forward:/error.jsp";
        }

    }

add_dept.jsp

    <form method="post" action="${pageContext.request.contextPath}/dept/save">
        <input type="text" name="dname"/><br/>
        <input type="submit" value="保存"/><br/>
    </form>

删除后台代码

TestDepartmentService

    @Test
    public void test03() { //

        service.deleteDepartmentById(1);
    }

IDepartmentService

     //删除指定id的部门数据
    void deleteDepartmentById(int i);

DepartmentServiceImpl

 @Override
    public void deleteDepartmentById(int id) { 
        dao.deleteById(id);
    }

IDepartmentDao

    //delete from department where did = 5;
    void deleteById(int id);

IDepartmentDao.xml

    <delete id="deleteById" parameterType="int">
        delete from department where did =#{id};
    </delete>

删除前台代码

list_depts.jsp

<a href="${pageContext.request.contextPath}/dept/delete?did=${dept.did}">删除</a>

DepartmentController

 //delete

    @RequestMapping(path = "/delete",method = RequestMethod.GET)
    public String delete(Integer did){ 
        l.info("delete did = "+did);
        iDepartmentService.deleteDepartmentById(did);
        return "redirect:/dept/list";
    }

修改后台代码

  • 修改功能,需要在打开页面的同时,查询一下数据,把数据带到页面进行同赋值,这操作叫做回显

TestDepartmentService

  @Test
    public void test04() { //
        Department dept = new Department();
        dept.setDid(3);
        dept.setDname("最牛部门");
        service.updateDepartmentById(dept);
    }
      @Test
    public void test05() { //为了方便用户修改,会在打开修改页面时做一个回显
    //获取id,查找数据,再将数据带到页面赋值

        Department dept = service.findDepartmentById(1);
        l.info("test05 dept="+dept);
    }

IDepartmentService

    //根据指定id修改部门名称
    void updateDepartmentById(Department dept);
    //查找指定id的部门数据
    Department findDepartmentById(int did);

DepartmentServiceImpl

    @Override
    public void updateDepartmentById(Department dept) { 
        dao.update(dept);
    }

    @Override
    public Department findDepartmentById(int did) { 
        return dao.findById(did);
    }

IDepartmentDao

    //update department set dname = ? where did = 1;
    void update(Department dept);

    //select * from department where did = 1;
    Department findById(int did);

IDepartmentDao.xml

    <update id="update" parameterType="department">
        update department set dname = #{dname} where did = #{did};
    </update>


    <select id="findById" parameterType="int" resultType="department">
        select * from department where did = #{did};
    </select>

修改前台代码

DepartmentController

 //updateUI?did=23
    @RequestMapping(path = "/updateUI",method = RequestMethod.GET)
    public String updateUI(Integer did,Model model){ 
        l.info("updateUI did = "+did);
        Department department = iDepartmentService.findDepartmentById(did);
        model.addAttribute("dept",department);
        return "update_dept";
    }

    //update
    @RequestMapping(path="/update",method = RequestMethod.POST)
    public String update(Department dept){ 
        //打印
        l.info("update dept="+dept);
        //调用service
        iDepartmentService.updateDepartmentById(dept);//
        //跳到查询页面
        return "redirect:/dept/list";
    }

update_dept.jsp


    <form method="post" action="${pageContext.request.contextPath}/dept/update">
        <input type="hidden" name="did" value="${dept.did}" >
        <input type="text" value="${dept.did}" disabled="disabled"/><br/>
        <input type="text" name="dname" value="${dept.dname}"/><br/>
        <input type="submit" value="保存修改"/><br/>
    </form>
  • form表单 不会将 标记为disabled="disabled"作为参数提交到后台
  • 所以,必须增加一个type="hidden"的input标签
 
打赏
 本文转载自:网络 
所有权利归属于原作者,如文章来源标示错误或侵犯了您的权利请联系微信13520258486
更多>最近资讯中心
更多>最新资讯中心
0相关评论

推荐图文
推荐资讯中心
点击排行
最新信息
新手指南
采购商服务
供应商服务
交易安全
关注我们
手机网站:
新浪微博:
微信关注:

13520258486

周一至周五 9:00-18:00
(其他时间联系在线客服)

24小时在线客服