springboot整合mybatis以及mybatis-plus 开发
一、springboot整合mybatis
1.注解版
1.1 导入坐标
<dependencies><!--mybatis坐标--><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.2.2</version></dependency><!--mysql--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.29</version></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></dependencies>
1.2 编写配置文件application.yml
#数据源
spring:datasource:username: rootpassword: 123456url: jdbc:mysql://localhost:3307/testdb?serverTimezone=GMTdriver-class-name: com.mysql.cj.jdbc.Driver
1.3 准备实体类Account ,省略了有参无参 setter getter toString方法
public class Account {private int aid;private String aname;private int amoney;
}
1.4 编写mapper映射器接口
@Mapper//注册注入一个mapper
@MapperScan(basePackages ="com.ztt.mybatis_springboot.mapper")//注册注入多个mapper(以包为单位) )
注意:两者只能写一个
//@Mapper//注册注入一个mapper
public interface AccountMapper {@Select("select * from account")public List<Account> findAll();
}
1.5 编写测试类
@SpringBootTest
class MybatisSpringbootApplicationTests {@Autowired(required = false)AccountMapper accountMapper;@Testvoid contextLoads() {List<Account> all=accountMapper.findAll();for (int i = 0; i <all.size() ; i++) {Account account=all.get(i);System.out.println(account);}}}
1.6 输出结果

