SSM之增删改查【开发版本一】

1 项目简介

  • (1)项目的经典模块-CRUD功能
    本次项目案例:部门增删改查
    实现功能:CRUD-不包含ajax
  • (2)什么是CRUD
    CRUD:Create( 创 建 ) Retrieve(查询) Update(更新) Delete(删除)
  • (3)使用技术
    ssm:SpringMVC+Spring+MyBatis
    在这里插入图片描述

2 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);

3 SMM标题环境搭建

利用之前已搭建好的SSM环境,在此基础上开发本项目。
在这里插入图片描述
在这里插入图片描述

4 CRUD开发代码

(1) 查询

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

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 + '\'' +
                '}';
    }
DempartmentServiceImpl
@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.syy.dao.IDepartmentDao">

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

</mapper>

查询前台代码:显示

DepartmentController
@Controller
@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>

(2)添加

添加后台代码

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>
list_depes.jsp
<a href="${pageContext.request.contextPath}/dept/addUI">新增</a>

(3)删除

删除后台代码

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";
    }

(4)修改

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

修改后台代码

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>
list_depts.jsp
<a href="${pageContext.request.contextPath}/dept/updateUI?did=${dept.did}">修改</a>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值