CRUD补充
1.在页面到增加页面,使用th:href
我们在static下增加addEmp.html页面
<a rel="nofollow" th:href="@{addEmp.html}">增加</a>
在EmpMapper,增加方法addEmp(Emp emp)
@Insert("insert emp(eid,ename,address,birth) values(#{eid},#{ename},#{address},#{birth})")
public int addEmp(Emp emp);
在EmpService,增加方法addEmp(Emp emp)
public int addEmp(Emp emp){ // 参数是一个实体对象emp
return empMapper.addEmp(emp);
}
在EmpController,增加方法
@RequestMapping("/addEmp")
public String addEmp(Emp emp,Model model){
int result=empService.addEmp(emp);
if(result>0)
{
return "forward:/selectAll";
}else{
model.addAttribute("msg","增加失败!");
return "status";
}
}
这时候,测试会报Date日期格式错误。修改实体类Emp
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") //注意是严格格式
Date birth;
<!--增加热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
setting设置
按Ctrl+Shift+Alt+/ 快捷键 内容设置
服务器配置
<a rel="nofollow" th:href="@{'deleteEmpById/'+${emp.eid}}">删除</a>
修改EmpMapper
@Delete("delete from emp where eid=#{eid}")
public int deleteEmpById(Integer eid);
修改EmpService
public int deleteById(Integer eid){
return empMapper.deleteEmpById(eid);
}
修改控制器
/**
* 根据编号来删除Emp
* @PathVariable("eid")这个是路径注解,可以让url使用下面的eid
* @return
*/
@RequestMapping("/deleteEmpById/{eid}")
public String deleteEmpById(@PathVariable("eid") Integer eid,Model model){
int result=empService.deleteById(eid);
if(result>0)
{
return "forward:/selectAll";
}else{
model.addAttribute("msg","删除失败!");
return "status";
}
}
mybatis.日志增加显示
application.yml文件增加配置即可
logging:
level:
com.aaa.mapper: debug
我们删除的时候delete和select日志信息会显示出
总体思路需要,首先 查询当前id的雇员信息,进入到雇员信息编辑页面;
其次,由页面去控制器,提交更新代码操作。
<a rel="nofollow" th:href="@{'/selectEmpById/'+${emp.eid}}">修改</a>
mapper
@Select("select * from emp where eid=#{eid}")
public Emp selectEmpById(Integer eid);
public Emp selectEmpById(Integer eid){
return empMapper.selectEmpById(eid);
}
控制器代码
@RequestMapping("/selectEmpById/{eid}")
public String selectEmpById(@PathVariable("eid")Integer eid,Model model){
Emp emp=empService.selectEmpById(eid);
System.out.println(emp);
//要去某个页面 会报错???return 默认去template下面的页面,去页面的有扩展名;
model.addAttribute("emp",emp);
//现在是forward格式:转发
// return "forward:/editEmp.html";
return "editEmp";
}
template下的页面代码
<!DOCTYPE html>
<html lang="en" xmlns:th="http://wwww.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>增加信息页面</title>
<style>
.hui{background: #999;}
</style>
</head>
<body>
<!--action:表示该view,去哪个控制器方法;不做表格了;-->
<form action="/updateEmp" method="post">
<div style="margin: 0 auto;width:600px;border: 1px dashed #eef;" >
编号:<input type="text" name="eid" readonly th:value="${emp.eid}" class="hui"/><br/>
姓名:<input type="text" name="ename" th:value="${emp.ename}"/><br/>
地址:<input type="text" name="address" th:value="${emp.address}"/><br/>
生日:<input type="text" name="birth" th:value="${#dates.format(emp.birth,'yyyy-MM-dd HH:mm:ss')}"/><br/>
<button type="submit">提交</button>
</div>
</form>
</body>
</html>
控制器代码
@RequestMapping("/updateEmp")
public String updateEmp(Emp emp,Model model){
int result=empService.updateEmp(emp);
if(result>0)
{
return "forward:/selectAll";
}else{
model.addAttribute("msg","更新数据失败!");
return "status";
}
}
service代码增加
public int updateEmp(Emp emp){
return empMapper.updateEmp(emp);
}
empMapper代码增加
@Update("update emp set ename=#{ename},address=#{address},birth=#{birth} where eid=#{eid}")
public int updateEmp(Emp emp);
最后实现业务操作