MybatisPlus的使用(一)--基本配置与无条件查询

创建测试用的数据库

CREATE DATABASE `mybatis_plus` /*!40100 DEFAULT CHARACTER SET utf8mb4 */; use `mybatis_plus`;
CREATE TABLE `user` (
`id` bigint(20) NOT NULL COMMENT '主键ID',
`name` varchar(30) DEFAULT NULL COMMENT '姓名 ',
`age` int(11) DEFAULT NULL COMMENT '年龄 ',
`email` varchar(50) DEFAULT NULL COMMENT '邮箱 ',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

向数据库插入数据

INSERT INTO user (id, name, age, email) VALUES
(1, 'Jone', 18, 'test1@baomidou.com'),
(2, 'Jack', 20, 'test2@baomidou.com'),
(3, 'Tom', 28, 'test3@baomidou.com'),
(4, 'Sandy', 21, 'test4@baomidou.com'),
(5, 'Billie', 24, 'test5@baomidou.com');

pom文件中引入依赖

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>

idea中安装lombok插件

配置application.yml

具体的数据库名,用户名,密码更换为自己的

spring:
# 配置数据源信息
datasource:
# 配置数据源类型
type: com.zaxxer.hikari.HikariDataSource
# 配置连接数据库信息
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/mybatis_plus?characterEncoding=utf- 8&useSSL=false
username: root
password: 123456

设置在启动类设置mapper扫描的包

@MapperScan("zoo.mapper")
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class SpringDemo2Application {
    public static void main(String[] args) {
        SpringApplication.run(SpringDemo2Application.class, args);
    }
}

添加实体类

  • 这里实体类的主键也就是id必须为Long类型的。
@Data //lombok注解
public class User {

private Long id;
private String name;
private Integer age;
private String email;

}
  • @Data注解的作用是自动生成getter、setter、equals、hashCode和toString等方法,无需我们再手动添加。

下面是加上@Data注解,经过编译后的User.class文件,可见它已经生成了getter、setter、equals、hashCode和toString等方法

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package zoo.pojo;

import zoo.enmu.SexEnum;

public class User {
    private Long id;
    private String name;
    private Integer age;
    private String email;
    private SexEnum sex;

    public User() {
    }

    public Long getId() {
        return this.id;
    }

    public String getName() {
        return this.name;
    }

    public Integer getAge() {
        return this.age;
    }

    public String getEmail() {
        return this.email;
    }

    public SexEnum getSex() {
        return this.sex;
    }

    public void setId(final Long id) {
        this.id = id;
    }

    public void setName(final String name) {
        this.name = name;
    }

    public void setAge(final Integer age) {
        this.age = age;
    }

    public void setEmail(final String email) {
        this.email = email;
    }

    public void setSex(final SexEnum sex) {
        this.sex = sex;
    }

    public boolean equals(final Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof User)) {
            return false;
        } else {
            User other = (User)o;
            if (!other.canEqual(this)) {
                return false;
            } else {
                label71: {
                    Object this$id = this.getId();
                    Object other$id = other.getId();
                    if (this$id == null) {
                        if (other$id == null) {
                            break label71;
                        }
                    } else if (this$id.equals(other$id)) {
                        break label71;
                    }

                    return false;
                }

                Object this$age = this.getAge();
                Object other$age = other.getAge();
                if (this$age == null) {
                    if (other$age != null) {
                        return false;
                    }
                } else if (!this$age.equals(other$age)) {
                    return false;
                }

                label57: {
                    Object this$name = this.getName();
                    Object other$name = other.getName();
                    if (this$name == null) {
                        if (other$name == null) {
                            break label57;
                        }
                    } else if (this$name.equals(other$name)) {
                        break label57;
                    }

                    return false;
                }

                Object this$email = this.getEmail();
                Object other$email = other.getEmail();
                if (this$email == null) {
                    if (other$email != null) {
                        return false;
                    }
                } else if (!this$email.equals(other$email)) {
                    return false;
                }

                Object this$sex = this.getSex();
                Object other$sex = other.getSex();
                if (this$sex == null) {
                    if (other$sex == null) {
                        return true;
                    }
                } else if (this$sex.equals(other$sex)) {
                    return true;
                }

                return false;
            }
        }
    }

    protected boolean canEqual(final Object other) {
        return other instanceof User;
    }

    public int hashCode() {
        int PRIME = true;
        int result = 1;
        Object $id = this.getId();
        int result = result * 59 + ($id == null ? 43 : $id.hashCode());
        Object $age = this.getAge();
        result = result * 59 + ($age == null ? 43 : $age.hashCode());
        Object $name = this.getName();
        result = result * 59 + ($name == null ? 43 : $name.hashCode());
        Object $email = this.getEmail();
        result = result * 59 + ($email == null ? 43 : $email.hashCode());
        Object $sex = this.getSex();
        result = result * 59 + ($sex == null ? 43 : $sex.hashCode());
        return result;
    }

    public String toString() {
        return "User(id=" + this.getId() + ", name=" + this.getName() + ", age=" + this.getAge() + ", email=" + this.getEmail() + ", sex=" + this.getSex() + ")";
    }
}