2.xml版
2.1 在接口中定义一个方法
public List<Account> find();
2.2 编写xml文件,在resources目录下新建一个mapper包,在mapper包里面新建一个AccountMapper.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.ztt.mybatis_springboot.mapper.AccountMapper"><select id="find" resultType="com.ztt.mybatis_springboot.pojo.Account">select * from account;</select>
</mapper>
2.3 在主配置文件中添加mybatis的配置
mybatis:mapper-locations: mappers/*.xml
2.4 编写测试类
@Testvoid contextLoads1() {List<Account> all=accountMapper.find();for (int i = 0; i <all.size() ; i++) {Account account=all.get(i);System.out.println(account);}}
2.5 输出结果

二、MyBatis-Plus
1.MyBatis-Plus介绍
MyBatis最佳搭档,只做增强不做改变,为简化开发、提高效率而生。
详细信息请看官方文档MyBatis-Plus 🚀 为简化开发而生


2.MyBatis-Plus使用步骤:
2.1.坐标
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.1</version>
</dependency>
注意:mp坐标添加后,mybatis坐标移除
2.2 编写注解配置实体类与关系表映射关系(truncate清空表以及主键)
@TableName(value = "关联表名称")=========================》修饰在类
@TableField(value = "关联字段名称")======================》修饰在属性
exist = "忽略字段"
@TableId(type="指定主键生成策略,默认雪花算法")=============》修饰在属性
AUTO(0),
NONE(1),
INPUT(2),
ASSIGN_ID(3),
ASSIGN_UUID(4);
@TableName("account")
public class Account {@TableId(value = "aid",type = IdType.AUTO)private int aid;@TableField("aname")private String aname;@TableField("amoney")private int amoney;
}
2.3 使用
BaseMapper===========》公共的数据访问层
IService/ServiceImp==》公共的业务层
//@Mapper//注册注入一个mapper
public interface AccountMapper extends BaseMapper<Account> {@Select("select * from account")public List<Account> findAll();public List<Account> find();
}
2.4 配置yml文件
mybatis-plus:configuration:log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
2.5 测试代码使用
2.5.1 新增insert()
@SpringBootTest
public class Test01 {@Autowired(required = false)AccountMapper mapper;//新增@Testpublic void show1(){Account account = new Account("小兔兔", 2000);int row = mapper.insert(account);System.out.println("主键回填id:"+account.getAid());System.out.println("影响行数:"+row);}
}
输出结果:

查看数据库表,发现小兔兔添加成功

2.5.2 修改IDupdateById()
//修改ID@Testpublic void test02()throws Exception{Account account = new Account(3,"小小兔",3000);int row = mapper.updateById(account);System.out.println("影响行数:"+row);}
输出结果:

查看数据库表表中的小兔兔被改为小小兔,工资改为3000

2.5.3 查询ID selectById()
//查询ID@Testpublic void test04()throws Exception{Account account = mapper.selectById(3);System.out.println(account);}
输出结果:

2.5.4 查询IDS selectBatchIds(Arrays.asList(5,1,3))
//查询IDS@Testpublic void test05()throws Exception{List<Account> list = mapper.selectBatchIds(Arrays.asList(5,1,3));for (int i = 0; i < list.size(); i++) {Account account = list.get(i);System.out.println(account);}}
输出结果:

2.5.5 查询count selectCount(null)
//查询count@Testpublic void test06()throws Exception{int count = mapper.selectCount(null);System.out.println(count);}
输出结果:

2.5.6 查询list selectList(queryWrapper)
//查询list@Testpublic void test07()throws Exception{QueryWrapper<Account> queryWrapper = new QueryWrapper();
// queryWrapper.eq("ahobby","吃饭饭");
// queryWrapper.eq("aage","18");queryWrapper.eq("ahobby","吃饭饭").or().eq("aage","18");List<Account> list = mapper.selectList(queryWrapper);for (int i = 0; i < list.size(); i++) {Account account = list.get(i);System.out.println(account);}}
输出结果:

2.5.7 分页
mp分页使用:
注意:
1.page.setCurrent(2);当前页码从1开始
2.分页需要配置插件
@Testpublic void test08()throws Exception{//1.定义分页规则Page<Account> page = new Page<Account>();page.setSize(3);//每页记录数page.setCurrent(2);//当前页码//2.查询条件(可选)QueryWrapper queryWrapper = new QueryWrapper();IPage<Account> iPage = mapper.selectPage(page,null);List<Account> list = iPage.getRecords();//分页结果System.out.println("总记录数:"+iPage.getTotal());System.out.println("总记页数:"+iPage.getPages());for (int i = 0; i < list.size(); i++) {Account account = list.get(i);System.out.println(account);}}
输出结果:

2.5.8 按照ID删除
//查询delete@Testpublic void test09()throws Exception{mapper.deleteById(4);}
输出结果:

LambdaQueryWrapper
1.按照条件查询
@SpringBootTest
public class Test02 {@Autowired(required = false)AccountMapper mapper;@Testpublic void show1(){//1.查询条件LambdaQueryWrapper<Account> lambdaQueryWrapper=new LambdaQueryWrapper<Account>();lambdaQueryWrapper.gt(Account::getAage,18);//2.查询List<Account> list=mapper.selectList(lambdaQueryWrapper);for (int i = 0; i <list.size() ; i++) {Account account=list.get(i);System.out.println(account);}}
输出结果:

2.模拟动态查询1
//模拟动态查询1@Testpublic void show2(){//1.前端发送来的数据Integer num1 = null;Integer num2 = 30;//1.查询条件LambdaQueryWrapper<Account> lambdaQueryWrapper = new LambdaQueryWrapper<Account>();//2.判断if(null != num2){lambdaQueryWrapper.lt(Account::getAage,num2);}if(null != num1){lambdaQueryWrapper.gt(Account::getAage,num1);}//3.查询List<Account> list = mapper.selectList(lambdaQueryWrapper);for (int i = 0; i < list.size(); i++) {Account account = list.get(i);System.out.println(account);}}
输出结果:

3.模拟动态查询2
//模拟动态查询2@Testpublic void show3(){//1.前端发送来的数据Integer num1 = null;Integer num2 = 30;//1.查询条件LambdaQueryWrapper<Account> lambdaQueryWrapper = new LambdaQueryWrapper<Account>();//2.判断lambdaQueryWrapper.lt(null != num2,Account::getAage,num2);lambdaQueryWrapper.gt(null != num1,Account::getAage,num1);//3.查询List<Account> list = mapper.selectList(null);for (int i = 0; i < list.size(); i++) {Account account = list.get(i);System.out.println(account);}}
输出结果:

4.投影查询-字段查询
//投影查询-字段查询@Testpublic void show4() {//1.条件LambdaQueryWrapper<Account> lambdaQueryWrapper = new LambdaQueryWrapper<Account>();lambdaQueryWrapper.select(Account::getAname,Account::getAhobby);//2.查询List<Account> list = mapper.selectList(lambdaQueryWrapper);//4.遍历for (int i = 0; i < list.size(); i++) {Account account = list.get(i);System.out.println(account);}}
输出结果:

物理删除:业务数据从数据库中丢弃,执行的是delete操作
逻辑删除:为数据设置是否可用状态字段,删除时设置状态字段为不可用状态,数据保留在数据库中,执行的是update操作
实现步骤:
步骤1:修改数据库表添加deleted列,比如0代表正常,1代表删除,可以在添加列的同时设置其默认值为0正常。
步骤2:实体类添加属性以及注解
@TableLogic(value="0",delval="1")
private Integer deleted;
value为正常数据的值,delval为删除数据的值
5.逻辑删除:
@TableLogic(value = "0",delval = "1")
@TableField("isdelete")
private int isdelete;
@Test
public void show5() {mapper.deleteById(7);
}
输出结果:

查看数据库表中的数据,我们会发现id为7的小熊并没有没删除只是将表中的isdelete改为了1,再次查询表中的数据会发现小熊会被自动屏蔽掉,不会在控制台输出。
相关文章:
springboot整合mybatis以及mybatis-plus 开发
一、springboot整合mybatis 1.注解版 1.1 导入坐标 <dependencies><!--mybatis坐标--><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.2.2</…...
大语言模型微调框架Unsloth:简化模型微调流程,提升模型性能
Unsloth 将 Llama-3、Mistral、Phi-3 和 Gemma 等大型语言模型的微调速度提高了 2 倍,内存使用量减少了 70%,而且准确性不会降低! 特点 通过手动派生所有计算繁重的数学步骤和手写 GPU 内核,unsloth 可以在不更改任何硬件的情况…...
IPD-华为研发之道分析与理解
关于西方众多优秀的管理模式、理论我们学习过,也借鉴以及实践过,拿到《IPD-华为研发之道》通读研习后,正如书中所述,IPD是一套产品开发的模式、理念与方法,IPD思想,流程和方法是诸多成功企业优秀实践的经验…...
时空序列顶会文章
ICLR 2024 时间序列(Time Series)高分论文 - 知乎 (zhihu.com) ICML2024全部论文 icml.cc/virtual/2024/papers.html...
C语言-使用指针数组作为函数参数,实现对10个字符串进行排序
使用指针数组作为函数参数,实现对10个字符串进行排序 1.输入 lisi hahaha hehehe helloa leihoua lisi nihaoa wangwu ajax bureau2.输出 ajax bureau hahaha hehehe helloa leihoua lisi lisi nihaoa wangwu3.程序: #define _CRT_SECURE_NO_WARNING…...
???ABC366:F - Maximum Composition(dp,无序:贪心排序)
问题陈述 给你 NN 个线性函数 f1,f2,…,fNf1,f2,…,fN ,其中 fi(x)AixBifi(x)AixBi . 求由 KK 组成的序列 p(p1,p2,…,pK)p(p1,p2,…,pK) 中 fp1(fp2(…fpK(1)…))fp1(fp2(…fpK(1)…)) 的最大可能值。介于 11 和 NN (含)之间的个不…...
unity项目打包为webgl后应用于vue项目中(iframe模式)的数据交互
参考文章: 1.Unity打包WebGL: 导入Vue 2.unity文档-WebGL:与浏览器脚本交互 3.unity与vue交互(无第三方插件) 目录 一、前期工作1.新建.jslib文件2.新建.cs脚本3. 新建一个Text对象和button按钮对象4.添加脚本空对象UIEvent5.导出unity为w…...
【数据结构与算法 | 图篇】Bellman-Ford算法(单源最短路径算法)
1. 前言 前文的迪杰斯特拉算法不能求解有负边的图的最短路径的问题。而此文的Bellman-Ford可以处理含负权边的图算法,并且能检测出图中是否存在负环(权重和为负数的环). 2. 基本思想 1. 初始化: 对于所有顶点 v ∈ V \ {s}&am…...
Python | Leetcode Python题解之第336题回文对
题目: 题解: class Solution:def palindromePairs1(self, words: List[str]) -> List[List[int]]:# 核心思想--枚举前缀和后缀# 如果两个字符串k1,k2组成一个回文字符串会出现三种情况# len(k1) len(k2),则需要比较k1 k2[::-1]# len(k1…...
C语言家教记录(六)
导语 本次授课的内容如下:指针,指针和数组 辅助教材为 《C语言程序设计现代方法(第2版)》 指针 指针变量 计算机按字节划分地址,每个地址访问一个字节 指针变量指向变量的地址,指的是变量第一个字节的…...
C++竞赛初阶L1-11-第五单元-for循环(25~26课)519: T454430 人口增长问题
题目内容 假设目前的世界人口有 x 亿,按照每年 0.1% 的增长速度,n 年后将有多少人? 输入格式 一行两个正整数 x 和 n,之间有一个空格。其中,1≤x≤100,1≤n≤100。 输出格式 一行一个数,表示答案。以亿…...
demo测试
目录 接口commonCodeGenerator entityuser mapperUserMapper controllerUserController serviceUserServiceimplUserServiceImpl mapper.xmlpom.xmlapplication.yml 接口 common CodeGenerator package com.llz.demo.common;import com.baomidou.mybatisplus.core.exceptions…...
TinTinLand Web3 + DePIN 共学月|深入探索 DePIN 项目,全景分析去中心化网络未来
「TinTinLand Web3 主题共学月」是由 TinTinLand 每月发起的主题学习活动,携手知名项目共同打造一个系统化、互动性强的学习平台,帮助开发者不断提升技能,紧跟 Web3 技术的前沿发展。活动通过演示视频、学习打卡、模拟环境、实际操作等多种方…...
Java并发编程(六)
1、java 中有几种方法可以实现一个线程 继承 Thread 类实现 Runnable 接口实现 Callable 接口,需要实现的是 call() 方法 2、如何停止一个正在运行的线程 使用共享变量的方式 在这种方式中,之所以引入共享变量,是因为该变量可以被多个执行…...
k8s对外服务之Ingress
目录 1.Ingress 简介 2.Ingress 组成 3.Ingress-Nginx 工作原理 4.部署 nginx-ingress-controller 5.总结 1.Ingress 简介 service的作用体现在两个方面,对集群内部,它不断跟踪pod的变化,更新endpoint中对应pod的对象,提供了…...
使用Python+moviepy在视频画面上绘制边框
一、 使用VideoFileClip对象的的fx函数设置vfx.margin,在视频画面上绘制边框 from moviepy.editor import * mvVideoFileClip(/home/Download/leaves.mp4) mv2mv.fx(vfx.margin,mar3,color(0,0,255),opacity0.5) # 绘制边框# mar3 :边框宽度3像素&#…...
灵办AI探索之旅:颠覆传统的代码开发工具
前言 灵办AI是一个先进的人工智能工具,专注于提高软件开发和项目管理的效率。其核心功能包括代码生成、优化、评估和自动化修复,旨在帮助开发者和团队提升开发速度和代码质量。 体验地址:https://ilingban.com/browser_extension/?fromjj …...
【Redis】Redis 数据类型与结构—(二)
Redis 数据类型与结构 一、值的数据类型二、键值对数据结构三、集合数据操作效率 一、值的数据类型 Redis “快”取决于两方面,一方面,它是内存数据库,另一方面,则是高效的数据结构。 Redis 键值对中值的数据类型,也…...
Tomcat初篇
目录 Tomcat主要特点Tomcat的核心组件Tomcat使用安装Tomcat配置Tomcat启动和停止Tomcat Tomcat工作原理目录结构配置文件性能优化策略 Tomcat Apache Tomcat是一个开源的Servlet容器和Web服务器,广泛用于运行基于Java的Web应用程序。它实现了Java Servlet和JavaSer…...
机器学习(2)-- KNN算法之手写数字识别
KNN算法 KNN(K-Nearest Neighbor,K最近邻)算法是一种用于分类和回归的非参数统计方法,尤其在分类问题中表现出色。在手写数字识别领域,KNN算法通过比较测试样本与训练样本之间的距离,找到最近的K个邻居&am…...
SpringBoot-17-MyBatis动态SQL标签之常用标签
文章目录 1 代码1.1 实体User.java1.2 接口UserMapper.java1.3 映射UserMapper.xml1.3.1 标签if1.3.2 标签if和where1.3.3 标签choose和when和otherwise1.4 UserController.java2 常用动态SQL标签2.1 标签set2.1.1 UserMapper.java2.1.2 UserMapper.xml2.1.3 UserController.ja…...
论文解读:交大港大上海AI Lab开源论文 | 宇树机器人多姿态起立控制强化学习框架(二)
HoST框架核心实现方法详解 - 论文深度解读(第二部分) 《Learning Humanoid Standing-up Control across Diverse Postures》 系列文章: 论文深度解读 + 算法与代码分析(二) 作者机构: 上海AI Lab, 上海交通大学, 香港大学, 浙江大学, 香港中文大学 论文主题: 人形机器人…...
python/java环境配置
环境变量放一起 python: 1.首先下载Python Python下载地址:Download Python | Python.org downloads ---windows -- 64 2.安装Python 下面两个,然后自定义,全选 可以把前4个选上 3.环境配置 1)搜高级系统设置 2…...
CMake基础:构建流程详解
目录 1.CMake构建过程的基本流程 2.CMake构建的具体步骤 2.1.创建构建目录 2.2.使用 CMake 生成构建文件 2.3.编译和构建 2.4.清理构建文件 2.5.重新配置和构建 3.跨平台构建示例 4.工具链与交叉编译 5.CMake构建后的项目结构解析 5.1.CMake构建后的目录结构 5.2.构…...
大语言模型如何处理长文本?常用文本分割技术详解
为什么需要文本分割? 引言:为什么需要文本分割?一、基础文本分割方法1. 按段落分割(Paragraph Splitting)2. 按句子分割(Sentence Splitting)二、高级文本分割策略3. 重叠分割(Sliding Window)4. 递归分割(Recursive Splitting)三、生产级工具推荐5. 使用LangChain的…...
论文解读:交大港大上海AI Lab开源论文 | 宇树机器人多姿态起立控制强化学习框架(一)
宇树机器人多姿态起立控制强化学习框架论文解析 论文解读:交大&港大&上海AI Lab开源论文 | 宇树机器人多姿态起立控制强化学习框架(一) 论文解读:交大&港大&上海AI Lab开源论文 | 宇树机器人多姿态起立控制强化…...
相机Camera日志分析之三十一:高通Camx HAL十种流程基础分析关键字汇总(后续持续更新中)
【关注我,后续持续新增专题博文,谢谢!!!】 上一篇我们讲了:有对最普通的场景进行各个日志注释讲解,但相机场景太多,日志差异也巨大。后面将展示各种场景下的日志。 通过notepad++打开场景下的日志,通过下列分类关键字搜索,即可清晰的分析不同场景的相机运行流程差异…...
学习STC51单片机32(芯片为STC89C52RCRC)OLED显示屏2
每日一言 今天的每一份坚持,都是在为未来积攒底气。 案例:OLED显示一个A 这边观察到一个点,怎么雪花了就是都是乱七八糟的占满了屏幕。。 解释 : 如果代码里信号切换太快(比如 SDA 刚变,SCL 立刻变&#…...
宇树科技,改名了!
提到国内具身智能和机器人领域的代表企业,那宇树科技(Unitree)必须名列其榜。 最近,宇树科技的一项新变动消息在业界引发了不少关注和讨论,即: 宇树向其合作伙伴发布了一封公司名称变更函称,因…...
libfmt: 现代C++的格式化工具库介绍与酷炫功能
libfmt: 现代C的格式化工具库介绍与酷炫功能 libfmt 是一个开源的C格式化库,提供了高效、安全的文本格式化功能,是C20中引入的std::format的基础实现。它比传统的printf和iostream更安全、更灵活、性能更好。 基本介绍 主要特点 类型安全:…...
