目录结构如图

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

  

Spring mybatis thymeleaf 基础操作,实现数据展示,修改,删除,查询的更多相关文章

  1. MYSQL基础操作之数据约束与关联查询

    一.MYSQL约束 1.默认值约束,当字段没有插入值的时候,mysql自动给该字段分配默认值. 默认值的字段允许为空. 对默认值字段也可以插入null. CREATE TABLE STUDENT( I ...

  2. UITaleView的基础使用及数据展示操作

    UITableView表视图,是实用的数据展示的基础控件,是继承于UIScrollView,所以也可以滚动.但不同于UIScrollView,UITableView只可以上下滚动,而不能左右滚动. 因 ...

  3. idea+springmvc+spring+mybatis+maven整合返回json数据webapi

    首先看一张目录结构图: : 创建步骤: 1.创建maven  webapp工程, 创建完后的目录结构为: 2.添加项目依赖(添加jar包) 需要的jar包: spring-webmvc, spring ...

  4. layui数据表格使用(一:基础篇,数据展示、分页组件、表格内嵌表单和图片)

    表格展示神器之一:layui表格 前言:在写后台管理系统中使用最多的就是表格数据展示了,使用表格组件能提高大量的开发效率,目前主流的数据表格组件有bootstrap table.layui table ...

  5. oracle navicat 可视化操作进行数据的修改

    在进行oracle数据库中的数据操作编辑时,需要小心.oracle内置的安全机制是无处不在,并且很有必要存在的. 使用navicat对oracle中数据进行select操作时,查询出的结果是只读的,这 ...

  6. mysql常用基础操作语法(十)~~子查询【命令行模式】

    mysql中虽然有连接查询实现多表连接查询,但是连接查询的性能很差,因此便出现了子查询. 1.理论上,子查询可以出现在查询语句的任何位置,但实际应用中多出现在from后和where后.出现在from后 ...

  7. SpringMVC + Spring + MyBatis 学习笔记:提交数据遭遇基础类型和日期类型报400错误解决方法

    系统:WIN8.1 数据库:Oracle 11GR2 开发工具:MyEclipse 8.6 框架:Spring3.2.9.SpringMVC3.2.9.MyBatis3.2.8 使用SpringMVC ...

  8. mysql基础操作(二):简单查询DQL

    -- 1.查询所有字段 select * from student; -- 2.查询指定的字段 select id from student; select id, name from student ...

  9. EXTJS4.2 内存中操作表格数据时,删除表格数据,行号不连续解决

    需要重新刷新下表格的view => grid.view.refresh();

随机推荐

  1. 用JS实现输出两个数范围内的随机数

    const rs = require("readline-sync"); function roundNum(min = 0, max = 0) { if (!isNaN(min) ...

  2. 服务端php之文件上传

    知识点 echo $_SERVER['PHP_SELF']; 自动获取当前文件的路劲(即提交地址为当前页面) 当一个表单有文件域(即文件上传)的时候,method(提交方式)要设置post,这样更加安 ...

  3. Java 之 Servlet中的生命周期

    Servlet 生命周期 一.重写servlet方法 当创建一个类,继承 servlet 这个接口时,需要实现里面的抽象方法. import javax.servlet.*; import java. ...

  4. linux防火墙开放端口,针对固定ip开放端口

    编辑/etc/sysconfig/iptables,添加 -A INPUT -m state --state NEW -m tcp -p tcp -s 127.0.0.1 --dport 6379 - ...

  5. MyBatis 多表连接查询

    多表连接的两种方式(数据库逻辑模型): 1.一对一关系 2.一对多关系 一.通过 resultMap 和 association 实现一对一关系 在 mapper.xml 文件里面的代码: <r ...

  6. 铁力项目mysql异常处理过程记录

    地区:铁力 故障:2019-06-26 10:19:34 139921514837760 [ERROR] mysqld: Error writing file 'mysql-bin' (errno: ...

  7. oracle中日期相关的区间

    and czrqb.lsrqb_rh_sj >= to_date('[?query_date_begin|2011-09-01?]A', 'yyyy-mm-dd') and czrqb.lsrq ...

  8. 3. 控制反转(IoC)和依赖注入(DI)

    1).控制反转是应用于软件工程领域中的,在运行时被装配器对象来绑定耦合对象的一种编程技巧,对象之间耦合关系在编译时通常是未知的.在传统的编程方式中,业务逻辑的流程是由应用程序中的早已被设定好关联关系的 ...

  9. Openwrt路由器上安装python

    在路由器安装python之前,还是经过了一番折腾的.淘宝上买了个已经刷好系统的小米迷你路由器,但里面安装的不是预期的Pandorbox,而是LEDE. 这个固件已经带了大量自带的软件,128的内存实在 ...

  10. js遍历删除数组中不符合条件的元素

    //一般解决方法 let arr = [1,2,3]; for(let i=0; i<arr.length; i++){ if(arr[i]==2){ arr.splice(i, 1); i-- ...