目录结构如图
index.html
<!DOCTYPE html>
<html lang="en"
xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<script>
function tvClick() {
var x=document.getElementById("tv").value;
//window.location.href="http://localhost:8080/search?id="+x;
window.location.href="search?id="+x;
}
</script>
<body>
<div th:width="300px" th:height="50px">
<a th:href="@{/edit(user_id=${null})}"> 添加新数据 </a>
</div>
<div th:width="300px" th:height="50px">
<input th:width="300px" th:height="50px" id="tv" th:value="${keyValue}"> </input> <button id="btn" onclick="tvClick()">查询</button>
</div>
<table border="1">
<tr>
<th>id</th>
<th>姓名</th>
<th>年龄</th>
<th>修改</th>
<th>删除</th>
</tr>
<tr th:each="student:${students}">
<td><span th:text="${student.id}"></span></td>
<td><span th:text="${student.name}"></span></td>
<td><span th:text="${student.age}"></span></td>
<td><a th:href="@{/edit(id=${student.id})}"> edit </a></td>
<td><a th:href="@{/del(user_id=${student.id})}"> delete </a></td>
</tr>
</table>
</body>
</html>
add.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>新增、编辑客户</title>
</head>
<body>
<form th:action="@{/save}" method="post">
<div>
<label>id</label>
<input type="text" name="id" readonly="readonly" th:field="${student.id}" />
</div>
<div>
<label>name</label>
<input type="text" name="name" th:field="${student.name}" />
</div>
<div>
<label>age</label>
<input type="text" name="age" th:field="${student.age}" />
</div>
<div>
<input type="submit" value="提交" />
</div>
</form>
</body>
</html>
mapper/student.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.gali.thymeleaf.mapper.StudentMapper">
<select id="findAll" resultType="java.util.Map">
select * from student
</select>
<select id="findAll2" resultType="java.util.Map" parameterType="Integer">
select * from student where id > #{id}
</select>
<delete id="delById" parameterType="Integer">
delete from student where id = #{user_id}
</delete>
<select id="findById" parameterType="Integer" resultType="com.gali.thymeleaf.entity.Student">
select * from student where id= #{id}
</select>
<insert id="create" parameterType="com.gali.thymeleaf.entity.Student">
insert into student ( name,age)
values (#{name},#{age})
<!-- selectKEY 用于回填数据 keyProperty 主键 keycolume是字段名 resultType 是字段类型 order 是指定在执行sql前或后返回数据-->
<selectKey keyProperty="id" keyColumn="id" resultType="Integer" order="AFTER">
select Last_INSERT_ID()
</selectKey>
</insert>
<update id="update" parameterType="com.gali.thymeleaf.entity.Student">
update student set name = #{name} , age= #{age} where id= #{id}
</update>
</mapper>
StudentHtmlController
这里使用@Controller ,不再使用@RestController
@Controller
@RequestMapping(path = "/")
public class StudentHtmlController {
@Autowired
StudentService studentService;
@RequestMapping(path = "/index" , method = RequestMethod.GET)
public String getHtml(Model model){
model.addAttribute("students" ,studentService.findAll());
return "index";
}
@RequestMapping(path = "/search" , method = RequestMethod.GET)
public ModelAndView getIndex(@RequestParam("id") Integer id){
ModelAndView av=new ModelAndView("index");
av.addObject("students",studentService.findAll2(id));
av.addObject("keyValue",id);
return av;
}
@RequestMapping(path = "/del")
public String del(@RequestParam(name = "user_id") Integer user_id){
studentService.delById(user_id);
return "redirect:/index";
}
@RequestMapping(path = "/save" ,method = RequestMethod.POST)
public String save(@ModelAttribute Student student){
if(student==null){
return "fail";
}
if(student.id!=null && student.id > 0){
studentService.update(student);
return "redirect:/index";
}else{
studentService.create(student);
return "redirect:/index";
}
}
@RequestMapping(path = "/findById" ,method = RequestMethod.GET)
public Student findById(@RequestParam("id") Integer id){
return studentService.findById(id);
}
@RequestMapping(path = "/edit" , method = RequestMethod.GET)
public String edit(ModelMap modelMap ,@RequestParam(defaultValue = "0") int id){
if(id>0){
modelMap.addAttribute("student",studentService.findById(id));
}else{
Student student=new Student();
student.setAge(null);
student.setName("");
modelMap.addAttribute("student",student);
}
return "update";
}
}
ThymeleafApplication
使用@MapperScan 扫描Mapper 包路径
package com.gali.thymeleaf;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.gali.thymeleaf.mapper")
public class ThymeleafApplication {
public static void main(String[] args) {
SpringApplication.run(ThymeleafApplication.class, args);
}
}
效果图
完整Code(thymeleaf)
链接:https://pan.baidu.com/s/1iOT2a59NFkppoNJOq4MIbg
提取码:47og