【MyBatis-Plus】简介 | 入门案例

 博客主页:准Java全栈开发工程师

 00年出生,即将进入职场闯荡,目标赚钱,可能会有人觉得我格局小、觉得俗,但不得不承认这个世界已经不再是以一条线来分割的平面,而是围绕财富旋转的球面,成为有钱人不是为了去掌控球体的转向,而是当有人恶意掌控时,努力保护好家人和自己。

MyBatis-Plus

一、MyBatis-Plus 简介

  • Mybatis-Plus(简称 MP )是基于 MyBatis 框架基础上开发的增强型工具,旨在简化开发、提供效率。 简化开发、提供效率 是它的优势也是目的。
  • 对于 Mybatis-Plus 相关内容的学习,我们可以在 官网 中查看,这个官网我认为还是非常友好的,因为它是中文形式的,就相当于给我们提供了一个学习笔记,方便使用。

二、入门案例

1、创建数据库表

2、创建项目

  • 我创建项目使用的是阿里云的网址方式(因为我在联网情况下也无法使用 spring 官方的网址创建),在选择依赖时选中 MySQL 连接驱动和 MyBatis Plus 依赖。之后按照创建 SpringBoot 项目的方式创建即可,如果不了解如何创建的,可以移步到 【SpringBoot】快速入门 文章查看。

  • 创建完项目后,我们可以看看都自动为我们导入了哪些依赖,重点看一下 mybatis-plus 的启动依赖。发现里面不光导了和 mybatis-plus 的相关依赖,还导入了 mybatis 以及 mybatis 和 spring 整合包,这也是体现了 MyBatis-Plus 是基于 MyBatis 框架基础上开发的增强型工具。

3、创建 User 实体类

  • 在编写实体类代码之前,为了简化开发,在此介绍一个工具 —— lombok。Lombok 是一个Java类库,提供了一组注解,用于简化 POJO 实体类开发,使用该工具,我们就无需再书写 setter 和 getter 方法,toString 方法,构造器方法 …
  • 使用之前我们需要先导入 lombok 的依赖包。
<pre class="prettyprint hljs xml" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
</dependency></pre>
  • 使用 lombok 简化开发的方式定义 User 实体类。
<pre class="prettyprint hljs less" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">@Setter                 // 添加 setter 方法
@Getter                 // 添加 getter 方法
@ToString               // 添加 toString 方法
@EqualsAndHashCode      // 添加 equals 和 hashCode 方法
@NoArgsConstructor      // 添加无参构造器
@AllArgsConstructor     // 添加全参构造器
public class User { 
    private Integer id;
    private String username;
    private Integer age;
    private String password;
}</pre>
  • 通过使用一些注解,来代替之前的书写方式,这样也使得实体类看着更整洁一些,但是每次实体类前面都得写这么多的注解,总会觉得有些麻烦,有没有一种更简单的方式呢?答案当时是肯定的,技术的每一次进步都是为了让我们能够更轻松的开发(也正是因为开发过程中想懒省事,在不断的推动技术的进步)。
  • 使用一个 @Data 注解替代上述全部注解,在之后的开发过程中,定义实体类时就可以使用该方式简化开发,非常的方便。
<pre class="prettyprint hljs vbscript" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">@Data
public class User { 
    private Integer id;
    private String username;
    private Integer age;
    private String password;
}</pre>

4、配置数据库信息

<pre class="prettyprint hljs less" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/ssm
    username: root
    password: 123456</pre>
  • 以上是我的数据库信息,你们需要换成自己的。

5、编写数据访问层 UserMapper

  • 只需要定义一个 UserMapper 接口使其继承 BaseMapper,并使用泛型将 User 传入。
  • 之后其他所有的方法都无需定义,因为 BaseMapper 中已经定义了许多常用的 CRUD 方法。
<pre class="prettyprint hljs java" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">@Mapper
public interface  UserMapper  extends  BaseMapper<User>  { 
}</pre>
  • 在此只是为了测试能否成功调用到相应的方法,所以不再定义业务逻辑层,表示层等等。

  • 明明 UserMapper 中并没有定义这些方法,为了可以调用呢?这是因为 BaseMapper 中定义了这些方法,我们可以直接拿过来用。

  • 简单测试以下根据 ID 查询方法。

<pre class="prettyprint hljs java" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">@SpringBootTest
class  MybatisplusApplicationTests  { 
    @Autowired
    private UserMapper userMapper;

    @Test
    void  testSelectById()  { 
        User user = userMapper.selectById(1);
        System.out.println(user);
    }
}</pre>
  • 运行之后惊奇的发现,竟然查出结果了。

  • 通过这个入门案例,也告诉了我们 MyBatis-Plus 的强大之处,又替我们做了许多事情,这也是为什么慢慢的人们更喜欢使用 MyBatis-Plus 的原因。

 以上就是文章的全部内容啦,诸佬如果有任何建议都可以提哦。

 创作不易,如果觉得对您有帮助的话,欢迎关注✨点赞收藏哦。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值