当前位置: 首页 > news >正文

Springboot怎么快速集成Mybatis和thymeleaf?

前言

有时候做方案,需要模拟一些业务上的一些场景来验证方案的可行性,基本上每次都是到处百度如何集成springboot+mybatis+thymeleaf这些东西的集成平时基本上一年也用不了一次,虽然比较简单,奈何我真得记不住详细的每一步,因此每次都是从零开始,我一直在想,把时间浪费在这种重复的事情是没有意义的,所以这篇文章记录一下,以后再也不到处百度来接拼凑了。

目标

springboot中集在mybatis和thymeleaf,简单实现一下新增和查询功能,后续有需要再往上补。

环境配置

jdk版本:1.8

开发工具:Intellij iDEA 2020.1

springboot:2.3.9.RELEASE

具体步骤

依赖引入

主要引入了springboot、thymeleaf、mybais、mysql、jdbc以及热部署和lombda相关的依赖;

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope>
</dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency><groupId>ognl</groupId><artifactId>ognl</artifactId><version>3.1.26</version>
</dependency>
<dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId>
</dependency>
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><optional>true</optional>
</dependency>
<dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.1.4</version>
</dependency>
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId>
</dependency>

配置文件

配置文件这里新增了三处配置,分别是thymeleaf、数据库连接、mybatis;

