开发环境

IDEA

JDK:1.8

Spring Boot:2.6.2

Maven:3.3.9

MySQL:8.0.23

数据库准备

CREATE DATABASE mybatis_plus_db;DROP TABLE IF EXISTS person;
CREATE TABLE person(id BIGINT(20) NOT NULL COMMENT '主键ID',
name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
age INT(11) NULL DEFAULT NULL COMMENT '年龄',
email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱',
PRIMARY KEY (id));INSERT INTO person (id, name, age, email) VALUES
(1, 'yixin', 18, 'test1@qq.com'),
(2, 'Jack', 20, 'test2@qq.com'),
(3, 'Tom', 28, 'test3@qq.com'),
(4, 'Sandy', 21, 'test4@qq.com'),
(5, 'Billie', 24, 'test5@qq.com');

一、项目搭建

1.1 创建一个Spring Boot项目

初始化以下依赖

1.2 导入依赖

        <!-- 数据库驱动 --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><!-- lombok --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><!-- mybatis-plus --><!-- mybatis-plus 是自己开发,并非官方的! --><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.0.5</version></dependency>

警告:引入 *MyBatis-Plus* 之后请不要再次引入 *MyBatis* 以及 *MyBatis-Spring*,以避免因版本差异导致的问题。

1.3 编写配置文件

application.properties:

# mysql 5 驱动不同 com.mysql.jdbc.Driver
# mysql 8 驱动不同com.mysql.cj.jdbc.Driver、需要增加时区的配置 serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis_plus_db?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

1.4 建立目录

1.5 编写实体类

实体类Person:

package com.yixin.mybatis_plus.pojo;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;@Data
@AllArgsConstructor
@NoArgsConstructor
public class Person {private Long id;private String name;private Integer age;private String email;
}

1.6 编写接口

PersonMapper接口:

package com.yixin.mybatis_plus.mapper;import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.yixin.mybatis_plus.pojo.Person;
import org.springframework.stereotype.Repository;// 在对应的Mapper上面继承基本的类 BaseMapper
@Repository// 代表持久层
public interface PersonMapper extends BaseMapper<Person> {// 所有的CRUD操作都已经编写完成了// 我们不需要像以前的配置一大堆文件了!
}

1.7 主启动类添加注解扫描

注意:在主启动类上去扫描我们的mapper包下的所有接口

package com.yixin.mybatis_plus;import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@MapperScan("com.yixin.mybatis_plus.mapper")
@SpringBootApplication
public class MybatisPlusApplication {public static void main(String[] args) {SpringApplication.run(MybatisPlusApplication.class, args);}}

1.8 测试

package com.yixin.mybatis_plus;import com.yixin.mybatis_plus.mapper.PersonMapper;
import com.yixin.mybatis_plus.pojo.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;@SpringBootTest
class MybatisPlusApplicationTests {@Autowiredprivate PersonMapper personMapper;@Testvoid contextLoads() {List<Person> personList = personMapper.selectList(null);personList.forEach(System.out::println);}}

结果:

这样就搭建成功了!

配置日志

通过以上的输出,我们并不知道其sql是怎么执行的,我们为了进一步探究其执行过程,我们在配置文件中加上日志配置

application.properties:

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

我们再次输出:

通过这样的日志输出,我们就知道MyBatis-Plus到底帮我们执行了什么样操作。

二、增删查改操作

2.1 查询操作

2.1.1 selectById

需求:查询id为1的用户信息。

代码实现:

    @Testvoid test() {Person person = personMapper.selectById(1);System.out.println(person);}

2.1.2 selectList

需求:查询全部的用户信息。

代码实现:

    @Testvoid contextLoads() {List<Person> personList = personMapper.selectList(null);personList.forEach(System.out::println);}

2.1.3 selectBatchIds

需求:查询id为1和3的用户信息。

代码实现:

    @Testvoid test2() {List<Person> personList = personMapper.selectBatchIds(Arrays.asList(1,3));personList.forEach(System.out::println);}

