您好,欢迎来到纷纭教育。
搜索
您的当前位置:首页MybatisPlus快速使用

MybatisPlus快速使用

来源:纷纭教育

MybatisPlus快速使用

1.MybatisPlus简介

MyBatis Plus是一个基于MyBatis的增强工具,旨在简化和增强与数据库交互的开发过程。它提供了一系列的功能和特性,使得使用MyBatis更加便捷和高效。

MyBatis Plus的一些主要特点和功能:

2.快速搭建

2.1初始化工程

创建一个空的 Spring Boot 工程

这里我们直接跳过

2.2添加依赖

引入 Spring Boot Starter 父工程:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.5+ 版本</version>
    <relativePath/>
</parent>

引入 spring-boot-starterspring-boot-starter-testmybatis-plus-boot-startermysql lombok依赖:

<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.4.2</version>
        </dependency>
        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>
    <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
</dependencies>

2.3配置

在application.yml配置文件中添加mysql数据库的相关配置

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/testdb?serverTimezone=GMT%2B8&useSSL=false
    username: root
    password: 123456

在 Spring Boot 启动类中添加 @MapperScan 注解,扫描 Mapper 文件夹:

@SpringBootApplication
//根据自己的项目路径修改
@MapperScan("com.cqgcxy.mapper")
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

2.4编码

编写实体类 User

@Data
@TableName("`user`")//指定数据库的user表
public class User {
    
    @TableId(value = "uid", type = IdType.AUTO)
    private Integer uid;

    @TableField("userName")
    private String username;

    @TableField("userPassword")
    private  String userpassword;

    private Integer sex;

    private String hobbit;

    private String degree;

    private String note;

    private Integer rule;

    private Integer state;

}

编写 Mapper 包下的 UserMapper接口

@Mapper
public interface UserMapper extends BaseMapper<User> {
    
}

手写编码太麻烦可以选择代码生成器,MybatisPlus有详细介绍,或者去

3.开始使用

MybatisPlus提供了很多模板供我们使用,我们在这里对部分模板进行学习, 选择测试类,进行功能测试:

UserMapper 中的 selectList() 方法的参数为 MP 内置的条件封装器 Wrapper,所以不填写就是无任何条件

/**
     * 查询user表全部数据
     */
    @Test
    void selectUserAll() {
        List<User> userList = userMapper.selectList(null);
        userList.forEach(System.out::println);
    }

控制台输出

User(uid=1, username=aa, userpassword=123456, sex=0, hobbit=打游戏, degree=本科, note=张三, rule=0, state=1)
User(uid=2, username=bb, userpassword=123456, sex=1, hobbit=, degree=博士, note=李四, rule=0, state=1)
User(uid=3, username=cc, userpassword=123456, sex=0, hobbit=, degree=硕士, note=王五, rule=1, state=1)
User(uid=4, username=哈哈, userpassword=123456, sex=1, hobbit=rap, degree=本科, note=老六, rule=0, state=1)
User(uid=13, username=小小, userpassword=123456, sex=null, hobbit=null, degree=null, note=null, rule=null, state=null)
User(uid=14, username=大大, userpassword=123456, sex=null, hobbit=null, degree=null, note=null, rule=null, state=null)
User(uid=15, username=大明, userpassword=123456, sex=null, hobbit=null, degree=null, note=null, rule=null, state=null)
User(uid=16, username=小a, userpassword=987654, sex=null, hobbit=null, degree=null, note=null, rule=null, state=null)

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- fenyunshixun.cn 版权所有 湘ICP备2023022495号-9

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务