• MyBatis-Plus + Spring Boot入门案例



    MyBatis-Plus + Spring Boot入门案例

    • pom.xml依赖
    • application.yml配置
    • 继承BaeMapper< T >
    • 使用 @Mapper 或 @MapperScan + Repository
    • 使用Lombok插件简化实体类 @Data

    1、创建数据库与表

    CREATE DATABASE `mybatis_plus` 
    
    USE `mybatis_plus`;
    
    DROP TABLE IF EXISTS `user`;
    
    CREATE TABLE `user` (
      `id` bigint NOT NULL COMMENT '主键ID',
      `name` varchar(30) DEFAULT NULL COMMENT '姓名',
      `age` int DEFAULT NULL COMMENT '年龄',
      `email` varchar(50) DEFAULT NULL COMMENT '邮箱',
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;
    
    
    insert  into `user`(`id`,`name`,`age`,`email`) values 
    (1,'Jone',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'),
    (6,'HeXi',20,'test6@qq.com');
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    2、创建SpringBoot工程

    在这里插入图片描述
    在这里插入图片描述

    3、选择版本与技术集SQL(MySQL Driver)

    在这里插入图片描述

    4、添加依赖pom.xml,记得配置与刷新Maven

            
            <dependency>
                <groupId>com.baomidougroupId>
                <artifactId>mybatis-plus-boot-starterartifactId>
                <version>3.5.1version>
            dependency>
    
            
            <dependency>
                <groupId>org.projectlombokgroupId>
                <artifactId>lombokartifactId>
                <optional>trueoptional>
            dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    5、配置application.yml

    spring:
      # 配置数据源信息
      datasource:
        # 配置数据源类型
        type: com.zaxxer.hikari.HikariDataSource
        # 配置连接数据库的各个信息
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://localhost:3306/mybatis_plus?serverTimezone=GMT%2B8&characterEncoding=utf-8&useSSL=false
        username: root
        password: 111111
    
    # 日志
    mybatis-plus:
      configuration:
        log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    6、创建pojo

    package com.sgz.pojo;
    
    import lombok.Data;
    
    @Data
    public class User {
    
        private Long id;
        private String name;
        private Integer age;
        private String email;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    7、创建Mapper接口继承BaseMapper< T >

    package com.sgz.mapper;
    
    import com.baomidou.mybatisplus.core.mapper.BaseMapper;
    import com.sgz.pojo.User;
    import org.apache.ibatis.annotations.Mapper;
    import org.springframework.stereotype.Repository;
    
    @Repository
    //@Mapper  // 这里使用@Mapper后,引导类的MapperScan就可以去掉了
    public interface UserMapper extends BaseMapper<User> {
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    8、引导类使用@MapperScan

    package com.sgz;
    
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    // 用于扫描mapper接口所在的包
    @MapperScan("com.sgz.mapper") // mapper中使用@Mapper,这里的注解就可以去掉了
    public class MybaitsplusSpringbootDemoApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(MybaitsplusSpringbootDemoApplication.class, args);
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    9、测试类

    package com.sgz;
    
    import com.sgz.mapper.UserMapper;
    import com.sgz.pojo.User;
    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    
    import java.util.Arrays;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    @SpringBootTest
    public class MyBatisPlusTest {
    
        @Autowired
        private UserMapper userMapper;
    
        // 查询全部
        @Test
        void testSelectList() {
            // SELECT id,name,age,email FROM user
            // 通过条件查询全部数据,调用BaseMapper,如果没有条件则设置为null
            userMapper.selectList(null);
        }
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
  • 相关阅读:
    C++核心编程:P15->STL----常用容器(上)
    变量、存储过程与函数
    Python学习之CSDN21天学习挑战赛计划之5
    Seata中的AT事务模式是如何实现行锁
    「运维有小邓」如何更有效的避免密码攻击
    1、Mybatis搭建流程
    【数字IC验证半科班历程:芯片概括_2023.10.20】
    [附源码]SSM计算机毕业设计基于的高校学生考勤管理系统JAVA
    SDEI初探-透过事务看本质
    【论文阅读】MARS:用于自动驾驶的实例感知、模块化和现实模拟器
  • 原文地址:https://blog.csdn.net/s17856147699/article/details/126324842