2.1.4 selectByMap

需求:查询name为yixin,并且年龄为18岁的用户信息。

代码实现:

    @Testvoid test3() {HashMap<String ,Object> map=new HashMap<>();map.put("name","yixin");map.put("age",18);List<Person> personList = personMapper.selectByMap(map);personList.forEach(System.out::println);}

我们来看一下这条语句,它是如何生成的:

通过这样日志的查看,是不是就感觉马上就理解了!

2.2 增加操作

2.2.1 insert

需求:插入用户的信息如下

name:张三

age:21

email:test6@qq.com

代码实现:

    @Testvoid test4() {// 我们没有自定义id,它会帮我们自动生成idPerson person =new Person();person.setName("张三");person.setAge(21);person.setEmail("test6@qq.com");int result=personMapper.insert(person);System.out.println(result);// 受影响的行数System.out.println(person);//可以发现,id会自动回填}

结果:

数据库插入的id的默认值为:全局的唯一id

2.2.2 自增Id

如果我们不想他每次都给我们随机生成id,而是希望通过在原有id基础上进行自增,那么我们可以这么操作。

第一步:设置数据库主键id为自增。

第二步:在实体类的id属性增加注解@TableId(type = IdType.AUTO)

package com.yixin.mybatis_plus.pojo;import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;@Data
@AllArgsConstructor
@NoArgsConstructor
public class Person {@TableId(type = IdType.AUTO)private Long id;private String name;private Integer age;private String email;
}

然后我们再进行多次插入,看看是否会进行自增操作。

可以发现,能够成功的进行自增操作了!

对于每一个id,大家不用担心会重复,因为其采用的是【雪花算法】生成的,可以保证id几乎全球唯一!

雪花算法

snowflake是Twitter开源的分布式ID生成算法,结果是一个long型的ID。

其核心思想是:

使用41bit作为毫秒数,

10bit作为机器的ID(5个bit是数据中心,5个bit的机器ID),

12bit作为毫秒内的流水号(意味着每个节点在每毫秒可以产生 4096 个 ID),

最后还有一个符号位,永远是0。

2.3 删除操作

2.3.1 deleteById

需求:删除id为5的用户信息。

代码实现:

    @Testvoid test6() {int result=personMapper.deleteById(5L);System.out.println(result);// 受影响的行数}

2.3.2 deleteByMap

需求:删除姓名为 Sandy,并且年龄为21的用户信息。

代码实现:

    @Testvoid test7() {HashMap<String, Object> map=new HashMap<>();map.put("name","Sandy");map.put("age",21);int result=personMapper.deleteByMap(map);System.out.println(result);// 受影响的行数}

2.4 更新操作

需求:将id为2的用户的姓名更改为"一心同学"

代码实现:

    @Testvoid test5() {Person person =new Person();person.setId(2L);person.setName("一心同学");person.setAge(20);person.setEmail("test2@qq.com");int result=personMapper.updateById(person);System.out.println(result);// 受影响的行数}

小结

以上就是对【MyBatis-Plus】基础入门【增删改查】的讲解,看到以上对CRUD的操作是不是感觉特别清爽,而【MyBatis-Plus】的功能不仅仅如此,下一篇博客【一心同学】将会对其【注解】进行讲解。

MyBatis-Plus——增删查改相关推荐

  1. (4) hibernate增删查改+批量操作+类似Mybatis动态sql

    简介 采用spring + hibernate + freemaker+ maven搭建起来的一个hibernate增删查改和 类似mybatis动态sql查询的一个案例 增删查改demo + 动态s ...

  2. MyBatis实现数据的增删查改

    MyBatis的配置请参考我的上一篇文章,在上一篇文章的基础上我们实现数据的增删查改. 创建实现增删查改的xml文件,这里才是真正实现增删查改的文件. 创建完后要在配置文件中注册创建好的xml文件: ...

  3. mybatis 介绍 入门 mapper配置文件 增删查改 别名配置 #和 $的区别

    mybatis 介绍 今天,一起来说说mybits这个框架吧.这是一个持久层的框架.之前叫做ibatis. 所以,在它的代码中出现ibatis这个词的时候,不要感到惊讶.不是写错了,它确实就是这个样子 ...

  4. Mybatis、SpringBoot入门实战(微型项目) -- Mysql增删查改、写接口、测试接口

    Mybatis入门实战(微型项目) – Mysql增删查改.写接口.测试接口 开发环境: 1.Window10 v1909 2.idea 2019 3.jdk 1.8 4.mybatis 3.5.5 ...

  5. spring和mybatis结合做简单的增删查改系统_springbootamp;amp;vue简单的景点信息管理系统...

    springboot&&vue简单的景点信息管理系统 这两天闲着没有什么事,就根据陈哥的教程,试着写了一个springboot和vue的简单的景点信息管理系统.也就大致实现了最基本的增 ...

  6. SpringBoot整合Mybatis-plus实现增删查改

    今天给大家分享一下SpringBoot整合Mybatis-plus的增删查改案例. pom.xml <?xml version="1.0" encoding="UT ...

  7. 8天学通MongoDB——第二天 细说增删查改

    2019独角兽企业重金招聘Python工程师标准>>> 看过上一篇,相信大家都会知道如何开启mongodb了,这篇就细说下其中的增删查改,首先当我们用上一篇同样的方式打开mongod ...

  8. MERGE批量增删查改数据

    MERGE优点: 在批量处理数据的时候,我可以用到merge一次完成数据处理. 示例代码一: MERGE INTO student AS t using (SELECT '丽水' AS NAME,20 ...

  9. Django:数据库表的建立与增删查改(ForeignKey和ManytoMany)

    数据库表的创建: 1.Django工程项目建立,基础环境调试. 2.创建表 from django.db import models class Publisher(models.Model):    ...

最新文章

  1. excel的if函数中android,excel中if函数嵌套式使用教程
  2. git如何查看sshkey_Jenkins配置SSH Key下载代码
  3. 记录遇到的Altium designer显示布线未完成坑
  4. mysql 数据库编程_MySQL数据库编程(C++语言)
  5. git 回退远端master分支版本
  6. 表达式的计算结果必须为节点集 调试
  7. linux访问nfs端口号,linux nfs配置及访问控制
  8. API系统1.2lite模板管理测试版发布
  9. 如何用 javascript 做一个高逼格的进度条
  10. CSM中一些常见问题的解决方式
  11. 机器学习集成模型学习——Bagging集成学习(三)
  12. 【转载】Debian 6安装小记
  13. odbc驱动程序配置失败_如何使用ODBC驱动程序配置链接服务器
  14. mysql硬盘复制无法启动_磁盘的移动导致MySQL数据启动失败
  15. php 医疗报销系统,费用报销系统
  16. 网线制作ppt_ppt模板网线
  17. sipP测试,UAS怎么主动发BYE消息
  18. python爬虫之cookie
  19. 一区希尔盖服务器找不到,魔兽世界怀旧服:服务器第一成就达成!分享一下心得...
  20. 武林外传挂机宝宝 v1.0 怎么用

热门文章

  1. python将16进制字符串转换为整数_Python 16进制与字符串的转换
  2. splat net_Ruby中的Splat参数
  3. java treemap_Java TreeMap keySet()方法与示例
  4. micropython 蜂鸣器_基于MicroPython的TPYBoard微信远程可燃气体报警器的设计与实现...
  5. vb.net变量值变化触发事件_Angular变化检测的理解
  6. 绘图的尺寸_Auto CAD机械绘图尺寸标注教程1(尺寸标注简介)
  7. 在CSS中使用not:first-child选择器
  8. 不吹牛逼,撸个注解有什么难的
  9. 这样写Java,同事直呼666
  10. pip/pip3更换国内源