#thymeleaf配置
spring.thymeleaf.cache=false
spring.thymeleaf.suffix=.html
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.servlet.content-type=text/html
spring.thymeleaf.encoding=utf-8
#数据库连接配置
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3307/happy_home?serverTimezone=Asia/Shanghai 
spring.datasource.username=root
spring.datasource.password=root
#mybatis配置
mybatis.mapper-locations=classpath:/mapper/*.xml
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

前端代码

1、resources/static目录下,新增静态文件index.html;

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
<div id="testContent"><form id="registeForm" name="registe" method="post" action="/person//registe" enctype="multipart/form-data"target="_self">登陆名: <input name="loginNo" type="text"/><br/>姓名: <input name="userName" type="text"/><br/>性别: <input name="sex" type="radio" value="男"/>男<input name="sex" type="radio" value="女"/>女<br/>手机号码: <input name="phoneNumber" type="text"/><br/>身份证号: <input name="IDCard" type="text"/><br/>地址: <input name="address" type="text"/><br/>门牌号: <input name="houseNumber" type="text"/><br/><input type="submit" value="提交" id="submitForm"></form>
</div>
</body>
</html>

2、resources/templates目录上,新增home.html文件;

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"/><title>主页</title><script type="text/javascript" src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
</head>
<body>
<div>ID:<span id="personId" data-th-text="${id}" ></span>
</div>
<div id="personInfo"><p>登陆名:<span></span></p><p>姓名:<span></span></p><p>性别:<span></span></p><p>手机号码:<span></span></p><p>身份证号:<span></span></p><p>地址:<span></span></p><p>门牌号:<span></span></p>
</div>
</body>
<script type="text/javascript">var id=$('#personId').text();$.ajax({url: 'http://localhost:8080/person/'+id,method: 'get',success: function (res) {console.log(res)$('#personInfo p')[0].append(res.loginNo);$('#personInfo p')[1].append(res.userName);$('#personInfo p')[2].append(res.sex);$('#personInfo p')[3].append(res.phoneNumber);$('#personInfo p')[4].append(res.idcard);$('#personInfo p')[5].append(res.address);$('#personInfo p')[6].append(res.houseNumber);}})
</script>
</html>

后端代码

1、PersonController.java

@Controller
@RequestMapping("/person")
public class PersonController {@Autowiredprivate IPersonService personService;@PostMapping("/registe")public String registe(Person person, Model model) {Integer id = this.personService.registe(person);model.addAttribute("id", id);return "home";}@GetMapping("/{id}")@ResponseBodypublic Person getPerson(@PathVariable("id") Integer id) {Person person = this.personService.get(id);return person;}
}

2、IPersonService.java

public interface IPersonService {Integer registe(Person person);Person get(Integer id);
}

3、PersonServiceImpl.java

@Service
public class PersonServiceImpl implements IPersonService {@Autowiredprivate PersonDao personDao;@Overridepublic Integer registe(Person person) {this.personDao.insert(person);return person.getId();}@Overridepublic Person get(Integer id) {Person person=personDao.selectById(id);return person;}
}

4、PersonDao.java

@Mapper
public interface PersonDao {Integer insert(Person person);Person selectById(Integer id);
}

5、PersonMapper.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.fanfu.dao.PersonDao"><resultMap id="personMap" type="com.fanfu.entity.Person"><result column="user_name" property="userName"></result><result column="login_no" property="loginNo"></result><result column="sex" property="sex"></result><result column="phone_number" property="phoneNumber"></result><result column="address" property="address"></result><result column="house_number" property="houseNumber"></result><result column="ID_card" property="IDCard"></result><result column="id" property="id"></result></resultMap><insert id="insert" parameterType="com.fanfu.entity.Person" keyProperty="id" keyColumn="id" useGeneratedKeys="true">insert into sys_person(user_name, login_no, phone_number, sex, ID_card, address, house_number)values (#{userName}, #{loginNo}, #{phoneNumber}, #{sex}, #{IDCard}, #{address}, #{houseNumber})</insert><select id="selectById" resultMap="personMap">select *from sys_personwhere id = #{id}</select>
</mapper>

6、Person.java

@Slf4j
@Data
public class Person  {private Integer id;private String userName;private String loginNo;private String phoneNumber;private String sex;private String IDCard;private String address;private String houseNumber;
}

相关文章:

Springboot怎么快速集成Mybatis和thymeleaf?

前言有时候做方案&#xff0c;需要模拟一些业务上的一些场景来验证方案的可行性&#xff0c;基本上每次都是到处百度如何集成springbootmybatisthymeleaf这些东西的集成平时基本上一年也用不了一次&#xff0c;虽然比较简单&#xff0c;奈何我真得记不住详细的每一步&#xff0…...

shell常见面试题一

&#xff08;1&#xff09;、set //查看系统变量 &#xff08;2&#xff09;、chsh -s /bin/zsh test //修改用户登录shell &#xff08;3&#xff09;、2>&1 //标准错误重定向到标准输出 &> //同样可以将标准错误重定向到标准输出 如下&#xff1a; ls test.…...

python如何快速采集美~女视频?无反爬

人生苦短 我用python~ 这次康康能给大家整点好看的不~ 环境使用: Python 3.8 Pycharm mou歌浏览器 mou歌驱动 —> 驱动版本要和浏览器版本最相近 <大版本一样, 小版本最相近> 模块使用: requests >>> pip install requests selenium >>> pip …...

kali内置超好用的代理工具proxychains

作者&#xff1a;Eason_LYC 悲观者预言失败&#xff0c;十言九中。 乐观者创造奇迹&#xff0c;一次即可。 一个人的价值&#xff0c;在于他所拥有的。所以可以不学无术&#xff0c;但不能一无所有&#xff01; 技术领域&#xff1a;WEB安全、网络攻防 关注WEB安全、网络攻防。…...

Java栈和队列·下

Java栈和队列下2. 队列(Queue)2.1 概念2.2 实现2.3 相似方法的区别2.4 循环队列3. 双端队列 (Deque)3.1 概念4.java中的栈和队列5. 栈和队列面试题大家好&#xff0c;我是晓星航。今天为大家带来的是 Java栈和队列下 的讲解&#xff01;&#x1f600; 继上一个讲完的栈后&…...

b01lers CTF web 复现

warmup 按照提示依次 base64 加密后访问&#xff0c;可以访问 ./flag.txt&#xff0c;也就是 Li9mbGFnLnR4dA 。 from base64 import b64decode import flaskapp flask.Flask(__name__)app.route(/<name>) def index2(name):name b64decode(name)if (validate(name))…...

三月份跳槽了,历经字节测开岗4轮面试,不出意外,被刷了...

大多数情况下&#xff0c;测试员的个人技能成长速度&#xff0c;远远大于公司规模或业务的成长速度。所以&#xff0c;跳槽成为了这个行业里最常见的一个词汇。 前几天&#xff0c;我看到有朋友留言说&#xff0c;他在面试字节的测试开发工程师的时候&#xff0c;灵魂拷问三小…...

springboot+vue驾校管理系统 idea科目一四预约考试,练车

加大了对从事道路运输经营活动驾驶员的培训管理力度&#xff0c;但在实际的管理过程中&#xff0c;仍然存在以下问题&#xff1a;(1)管理部门内部人员在实际管理过程中存在人情管理&#xff0c;不进行培训、考试直接进行发证。(2)从业驾驶员培训机构不能严格执行管理部门的大纲…...

【pytorch】使用deepsort算法进行目标跟踪,原理+pytorch实现

目录deepsort流程一、匈牙利算法二、卡尔曼滤波车速预测例子动态模型的概念卡尔曼滤波在deepsort中的动态模型三、预测值及测量值的含义deepsort在pytorch中的运行deepsort流程 DeepSORT是一种常用的目标跟踪算法&#xff0c;它结合了深度学习和传统的目标跟踪方法。DeepSORT的…...

Python 基础教程【3】:字符串、列表、元组

本文已收录于专栏&#x1f33b;《Python 基础》文章目录&#x1f315;1、字符串&#x1f95d;1.1 字符串基本操作&#x1f34a;1.1.1 字符串创建&#x1f34a;1.1.2 字符串元素读取&#x1f34a;1.1.3 字符串分片&#x1f34a;1.1.4 连接和重复&#x1f34a;1.1.5 关系运算&…...

(数据结构)八大排序算法

目录一、常见排序算法二、实现1. 直接插入排序2.&#x1f31f;希尔排序3. 选择排序4.&#x1f31f;堆排序5. 冒泡排序7. &#x1f31f;快速排序7.1 其他版本的快排7.2 优化7.3 ⭐非递归7. &#x1f31f;归并排序7.1 ⭐非递归8. 计数排序三、总结1. 分析排序 (Sorting) 是计算机…...

构建GRE隧道打通不同云商的云主机内网

文章目录1. 环境介绍2 GRE隧道搭建2.1 华为云 GRE 隧道安装2.2 阿里云 GRE 隧道安装3. 设置安全组4. 验证GRE隧道4.1 在华为云上 ping 阿里云云主机内网IP4.2 在阿里云上 ping 华为云云主机内网IP5. 总结1. 环境介绍 华为云上有三台云主机&#xff0c;内网 CIDR 是 192.168.0.0…...

48天C++笔试强训 001

作者&#xff1a;小萌新 专栏&#xff1a;笔试强训 作者简介&#xff1a;大二学生 希望能和大家一起进步&#xff01; 本篇博客简介&#xff1a;讲解48天笔试强训第一天的题目 笔试强训 day1选择题12345678910编程题12选择题 1 以下for循环的执行次数是&#xff08;&#xff…...

Android 11新增系统服务

1.编写.aidl文件存放位置&#xff1a;frameworks/base/core/java/android/ospackage android.os;interface ISystemVoiceServer {void setHeightVoice(int flag);void setBassVoice(int flag);void setReverbVoice(int flag);}2.将.aidl文件添加到frameworks/base/Android.bp f…...

“你要多弄弄算法”

开始瞎掰 ▽ 2月的第一天&#xff0c;猎头Luna给我推荐了字节的机会&#xff0c;菜鸡我呀&#xff0c;还是有自知之明的&#xff0c;赶忙婉拒&#xff1a;能力有限&#xff0c;抱歉抱歉。 根据我为数不多的和猎头交流的经验&#xff0c;一般猎头都会稍微客套一下&#xff1a…...

【数据结构】千字深入浅出讲解队列(附原码 | 超详解)

&#x1f680;write in front&#x1f680; &#x1f4dd;个人主页&#xff1a;认真写博客的夏目浅石. &#x1f381;欢迎各位→点赞&#x1f44d; 收藏⭐️ 留言&#x1f4dd; &#x1f4e3;系列专栏&#xff1a;C语言实现数据结构 &#x1f4ac;总结&#xff1a;希望你看完…...

vue面试题(day04)

vue面试题vue插槽&#xff1f;vue3中如何获取refs&#xff0c;dom对象的方式&#xff1f;vue3中生命周期的和vue2中的区别&#xff1f;说说vue中的diff算法&#xff1f;说说 Vue 中 CSS scoped 的原理&#xff1f;vue3中怎么设置全局变量&#xff1f;Vue中给对象添加新属性时&a…...

自动标注工具 Autolabelimg

原理简介~~ 对于数据量较大的数据集&#xff0c;先对其中一部分图片打标签&#xff0c;Autolabelimg利用已标注好的图片进行训练&#xff0c;并利用训练得到的权重对其余数据进行自动标注&#xff0c;然后保存为xml文件。 一、下载yolov5v6.1 https://github.com/ultralytic…...

2023-03-20干活

transformer复现 from torch.utils.data import Dataset,DataLoader import numpy as np import torch import torch.nn as nn import os import time import math from tqdm import tqdmdef get_data(path,numNone):all_text []all_label []with open(path,"r",e…...

Java 注解(详细学习笔记)

注解 注解英文为Annotation Annotation是JDK5引入的新的技术 Annotation的作用&#xff1a; 不是程序本身&#xff0c;可以对程序做出解释可以被其他程序&#xff08;比如编译器&#xff09;读取。 Annotation的格式&#xff1a; 注解是以注解名在代码中存在的&#xff0c;还…...

威联通NAS安全防护全攻略:10个必做设置让你的数据固若金汤

威联通NAS安全防护全攻略&#xff1a;10个必做设置让你的数据固若金汤 在数字化时代&#xff0c;数据安全已成为个人和企业最关注的议题之一。威联通NAS作为专业级网络存储设备&#xff0c;凭借其强大的硬件性能和丰富的软件生态&#xff0c;成为许多用户存储重要数据的首选。然…...

SimCLR揭秘:自监督学习中的对比学习艺术

1. 自监督学习与对比学习的革命性结合 第一次听说SimCLR这个名词时&#xff0c;我正被海量无标注图像数据的处理问题困扰。传统监督学习需要大量人工标注&#xff0c;成本高得吓人。而SimCLR的出现&#xff0c;就像给计算机视觉领域投下了一颗震撼弹——原来模型可以自己教自己…...

拦截器与 JWT 联合使用详解

1. 核心概念1.1 什么是 JWT&#xff1f;JWT 是一个开放标准&#xff08;RFC 7519&#xff09;&#xff0c;用于在各方之间以 JSON 对象的形式安全地传输信息。该信息可以被验证和信任&#xff0c;因为它是数字签名的。JWT 结构&#xff1a;Header&#xff08;头部&#xff09;&…...

一、BLE入门:从广播信道到报文解析,构建无线连接基石

1. BLE技术入门&#xff1a;无线世界的敲门砖 第一次接触BLE技术时&#xff0c;我完全被那些专业术语搞懵了。什么广播信道、报文解析&#xff0c;听起来就像天书一样。但当我真正动手调试一个智能手环项目后&#xff0c;才发现BLE其实就像两个人在嘈杂的教室里传纸条——需要…...

从‘够用’到‘好用’:聊聊Artix-7 FPGA在工业视频处理中的那些‘甜点’级设计

从‘够用’到‘好用’&#xff1a;Artix-7 FPGA在工业视频处理中的设计哲学 工业视频处理领域正经历一场静默的革命——当4K/8K超高清、120fps高帧率成为行业热词时&#xff0c;真正推动生产线变革的却是那些在成本与性能间找到完美平衡点的解决方案。Artix-7系列FPGA&#xf…...

STC89C52抢答器DIY避坑指南:从万能板焊接调试到常见故障排查(蜂鸣器不响、按键失灵)

STC89C52抢答器DIY避坑指南&#xff1a;从万能板焊接调试到常见故障排查 在电子制作领域&#xff0c;抢答器是一个经典的单片机实践项目。不同于市面上现成的模块化套件&#xff0c;使用万能板手工焊接STC89C52抢答器不仅能深入理解电路原理&#xff0c;更能锻炼实际动手能力。…...

COMSOL数据可视化避坑指南:如何用SciPy的griddata处理不规则网格数据?

COMSOL数据可视化避坑指南&#xff1a;如何用SciPy的griddata处理不规则网格数据&#xff1f; 当你从COMSOL导出电场、温度场或其他物理场数据时&#xff0c;是否遇到过这样的困扰&#xff1a;明明在COMSOL中看起来光滑连续的场分布&#xff0c;导出到MATLAB或Python中绘制时却…...

掌握LiteDB.Studio:嵌入式文档数据库可视化管理工具全攻略

掌握LiteDB.Studio&#xff1a;嵌入式文档数据库可视化管理工具全攻略 【免费下载链接】LiteDB.Studio A GUI tool for viewing and editing documents for LiteDB v5 项目地址: https://gitcode.com/gh_mirrors/li/LiteDB.Studio 在现代软件开发中&#xff0c;嵌入式数…...

2026年高压电磁阀销售厂家哪家强?口碑好才是真的香

在工业阀门领域&#xff0c;高压电磁阀是许多高难度、复杂工况下的关键设备。随着技术的不断进步和市场需求的增加&#xff0c;选择一家优质的高压电磁阀销售厂家显得尤为重要。本文将从多个维度对比分析几家主要的高压电磁阀生产厂家&#xff0c;并给出实操建议&#xff0c;帮…...

LockSupport深度解析:线程阻塞与唤醒的底层实现原理

在技术领域&#xff0c;我们常常被那些闪耀的、可见的成果所吸引。今天&#xff0c;这个焦点无疑是大语言模型技术。它们的流畅对话、惊人的创造力&#xff0c;让我们得以一窥未来的轮廓。然而&#xff0c;作为在企业一线构建、部署和维护复杂系统的实践者&#xff0c;我们深知…...