CRUD
Create( 创 建 ) Retrieve(查询) Update(更新) Delete(删除)
技术:ssm(spring+springmvc+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框架
查询
- Test模块
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestDepartmentService {
private static final Logger l = LoggerFactory.getLogger(TestDepartmentService.class);
@Autowired
IDepartmentService iDepartmentService;
@Test
public void test01(){
List<Department> list=iDepartmentService.findAllDepartment();
l.info("test01="+list);
}
}
- Department.java
package com.dsf.domain;
public class Department {
private Integer did;
private String dname;
public Department(String dname) {
this.dname = dname;
}
@Override
public String toString() {
return "Department{" +
"did=" + did +
", 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;
}
}
- service层
List<Department> findAllDepartment();
@Service
public class DepartmentServiceImpl implements IDepartmentService {
@Autowired
IDepartmentDao dao;
@Override
public List<Department> findAllDepartment() {
List<Department> list=dao.findAll();
return list;
}
}
- dao层
List<Department> findAll();
- dao层的映射文件
<?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.dsf.dao.IDepartmentDao">
<select id="findAll" resultType="department">
select * from department order by did asc;
</select>
</mapper>
- controller层
@Controller
@RequestMapping("/dept")
public class DepartmentContorller {
private static final Logger l= LoggerFactory.getLogger(DepartmentContorller.class);
@Autowired
IDepartmentService service;
@RequestMapping(path = "/list",method = RequestMethod.GET)
public String list(Model model){
List<Department> depts=service.findAllDepartment();
l.info("list="+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>
添加
- Test模块
@Test
public void test02(){
Department department=new Department("运维部门");
iDepartmentService.saveDepartment(department);
}
- service层
void saveDepartment(Department department);
@Override
public void saveDepartment(Department department) {
dao.save(department);
}
- dao层
void save(Department department);
- dao层的映射文件
<insert id="save" parameterType="department">
insert into department values(null,#{dname});
</insert>
- controller层
@RequestMapping(path = "/addUI",method = RequestMethod.GET)
public String addUI(){
l.info("addUI");
return "add_depts";
}
@RequestMapping(path = "/save",method = RequestMethod.POST)
public String save(Department department,Model model){
l.info("save department="+department);
if(department.getDname()!=null &&!"".equals(department.getDname())){
service.saveDepartment(department);
l.info("save="+department);
return "redirect:/dept/list";
}else{
model.addAttribute("error_msg","部门名称不合法");
return "forward:/error.jsp";
}
}
- add_dept.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ 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>
- list_depts.jsp
<a href="${pageContext.request.contextPath}/dept/addUI">新增</a>
- error.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${error_msg}
</body>
<a href="${pageContext.request.contextPath}/dept/addUI">返回</a>
</html>
删除
- Test模块
@Test
public void test03(){
iDepartmentService.deleteDepartment(4);
}
- service层
void deleteDepartment(int id);
@Override
public void deleteDepartment(int id) {
dao.deleteByid(id);
}
- dao层
void deleteByid(int id);
- dao层的映射文件
<delete id="deleteByid" parameterType="int">
delete from department where did=#{id};
</delete>
- controller层
@RequestMapping(path = "/delete",method = RequestMethod.GET)
public String delete(Integer did){
l.info("delete id="+did);
service.deleteDepartment(did);
return "redirect:/dept/list";
}
- list_depts.jsp
<a href="${pageContext.request.contextPath}/dept/delete?did=${department.did}">删除</a>
修改
需要在打开页面的同时,查询一下数据,把数据带到页面进行同赋值,这操作叫做回显
- Test模块
@Test
public void test04() {//
Department dept = new Department();
dept.setDid(3);
dept.setDname("最牛部门");
service.updateDepartmentById(dept);
}
@Test
public void test05() {
Department dept = service.findDepartmentById(1);
l.info("test05 dept="+dept);
}
- service层
void updateDepartment(Department department);
Department findDepartmentByid(int did);
@Override
public void updateDepartment(Department department) {
dao.update(department);
}
@Override
public Department findDepartmentByid(int did) {
return dao.findByid(did);
}
- dao层
void update(Department department);
Department findByid(int did);
- dao层的映射文件
<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>
- controller层
@RequestMapping(path = "/updateUI",method = RequestMethod.GET)
public String updateUI(Integer did,Model model){
l.info("updateUI did="+did);
Department department=service.findDepartmentByid(did);
model.addAttribute("department",department);
return "update_dept";
}
@RequestMapping(path = "/update",method = RequestMethod.POST)
public String update(Department department){
l.info("update =",department);
service.updateDepartment(department);
return "redirect:/dept/list";
}
- list_depts.jsp
<a href="${pageContext.request.contextPath}/dept/updateUI?did=${department.did}">修改</a>
- update_dept.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ 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/update">
<input type="hidden" name="did" value="${department.did}"><br/>
<input type="text" value="${department.did}" disabled="disabled"><br/>
<input type="text" name="dname" value="${department.dname}"><br/>
<input type="submit" value="保存修改"><br/>
</form>
</body>
</html>
form表单不会将标记为disabled="disabled"
的参数提交到后台
所以,必须增加一个type="hidden"
的input标签