当前位置: 首页 > web >正文

mybatis-plus从入门到入土(一):快速开始

​ 朋友们, 大家好, 从今天开始我想开一个系列博客。名字起的比较随意就叫Mybatis-Plus从入门到入土, 这系列博客的定位是从基础使用开始, 然后逐步深入全面的了解Mybatis-Plus框架, 写这个博客的主要原因是工作中经常用到Mybatis-Plus框架, 因而对这个框架相对比较了解一些, 顺便在写作的过程中对自身知识查漏补缺, 欢迎小伙伴和我一同步入Mybatis-Plus之旅, 我们马上起飞。

快速开始

我们从Mybatis-Plus官网的快速开始一步步的学习这个框架。

引入Mybatis-Plus相关依赖

对于SpringBoot项目来说, Mybatis-Plus的依赖相对简单, 只有一个starter。这里我基于Spring Boot2mybatis-plus 3.5.12, 今后的学习也基于该版本。

<dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.5.12</version>
</dependency>

配置

  • application.yml 配置文件中添加 H2 数据库的相关配置:
# DataSource Config
spring:datasource:driver-class-name: org.h2.Driverusername: rootpassword: testsql:init:schema-locations: classpath:db/schema-h2.sqldata-locations: classpath:db/data-h2.sql

这里基于H2数据库, 如果小伙伴手头有其他数据库也可以使用, sql-init中指定的是数据库的表结构示例数据。关于mybatis-plus具体支持哪些数据库可以参考https://baomidou.com/introduce/。

  • 在 Spring Boot 启动类中添加 @MapperScan 注解,扫描 Mapper 文件夹:
@SpringBootApplication
@MapperScan("com.example.mybatispluslearning.mapper")
public class MybatisPlusLearningApplication {public static void main(String[] args) {SpringApplication.run(MybatisPlusLearningApplication.class, args);}}

编码

创建一个User类

@Data
@TableName("sys_user")
public class User {private Long id;private String name;private Integer age;private String email;
}

创建一个UserMapper接口, 这个类继承了mybatis-plus提供的BaseMapper接口

public interface UserMapper extends BaseMapper<User> {}

测试

@SpringBootTest
public class SampleTest {@Autowiredprivate UserMapper userMapper;@Testpublic void testSelect() {System.out.println(("----- selectAll method test ------"));List<User> userList = userMapper.selectList(null);Assert.isTrue(5 == userList.size(), "");userList.forEach(System.out::println);}}

在这里插入图片描述

好了, 小伙伴们, 第一讲就讲完了, 敬请收看第二讲。

http://www.xdnf.cn/news/14826.html

相关文章:

  • 【学习】《算法图解》第八章学习笔记:平衡树
  • Linux下基于C++11的socket网络编程(基础)个人总结版
  • 智能新纪元:大语言模型如何重塑电商“人货场”经典范式
  • 电子计数跳绳原型
  • X-Search:Spring AI实现的AI智能搜索
  • JS中判断数据类型的方法
  • 左神算法之矩阵旋转90度
  • linux运维学习第10周
  • 设计模式:观察者模式 (Observer) 案例详解
  • 消息队列:Redis Stream到RabbitMQ的转换
  • MongoDB06 - MongoDB 地理空间
  • PyQt5—QPushButton 功能 API 学习笔记
  • Zynq7020 Linux更新启动分区文件导致文件大小为0的处理方式
  • 力扣第84题-柱状图中最大的矩形
  • Webpack中的Loader详解
  • 用户行为序列建模(篇六)-【阿里】DSIN
  • 实战篇----利用 LangChain 和 BERT 用于命名实体识别-----完整代码
  • flask使用-链接mongoDB
  • Python爬虫-爬取汽车之家全部汽车品牌及车型数据
  • ListExtension 扩展方法增加 转DataTable()方法
  • Lua现学现卖
  • DOP数据开放平台(真实线上项目)
  • 电商返利APP架构设计:如何基于Spring Cloud构建高并发佣金结算系统
  • OpenLayers 下载地图切片
  • 解决cursor无法下载插件等网络问题
  • vue-29(创建 Nuxt.js 项目)
  • 从用户到权限:解密 AWS IAM Identity Center 的授权之道
  • 给定一个没有重复元素的数组,写出生成这个数组的MaxTree的函数
  • TDengine 如何使用 MQTT 采集数据?
  • lambda、function基础/响应式编程基础