springboot整合mybatis与thymeleaf
1.创建springboot项目
(1)选择Spring Initializr

(2)填写自己的Group 与 Artifact

(3)选择依赖框架

等待maven下载好依赖和插件即可
2.主配置文件(这里使用的是更为简洁的ylm文件)
application.yml
主要配置了数据源和mapper路径与实体类别名
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
username: root
password: 330069879
mybatis:
mapper-locations: classpath:/mapper/*.xml
type-aliases-package: com.datatest.signup.entity
3.实体类与表的映射关系
Person.java
package com.datatest.signup.entity;
public class Person {
private String id;
private String name;
private int age;
private int sex;
public void setId(String id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void setSex(int sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Person{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", age=" + age +
", sex=" + sex +
'}';
}
}
数据库表person

4.mapper.xml 与 PersonMapper
<?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.datatest.signup.mapper.PersonMapper">
<select id="queryPersonById" resultType="Person" parameterType="String">
select * from person where id=#{id}
</select>
</mapper>
PersonMapper.java
package com.datatest.signup.mapper; import com.datatest.signup.entity.Person;
import org.springframework.stereotype.Repository; @Repository
public interface PersonMapper {
Person queryPersonById(String id);
}
5.Service层
PersonService.java
package com.datatest.signup.service;
import com.datatest.signup.entity.Person;
public interface PersonService {
public Person queryPersonById(String id);
}
package com.datatest.signup.service.impl; import com.datatest.signup.entity.Person;
import com.datatest.signup.mapper.PersonMapper;
import com.datatest.signup.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; @Service
public class PersonServiceImpl implements PersonService {
@Autowired
private PersonMapper personMapper; @Override
public Person queryPersonById(String id) {
return personMapper.queryPersonById(id);
}
}
6.controller层
package com.datatest.signup.controller; import com.datatest.signup.entity.Person;
import com.datatest.signup.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping; import java.util.Map; @Controller
public class StudentController { @Autowired
private PersonService personService; @RequestMapping("query/{id}")
public String queryPersonById(@PathVariable String id, Map<String,Object> map){
Person person = personService.queryPersonById(id);
System.out.println(person);
map.put("person",person);
return "result";
}
}
7.前段页面
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>sprint boot</title>
</head>
<body>
<a href="query/P01">查询一个人</a>
</body>
</html>
result.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>result</title>
</head>
<body>
<p th:text="${person}">没有查询到该人</p>
</body>
</html>
8.主程序文件
package com.datatest.signup; import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @MapperScan("com.datatest.signup.mapper")
@SpringBootApplication
public class SignupApplication { public static void main(String[] args) {
SpringApplication.run(SignupApplication.class, args);
} }
9.测试效果


springboot整合mybatis与thymeleaf的更多相关文章
- JavaEE开发之SpringBoot整合MyBatis以及Thymeleaf模板引擎
上篇博客我们聊了<JavaEE开发之SpringBoot工程的创建.运行与配置>,从上篇博客的内容我们不难看出SpringBoot的便捷.本篇博客我们继续在上篇博客的基础上来看一下Spri ...
- SpringBoot整合MyBatis及Thymeleaf
http://www.cnblogs.com/ludashi/archive/2017/05/08/6669133.html 上篇博客我们聊了<JavaEE开发之SpringBoot工程的创建. ...
- SpringBoot整合Jsp和Thymeleaf (附工程)
前言 本篇文章主要讲述SpringBoot整合Jsp以及SpringBoot整合Thymeleaf,实现一个简单的用户增删改查示例工程.事先说明,有三个项目,两个是单独整合的,一个是将它们整合在一起的 ...
- springBoot整合mybatis、jsp 或 HTML
springBoot整合mybatis.jsp Spring Boot的主要优点: 1: 为所有Spring开发者更快的入门: 2: 开箱即用,提供各种默认配置来简化项目配置: 3: 内嵌式容器 ...
- SpringBoot整合Mybatis完整详细版二:注册、登录、拦截器配置
接着上个章节来,上章节搭建好框架,并且测试也在页面取到数据.接下来实现web端,实现前后端交互,在前台进行注册登录以及后端拦截器配置.实现简单的未登录拦截跳转到登录页面 上一节传送门:SpringBo ...
- SpringBoot整合Mybatis完整详细版
记得刚接触SpringBoot时,大吃一惊,世界上居然还有这么省事的框架,立马感叹:SpringBoot是世界上最好的框架.哈哈! 当初跟着教程练习搭建了一个框架,传送门:spring boot + ...
- SpringBoot整合mybatis、shiro、redis实现基于数据库的细粒度动态权限管理系统实例
1.前言 本文主要介绍使用SpringBoot与shiro实现基于数据库的细粒度动态权限管理系统实例. 使用技术:SpringBoot.mybatis.shiro.thymeleaf.pagehelp ...
- springboot整合mybatis(SSM开发环境搭建)
0.项目结构: ---------------------方法一:使用mybatis官方提供的Spring Boot整合包实现--------------------- 1.application.p ...
- 三、SpringBoot 整合mybatis 多数据源以及分库分表
前言 说实话,这章本来不打算讲的,因为配置多数据源的网上有很多类似的教程.但是最近因为项目要用到分库分表,所以让我研究一下看怎么实现.我想着上一篇博客讲了多环境的配置,不同的环境调用不同的数据库,那接 ...
随机推荐
- python异常处理(学习)
异常处理可以保证程序在恶劣的环境也能运行,或者给用户相关的提示 如下是运行异常的情况 如无异常 也可以创建异常处理类型
- c++生成的动态库移到其他电脑上,动态库不能运行
最近的一个项目中遇到了一个问题,C++的一个动态库在我自己的电脑上可以被C#程序引用,我把程序安装到其他电脑上出现了异常,提示找不到DLL,偶然间发现我安装vsc++,C#的程序就不会报错.因为这个C ...
- Mysql8.0免安装包配置方法
1. 官网下载mysql_8.0.12免安装包,解压到你存放的地方: https://www.jb51.net/softs/609101.html 2. 配置环境变量(把bin的文件夹弄进系统path ...
- PHPXhprof扩展在windows安装
1.下载在这里 http://dev.freshsite.pl/php-extensions/xhprof.html.(找不到资源可以私我我给你,这个上传不了资源) 注意:一定要找对应的php版本,t ...
- 什么是IPFS?IPFS与区块链有什么关系
1.什么是IPFS? IPFS是Inter Planetary File System(星际文件系统)的缩写,是一个典型的点对点分布式文件系统, 旨在用同一个文件系统连接所有的计算设备.这时候有些小伙 ...
- 7.1 Varnish VCL
根据以上的配置增加集群,修改default.vcl # This ) # man page for details on VCL syntax and semantics. # # Default b ...
- 13.在项目中部署redis企业级数据备份方案以及各种踩坑的数据恢复容灾演练
到这里为止,其实还是停留在简单学习知识的程度,学会了redis的持久化的原理和操作,但是在企业中,持久化到底是怎么去用得呢? 企业级的数据备份和各种灾难下的数据恢复,是怎么做得呢? 1.企业级的持久化 ...
- JAVA学习笔记-数组的三种初始化方式
package Study; public class TestArray02 { public static void main(String[] args){//声明 int[] a; int ...
- 解决fedora28桌面图标问题
正文 在fedora28中默认是没有桌面图标的,对于那些习惯使用桌面的图标的人来说使用有点不适应. 替代方法是: 下载nemo,在终端内输入sudo dnf install nemo 创建~/.con ...
- ACM-吴奶奶买鱼
题目描述:吴奶奶买鱼 吴奶奶有个可爱的外孙女——琪琪,她很喜欢小动物,尤其喜欢养鱼.为了让小孙女养到漂亮的小鱼,吴奶奶一大早就到花鸟鱼虫市场买鱼.这个市场可真大,里面有各种各样的宠物,就连宠物鱼都 ...