2流高手速成记(之三):SpringBoot整合mybatis/mybatis-plus实现数据持久化
接上回
上一篇我们简单介绍了基于SpringBoot实现简单的Web开发,本节来看Web开发中必不可少的内容——数据持久化
先看项目结构:

1. 创建数据表
打开mysql,打开数据库 test (没有可以创建一个),创建表格 person
给 person 表创建两个字段 id、name

2. 打开 pom.xml,添加相关依赖
<!-- 引入mybatis、mybatis-plus、mysql等依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
mybatis-spring-boot-starter 满足了 mybatis在springboot下的拆箱即用
mybatis-plus-boot-starter 实现了 mybatis-plus 的自动化配置,同样拆箱即用
注意:是mybatis-plus-boot-starter,不是mybatis-plus;前者包含后者的引用,如果只引用后者执行程序会报错!
由于mybatis-plus是基于mybatis的,所以两者引用缺一不可
mysql-connector-java 是基础的mysql驱动接口,这个也是不可或缺的
mybatis是安全、优秀的java持久层框架,基于xml可灵活定制sql语句
mybatis-plus在mybatis的基础上做了更进一步的简化,可免去xml编写
同时,mybatis-plus遵循非侵入式设计的原则,即完全兼容原mybatis的使用习惯,非常方便
3. 给application.properties添加数据库配置
# mysql相关设置
spring.datasource.username=admin
spring.datasource.password=admin
spring.datasource.url=jdbc:mysql://localhost:3306/test?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
到这里可能有人会问,咋没看到mybatis.xml的配置?不是一般都会有一句:
#指定Mybatis的Mapper文件
mybatis.mapper-locations=classpath:mapper/*xml
如果我们使用mybatis的原生功能,这一句配置是需要加上的,但是如果我们基于mybatis-plus,可以先不加这一句,因为它是免xml配置的!
4. 新建 model/Person
package com.example.hellospringboot.model;
public class Person {
private Integer id = 0;
private String name = "";
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
注意:类名 Person 要和数据库表名 person 一致(首字母大写是Java的类命名规则,这个没有问题)
id和name两个字段的名称和类型也要和数据库保持一致
5. 新建 mapper/PersonMapper
package com.example.hellospringboot.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.hellospringboot.model.Person;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository; @Mapper
@Repository
public interface PersonMapper extends BaseMapper<Person> {
}
这里让PersonMapper继承自mybatis-plus提供的BaseMapper,这是启用mybatis-plus免xml特性的关键!
BaseMapper为我们定制常用的数据库增删改查的方法,直接继承使用即可!
6. 新建 service/PersonService 接口及其实现类 service/impl/PersonServiceImpl
package com.example.hellospringboot.service; import com.example.hellospringboot.model.Person;
import java.util.List; public interface PersonService {
Integer insert(Person person);
Integer update(Person person);
Integer delete(int id);
List<Person> select();
}
package com.example.hellospringboot.service.impl; import com.example.hellospringboot.mapper.PersonMapper;
import com.example.hellospringboot.model.Person;
import com.example.hellospringboot.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import java.util.List; @Service
public class PersonServiceImpl implements PersonService { @Autowired
PersonMapper mapper; public Integer insert(Person person){
return mapper.insert(person);
} public Integer update(Person person){
return mapper.updateById(person);
} public Integer delete(int id){
return mapper.deleteById(id);
} public List<Person> select(){
return mapper.selectList(null);
}
}
我们给mapper新增了@Repository注解,可以让Service自动装载Mapper不报错
通过代码我们可以看到,继承自BaseMapper<Person>的PersonMapper,不加任何代码不写任何xml,就可以支持Person数据模型的常见的增删改查等操作,真的非常方便!
7. 新建 controller/PersonController
package com.example.hellospringboot.controller; import com.example.hellospringboot.model.Person;
import com.example.hellospringboot.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController
@RequestMapping("/person")
public class PersonController { @Autowired
PersonService service; @PostMapping("/insert")
public Integer insert(Person person){
return service.insert(person);
} @PostMapping("/update")
public Integer update(Person person){
return service.update(person);
} @PostMapping("/delete")
public Integer delete(int id){
return service.delete(id);
} @GetMapping("/select")
public List<Person> select(){
return service.select();
}
}
我们这里使用了@RestController注解,这样可以非常方便的测试我们的业务逻辑
这里可以看到,insert、update、delete三个写方法我们使用了Post协议,select读方法使用了Get协议
其实标准的RestApi风格另外还有Put和Delete协议,这里其实没有严格的规定
由于Get协议的参数是直接暴露在url串里的,所以一般写方法我们不建议使用Get协议
8. 使用Postman测试结果



我们在请求参数中分别传入id和name,springboot框架会自动将其拼装成Person对象,真的是非常智能化!
另外,得益于mybatis-plus免xml的特性,我们不用自己手写任何的xml逻辑实现,甚至通篇未出现任何大家常见的mybatis相关配置!
以上。
本节内容我们介绍了数据持久化的相关操作,并且是基础传统的关系型数据库——mysql
下一节我们将共同探讨如何使用SpringBoot整合redis及mongodb,敬请期待!
2流高手速成记(之三):SpringBoot整合mybatis/mybatis-plus实现数据持久化的更多相关文章
- 2流高手速成记(之六):从SpringBoot到SpringCloudAlibaba
咱们接上回 2流高手速成记(之五):Springboot整合Shiro实现安全管理 - 14号程序员 - 博客园 (cnblogs.com) 身边常有朋友说:小项目用PHP.大项目用Java(这里绝无 ...
- 2流高手速成记(之七):基于Dubbo&Nacos的微服务简要实现
本节内容会用到之前给大家讲过的这两篇: 2流高手速成记(之六):从SpringBoot到SpringCloudAlibaba 2流高手速成记(之三):SpringBoot整合mybatis/mybat ...
- 2流高手速成记(之四):SpringBoot整合redis及mongodb
最近很忙,好不容易才抽出了时间,咱们接上回 上次我们主要讲了如何通过SpringBoot快速集成mybatis/mybatis-plus,以实现业务交互中的数据持久化,而这一切都是基于关系型数据库(S ...
- 2流高手速成记(之五):Springboot整合Shiro实现安全管理
废话不多说,咱们直接接上回 上一篇我们讲了如何使用Springboot框架整合Nosql,并于文章最后部分引入了服务端Session的概念 而早在上上一篇中,我们则已经讲到了如何使用Springboo ...
- 2流高手速成记(之八):基于Sentinel实现微服务体系下的限流与熔断
我们接上回 上一篇中,我们进行了简要的微服务实现,也体会到了SpringCloudAlibaba的强大和神奇之处 我们仅改动了两个注释,其他全篇代码不变,原来的独立服务就被我们分为了provider和 ...
- 【SpringBoot】11.Springboot整合SpringMVC+Mybatis(上)
Springboot整合SpringMVC+Mybatis 需求分析:通过使用Springboot+SpringMVC+Mybatis 整合实现一个对数据库表users表的CRUD操作. 1.创建项目 ...
- SpringBoot整合Freemarker+Mybatis
开发工具 , 开始 新建工程 .选择Spring Initializr 下一步 下一步,选择需要的组件 ..改一下工程名,Finish ..目录结构 首先,修改pom文件 然后,将applicatio ...
- 【SpringBoot】11-1.Springboot整合Springmvc+Mybatis增删改查操作(下)
整合过程:https://www.isdxh.com/68.html 一.增--增加用户 1.创建实体类 package com.dxh.pojo; public class Users { priv ...
- springboot 整合 mongodb实现 批量更新数据
现需求:需要批量将1000个数据先查询在更新到mongodb(如果查询不到数据,则添加数据) 1:工具类BathUpdateOptions import org.springframework.dat ...
随机推荐
- 掌握CSS中的z-index
前言 z-index是一个用于控制文档中图层顺序的属性.具有较高z-index值的元素将会出现在具有较低值的元素之上.就像页面上的x轴和y轴决定一个元素在水平和垂直方向上的位置一样,z-index控制 ...
- 意向不到的Dubug妙招
1.直接dubug到想要到达的位置,直接点击旁边的数字即可. 2.debug后不想重新启动,想重新进入再执行一次debug,可以使用drop frame来删除当前栈,跳到之前的栈再一次进入这个栈. 注 ...
- Stream流中的常用方法foeEach和Stream流中的常用方法filter
延迟方法:返回值类型仍然是Stream接口自身类型的方法,因此支持链式调用.(除了中介方法外,其余方法均为延迟方法) 终结方法:返回值类型不再是Stream接口自身类型的方法,因此不再支持类似Stri ...
- linux docker .net core 从建立网站到预览
docker的安装在网上一搜一大把,windows安装的就是exe双击,linux安装需要执行语句 ps:需要准备xftp.xshell.vs 2019.linux服务器.docker账号密码 例如: ...
- python 操作xml、html文件
简介 在一些项目中可能会使用到解析html文件,尤其是爬虫相关的,需要解析获取到的html内容,通常我们会使用lxml模块去进行html文件的解析. html文件 当前存在一个简单的html < ...
- HTML 本地缓存
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8" /> 5 & ...
- 浅淡 Apache Kylin 与 ClickHouse 的对比
作者简介 周耀,Kyligence 解决方案架构师,Apache Kylin.Apache Superset Contributor. Apache Kylin 和 ClickHouse 都是目前市场 ...
- CF1204E Natasha, Sasha and the Prefix Sums (卡塔兰数推理)
题面 题解 把题意变换一下,从(0,0)走到(n,m),每次只能网右或往上走,所以假设最大前缀和为f(n),那么走的时候就要到达但不超过 y = x-f(n) 这条线, 我们可以枚举答案,然后乘上方案 ...
- windows如何禁止更新
注意!本方法针对windows专业版本 家庭版本可以直接下载一个windows update blocker软件 windows+r快捷键输入代码如下图 gpedit.msc 进入后需要的路径如下 第 ...
- Oracle 服务器迁移的一些经验
前言 通过此文章来分享一下 Oracle 服务器迁移过程中的一些经验,希望对大家有些许帮助. 本文旨在帮助更多的同学,会提及一些基本命令或技巧,但不赘述,后续有机会再进一步分享各个细节. 背景 之前因 ...