添加mapper

让UserMapper继承BaseMapper,这样无需我们手动添加就有了单表增删改查的很多函数。

public interface UserMapper extends BaseMapper<User> {
}

查询测试

@SpringBootTest

public class MybatisPlusTest {

@Autowired

private UserMapper userMapper;

@Test

public void testSelectList(){

//selectList()根据MP内置的条件构造器查询一个list集合,null表示没有条件,即查询所有

userMapper.selectList(null).forEach(System.out::println);

}

}

测试结果

application.yml中配置日志输出

# 配置MyBatis日志
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/428102.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

JavaWeb - 1 - 概述

一.什么是Web&#xff1f; Web&#xff1a;全球广域网&#xff0c;也称为万维网&#xff08;www World Wide Web&#xff09;&#xff0c;能够通过浏览器访问的网站 二.Web网站的工作流程 三.Web网站的开发模式 3.1 前后端分离开发&#xff08;主流&#xff09; 3.2 混合开发…

thymeleaf 一个莫名其妙的错误提示 org.attoparser.ParseException

thymeleaf 一个莫名其妙的错误提示 介绍 开发过程中遇到一个莫名奇妙的错误&#xff0c;一时竟然不知道怎么解决&#xff0c;找官网也没有找到 问题 页面显示 错误日志 org.attoparser.ParseException: (Line 96, Column 5) Malformed markup: Attribute “}” appears m…

羊大师揭秘羊奶将成为,健康新选择

羊大师揭秘羊奶将成为&#xff0c;健康新选择 羊奶作为一种传统的营养食品&#xff0c;已经在全球范围内受到了广泛的关注和认可。随着人们对健康生活的追求和对食品安全的重视&#xff0c;羊奶正逐渐成为健康的新选择。 羊奶的营养价值得到了科学的验证。羊奶中含有丰富的蛋…

【深度学习笔记】5_5 LeNet

注&#xff1a;本文为《动手学深度学习》开源内容&#xff0c;部分标注了个人理解&#xff0c;仅为个人学习记录&#xff0c;无抄袭搬运意图 5.5 卷积神经网络&#xff08;LeNet&#xff09; 在3.9节&#xff08;多层感知机的从零开始实现&#xff09;里我们构造了一个含单隐藏…

飞书文档批量导出

背景需求 最近所参与的项目即将结项&#xff0c;需要将飞书中的产品需求文档&#xff08;PRD&#xff09;交付给甲方&#xff0c;由于文档较多&#xff0c;大概有两百多个&#xff0c;一个一个的下载导出&#xff0c;太麻烦了&#xff08;PS&#xff1a;本人比较懒&#xff09;…

ruoyi-nbcio-plus的Vue3前端升级组件后出现的问题(一)

更多ruoyi-nbcio功能请看演示系统 gitee源代码地址 前后端代码&#xff1a; https://gitee.com/nbacheng/ruoyi-nbcio 演示地址&#xff1a; http://122.227.135.243:9666 更多nbcio-boot功能请看演示系统 gitee源代码地址 后端代码&#xff1a; https://gitee.com/nbach…

Jenkins 安装

目录 1、部署 Jenkins 安装配置 Jenkins 解锁 Jenkins 安装 Jenkins 插件 创建管理员账号 手动安装插件 2、Jenkins 从 GitLat 拉取代码 安装 Jenkins 插件 在 node-16 上生成密钥对 把公钥配置到 gitlab 上 把 root 用户私钥配置到 jenkins 上 Jenkins 创建一个任务…

PCSA时钟控制集成之时钟门控集成

1.4 时钟门控集成 高级时钟门控是使用每个时钟域的时钟控制器组件实现的。时钟控制器支持多个组件的时钟门控&#xff0c;并为每个组件提供一个Q-Channel接口。 大多数Arm组件都支持这种类型的时钟门控。大多数组件使用Q-Channel。一些较早的组件使用AXI LPI&#xff0c;但在…

2024全国护网行动HW行动招聘/收人!!!

2024全国护网行动HW行动招聘 溯蓉信创开始收人啦&#xff01;&#xff01;&#xff01;现在开始收录2024HW简历&#xff0c;感兴趣的小伙伴扫码二维码添加微信 我们签约后&#xff0c;入场即预付款3k&#xff0c;签约后我们会在HW之前对我们的人员进行HW培训&#xff0c;保证上…

