增删改查
在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>
总结:其他的增删改流程都差不多的。