SSM之CRUD开发部门增删改查无ajax

项目简介

  • 项目的经典模块-C(Create 创建)R(Retrieve 查询)U(Update 更新)D(Delete 删除)功能
  • 使用技术
    ssm:SpringMVC+Spring+Mybatis

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项目(如果是复制原来的项目,记得修改pom.xml,我这里用的是原来的项目,所以并不进行修改)

代码编写

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

  • TestDepartmentService
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestDepartment {
    private static final Logger l = LoggerFactory.getLogger(TestDepartment.class);
    @Qualifier("departmentServiceImpl")
    @Autowired
    IDepartmentService service;
    @Test
    //查询所有数据
    public void test01(){
        List<Department> list = service.findAllDepartment();
        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 + '\'' +
                '}';
    }
  • IDepartmentService
@Service
public interface IDepartmentService {
    //查询所有部门信息
    List<Department> findAllDepartment();
  • DepartmentServiceImpl
@Service
public class DepartmentServiceImpl implements IDepartmentService {
    @Autowired
    IDepartmentDao dao;
    @Override
    public List<Department> findAllDepartment() {
        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.wxx.dao.IDepartmentDao">
    <select id="findAll" resultType="department">
        select * from department;
    </select>
</mapper>

查询前台代码:显示

  • DepartmentController
@Controller
@RequestMapping("/dept")
public class DepartmentController {
    private static  final Logger l = LoggerFactory.getLogger(DepartmentController.class);
    @Qualifier("departmentServiceImpl")
    @Autowired
    private IDepartmentService iDepartmentService;
    @RequestMapping(path = "/list",method = RequestMethod.GET)
    public String list(Model model){
        List<Department> depts = iDepartmentService.findAllDepartment();
        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>

添加部门功能的后台代码

  • TestDepartmentSservice
@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
void save(Department dept);
  • IDepartmentDao.xml
<insert id="save" parameterType="department">
        insert into department values(null,#{dname});
    </insert>

添加功能的前台代码

  • DepartmentController
@RequestMapping(path = "/addUI",method = RequestMethod.GET)
    public String addUI(){
        l.info("addUI");
        return "add_dept";
    }
  • list_depts.jsp
<a href="${pageContext.request.contextPath}/dept/addUI">新增</a>
  • add_dept.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
    <title>添加部门</title>
</head>
<body>
    <form method="post" action="${pageContext.request.contextPath}/dept/save">
        <input type="text" name="dname"/><br/>
        <input type="submit" value="保存"/><br/>
    </form>
</body>
</html>

删除功能实现的后台代码

  • TestDepartmentService
 @Test
    //删除部门数据
    public void test03(){
        service.deleteDepartmentById(3);
    }
  • IDepartmentService
  //删除指定id的部门数据
    void deleteDepartmentById(int i);
  • DepartmentServiceImpl
   @Override
    public void deleteDepartmentById(int id) {
        dao.deleteById(id);
    }

  • IDepartmentDao
void deleteById(int id);
  • IDepartmentDao.xml
 <delete id="deleteById" parameterType="int">
        delete from department where did = #{id};
    </delete>

删除功能实现的前台代码

  • DepartmentController
 @RequestMapping(path = "/delete",method = RequestMethod.GET)
    public String delete(Integer did){
        l.info("delete did="+did);
        iDepartmentService.deleteDepartmentById(did);
        return "redirect:/dept/list";
    }
  • list_depts.jsp
<a href="${pageContext.request.contextPath}/dept/delete?did=${dept.did}">删除</a>

修改功能实现的后台代码

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

  • TestDepartmentService
 @Test
    //修改部门数据
    public void test04(){
        Department dept =new Department();
        dept.setDid(4);
        dept.setDname("开发");
        service.updateDepartmentById(dept);
    }
    @Test
    //为了方便用户修改,会在打开修改页面时做一个回显
    public void test05(){
        Department dept = service.findDepartmentById(1);
        l.info("test05 dept= "+dept);
    }
  • IDepartmentService
   //修改部门信息
    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
 void update(Department dept);

    Department findById(int did);
  • IDepartmentDao.xml
<delete id="update" parameterType="department">
        update department set dname = #{dname} where did = #{did};
    </delete>
    <select id="findById" parameterType="int" resultType="department">
        select * from department where did = #{did};
    </select>

修改功能实现的前台代码

  • DepartmentController
  @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";
    }
    @RequestMapping(path = "/update" ,method = RequestMethod.POST)
    public String update(Department dept){
        l.info("update dept="+dept);
        iDepartmentService.updateDepartmentById(dept);
        return "redirect:/dept/list";
    }

  • list_depts.jsp
<a href="${pageContext.request.contextPath}/dept/updateUI?did=${dept.did}">修改</a> 
  • update_dept.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<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="保存修改">
</form>

</body>
</html>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值