AI加速引擎PAI-TorchAcc:整体介绍与性能概述

作者&#xff1a;沈雯婷、黄奕桐、艾宝乐、王昂、李永 1、简介 PAI-TorchAcc(Torch Accelerator)是阿里云人工智能平台开发的Pytorch上的大模型训练加速框架。 PAI-TorchAcc提供了一套基于Pytorch的简洁、易用的接口&#xff0c;无需进行模型转换就可以无缝地接入HuggingFac…

HTTP笔记(五)

个人学习笔记&#xff08;整理不易&#xff0c;有帮助点个赞&#xff09; 笔记目录&#xff1a;学习笔记目录_pytest和unittest、airtest_weixin_42717928的博客-CSDN博客 目录 一&#xff1a;HTTP报文首部 &#xff08;1&#xff09;HTTP请求报文 &#xff08;2&#xff09…

【C++庖丁解牛】默认成员函数

&#x1f4d9; 作者简介 &#xff1a;RO-BERRY &#x1f4d7; 学习方向&#xff1a;致力于C、C、数据结构、TCP/IP、数据库等等一系列知识 &#x1f4d2; 日后方向 : 偏向于CPP开发以及大数据方向&#xff0c;欢迎各位关注&#xff0c;谢谢各位的支持 目录 前言1. 构造函数1.1 …

Linux配置网卡功能

提示:工具下载链接在文章最后 目录 一.network功能介绍二.配置network功能2.1 network_ip配置检查 2.2 network_br配置2.2.1 配置的网桥原先不存在检查2.2.2 配置的网桥已存在-修改网桥IP检查2.2.3 配置的网桥已存在-只添加网卡到网桥里检查 2.3 network_bond配置检查 2.4 netw…

数据结构与算法-选择排序

引言 在计算机科学中&#xff0c;数据结构和算法是两个至关重要的基石。它们共同决定了程序的效率、可读性和可维护性。本文我们将聚焦于一种基础而直观的排序算法——选择排序&#xff0c;并探讨其内在的工作机制以及在实际应用中的优缺点。 一、什么是选择排序&#xff1f; …

LeetCode 刷题 [C++] 第763题.划分字母区间

题目描述 给你一个字符串 s 。我们要把这个字符串划分为尽可能多的片段&#xff0c;同一字母最多出现在一个片段中。 注意&#xff0c;划分结果需要满足&#xff1a;将所有划分结果按顺序连接&#xff0c;得到的字符串仍然是 s 。 返回一个表示每个字符串片段的长度的列表。 …

如何在Vue中实现事件处理?

Vue是一种流行的JavaScript框架&#xff0c;广泛应用于前端开发。在Vue中&#xff0c;事件处理是一个非常关键的概念&#xff0c;可以帮助我们实现用户与页面的交互&#xff0c;今天我们就来探讨一下如何在Vue中实现事件处理。 首先&#xff0c;让我们先了解一下在Vue中如何绑…

微信小程序开发:接入阿里云人像动漫化api接口

前面我已经把腾讯云的人像转动漫化接口接到了我的小程序里&#xff0c;但是和阿里云的对比后&#xff0c;发现阿里云的效果会更好一些&#xff0c;且支持更多特效&#xff0c;如下&#xff1a; 我比较喜欢这个3D特效风格&#xff0c;动画3D也可以&#xff0c;大家拭目以待。 话…

波奇学Liunx:信号的产生,保存,处理

信号的产生&#xff0c;信号的保存&#xff0c;信号的处理 在操作系统中进程接受到信号会保存&#xff0c;产生 进程必须识别和能够处理信号&#xff0c;处理信号是进程的内置功能 进程收到信号时不一定会立即执行&#xff0c;所以进程必然有一套识别&#xff0c;保存&#xff…

Nodejs 第四十四章(redis基本使用)

字符串的操作 SET key value [NX|XX] [EX seconds] [PX milliseconds] [GET]key&#xff1a;要设置的键名。value&#xff1a;要设置的值。NX&#xff1a;可选参数&#xff0c;表示只在键不存在时才设置值。XX&#xff1a;可选参数&#xff0c;表示只在键已经存在时才设置值。…

MySQL字符集和比较规则

MySQL字符集和比较规则 字符集和比较规则简介 字符集&#xff1a; 描述字符与二进制数据的映射关系 比较规则&#xff1a;比较指定字符集中的字符的规则 字符集 我们知道&#xff0c;计算机无法直接存储字符串&#xff0c;实际存储的都是二进制数据。字符集是有限的&#xff…
最新文章