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

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.lqws.cn/news/575623.html

相关文章:

  • Windows安装虚拟机、ROS2
  • 实战四:基于PyTorch实现猫狗分类的web应用【2/3】
  • springboot校园新闻网站
  • 如果将Word里每页的行数设置成50行
  • STM32——HAL库总结
  • ECMAScript 2019(ES2019):数组与对象操作的精细化升级
  • PYTHON从入门到实践10-文件操作与异常
  • MySQL-复合查询
  • 推荐几本关于网络安全的书
  • 暴力风扇方案介绍
  • 【学习】《算法图解》第八章学习笔记:平衡树
  • docker的离线安装
  • [数论](a % MOD + b % MOD) % MOD = (a + b) % MOD
  • 【系统分析师】高分论文:论软件开发模型及应用
  • 引入的js文件里有过引用vue,在外侧仍需再写一次吗
  • 【C++】C++的虚析构函数
  • leetcode437-路径总和III
  • 【Flask开发】嘿马文学web完整flask项目第2篇:2.用户认证,Json Web Token(JWT)【附代码文档】
  • 桌面小屏幕实战课程:DesktopScreen 17 HTTPS
  • 熟悉 PyCharm
  • Tomcat服务概述
  • 用户行为序列建模(篇六)-【阿里】DSIN
  • Python爬虫-爬取汽车之家全部汽车品牌及车型数据
  • Linux下基于C++11的socket网络编程(基础)个人总结版
  • 应用层网络编程范式
  • 现代 JavaScript (ES6+) 入门到实战(五):告别回调地狱,Promise 完全入门
  • Origin绘制复合子母饼状图—复合柱饼图、复合环饼图及复合饼图
  • 爬虫实战之图片及人物信息爬取
  • 【IQA技术专题】大模型视觉强化学习IQA:Q-Insight
  • 数据同步工具对比:Canal、DataX与Flink CDC