ssm-curd.version1

这篇博客详细介绍了在SSM(Spring、SpringMVC、MyBatis)框架下进行增删改查(CRUD)操作的具体步骤,包括编写查询的测试类、Domain、Service、Dao、Dao.xml、Controller以及前台展示页面的代码。总结提到,其他增删改操作流程与查询类似。
摘要由CSDN通过智能技术生成

增删改查

在ssm整合情况下进行CRUD
在这里插入图片描述

编写模式-查询

测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestDepartmentService {

    Logger log = LoggerFactory.getLogger(TestDepartmentService.class);
    @Autowired
    IDepartmentService service;

    @Test
    public void  test1(){
        List<Department> list = service.findAllDepartment();

        log.info("test list="+list);

    }
    }
Domain

domain\Department.java

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

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

   public Department() {
   }

   public Integer getDid() {
       return did;
   }

   public void setDid(Integer did) {
       this.did = did;
   }

   public String getDname() {
       return dname;
   }

   public void setDname(String dname) {
       this.dname = dname;
   }

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

service\IDepartmentService

 public interface IDepartmentService {

    List<Department> findAllDepartment();
}

service\departmentservice\DepartmentService

@Service
public class DepartmentService implements IDepartmentService {

    @Autowired
    private IDepartmentDao dao;

    @Override
    public List<Department> findAllDepartment() {

        List<Department> list = dao.findAll();

        return list;
    }
    }
Dao

dao\IDepartmentDao

 public interface IDepartmentDao {

   List<Department> findAll();
}
Dao.xml

dao\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.yzf.dao.IDepartmentDao">
    <select id="findAll" resultType="com.yzf.domain.Department">
        select * from department order by did asc;
    </select>
</mapper>
Controller

controller\DepartmentController

@Controller
@RequestMapping("/dept")
public class DepartmentController {

    Logger log = LoggerFactory.getLogger(DepartmentController.class);

    @Autowired
    private IDepartmentService iDepartmentService;

    @RequestMapping(path = "/list", method = RequestMethod.GET)
    public String list(Model model) {
        List<Department> depts = iDepartmentService.findAllDepartment();
        log.info("list   depts=" + depts);
        //数据添加到页面
        model.addAttribute("depts", depts);
        return "list_depts";
    }
   }
前台代码

src/main/webapp/WEB-INF/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="${pageContext.request.contextPath}/add_depts.jsp">新增</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="${pageContext.request.contextPath}/dept/delete?did=${dept.did}">删除</a><a href="${pageContext.request.contextPath}/dept/updateUI?did=${dept.did}">修改</a></td>
        </tr>
    </c:forEach>
</table>
</body>
</html>

总结:其他的增删改流程都差不多的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值