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…...
挑战杯推荐项目
“人工智能”创意赛 - 智能艺术创作助手:借助大模型技术,开发能根据用户输入的主题、风格等要求,生成绘画、音乐、文学作品等多种形式艺术创作灵感或初稿的应用,帮助艺术家和创意爱好者激发创意、提高创作效率。 - 个性化梦境…...
Xshell远程连接Kali(默认 | 私钥)Note版
前言:xshell远程连接,私钥连接和常规默认连接 任务一 开启ssh服务 service ssh status //查看ssh服务状态 service ssh start //开启ssh服务 update-rc.d ssh enable //开启自启动ssh服务 任务二 修改配置文件 vi /etc/ssh/ssh_config //第一…...
2025年能源电力系统与流体力学国际会议 (EPSFD 2025)
2025年能源电力系统与流体力学国际会议(EPSFD 2025)将于本年度在美丽的杭州盛大召开。作为全球能源、电力系统以及流体力学领域的顶级盛会,EPSFD 2025旨在为来自世界各地的科学家、工程师和研究人员提供一个展示最新研究成果、分享实践经验及…...
Redis相关知识总结(缓存雪崩,缓存穿透,缓存击穿,Redis实现分布式锁,如何保持数据库和缓存一致)
文章目录 1.什么是Redis?2.为什么要使用redis作为mysql的缓存?3.什么是缓存雪崩、缓存穿透、缓存击穿?3.1缓存雪崩3.1.1 大量缓存同时过期3.1.2 Redis宕机 3.2 缓存击穿3.3 缓存穿透3.4 总结 4. 数据库和缓存如何保持一致性5. Redis实现分布式…...
智能在线客服平台:数字化时代企业连接用户的 AI 中枢
随着互联网技术的飞速发展,消费者期望能够随时随地与企业进行交流。在线客服平台作为连接企业与客户的重要桥梁,不仅优化了客户体验,还提升了企业的服务效率和市场竞争力。本文将探讨在线客服平台的重要性、技术进展、实际应用,并…...
镜像里切换为普通用户
如果你登录远程虚拟机默认就是 root 用户,但你不希望用 root 权限运行 ns-3(这是对的,ns3 工具会拒绝 root),你可以按以下方法创建一个 非 root 用户账号 并切换到它运行 ns-3。 一次性解决方案:创建非 roo…...
SpringBoot+uniapp 的 Champion 俱乐部微信小程序设计与实现,论文初版实现
摘要 本论文旨在设计并实现基于 SpringBoot 和 uniapp 的 Champion 俱乐部微信小程序,以满足俱乐部线上活动推广、会员管理、社交互动等需求。通过 SpringBoot 搭建后端服务,提供稳定高效的数据处理与业务逻辑支持;利用 uniapp 实现跨平台前…...
Spring Boot面试题精选汇总
🤟致敬读者 🟩感谢阅读🟦笑口常开🟪生日快乐⬛早点睡觉 📘博主相关 🟧博主信息🟨博客首页🟫专栏推荐🟥活动信息 文章目录 Spring Boot面试题精选汇总⚙️ **一、核心概…...
《基于Apache Flink的流处理》笔记
思维导图 1-3 章 4-7章 8-11 章 参考资料 源码: https://github.com/streaming-with-flink 博客 https://flink.apache.org/bloghttps://www.ververica.com/blog 聚会及会议 https://flink-forward.orghttps://www.meetup.com/topics/apache-flink https://n…...
NFT模式:数字资产确权与链游经济系统构建
NFT模式:数字资产确权与链游经济系统构建 ——从技术架构到可持续生态的范式革命 一、确权技术革新:构建可信数字资产基石 1. 区块链底层架构的进化 跨链互操作协议:基于LayerZero协议实现以太坊、Solana等公链资产互通,通过零知…...
