• MybatisPlus 1 MybatisPlus 入门案例与简介 1.1 入门案例


    MybatisPlus

    【黑马程序员2022新版SSM框架教程_Spring+SpringMVC+Maven高级+SpringBoot+MyBatisPlus企业实用开发技术】

    1 MybatisPlus 入门案例与简介

    1.1 入门案例

    MybatisPlus(简称MP)是基于MyBatis框架基础上开发的增强型工具,旨在简化开发、提供效率。

    【开发方式】

    • 基于MyBatis使用MyBatisPlus
    • 基于Spring使用MyBatisPlus
    • 基于SpringBoot使用MyBatisPlus

    【回顾SpringBoot 整合Mybatis 的开发过程】

    1. 创建SpringBoot 工程
    2. 勾选配置使用的技术,能够实现自动添加起步依赖包
    3. 设置dataSource相关属性(JDBC参数)
    4. 定义数据层接口映射配置

    【快速实现SpringBoot 整合 MybatisPlus】

    ① 创建数据库及表

    create database if not exists mybatisplus_db character set utf8;
    
    use mybatisplus_db;
    
    create table user (
    	id bigint(20) primary key auto_increment,
    	name varchar(32) not null,
    	password varchar(32) not null,
    	age int(3) not null ,
    	tel varchar(32) not null
    );
    
    insert into user values(1,'Tom','tom',3,'18866668888');
    insert into user values(2,'Jerry','jerry',4,'16688886666');
    insert into user values(3,'Jock','123456',41,'18812345678');
    insert into user values(4,'传智播客','itcast',15,'4006184000');
    
    select * from user;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    在这里插入图片描述

    ② 创建SpringBoot 工程

    在这里插入图片描述

    ③ 勾选配置使用技术

    笔者这里使用的阿里云起步,所以可以勾mybatisplus

    在这里插入图片描述

    ④ 补全pom.xml 依赖

    在这里插入图片描述

    这个快速入门案例,只是进行了测试,所以没用web

    说明:

    • druid数据源可以加也可以不加,SpringBoot有内置的数据源,可以配置成使用Druid数据源

    • 从MP的依赖关系可以看出,通过依赖传递已经将MyBatis与MyBatis整合Spring的jar包导入,所以不需要额外在添加MyBatis的相关jar包

      在这里插入图片描述

    ⑤ 添加MP 的相关配置信息

    resources默认生成的是properties配置文件,可以将其替换成yml文件,并在文件中配置数据库连接的相关信息

    application.yml

    spring:
      datasource:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:3306/mybatisplus_db?serverTimezone=UTC
        username: root
        password: 200039
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    ⑥ 根据数据库表创建实体类

    package com.dingjiaxiong.domain;
    
    /**
     * ClassName: User
     * date: 2022/9/23 20:40
     *
     * @author DingJiaxiong
     */
    
    public class User {
        private Long id;
        private String name;
        private String password;
        private Integer age;
        private String tel;
    
        public Long getId() {
            return id;
        }
    
        public void setId(Long id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    
        public Integer getAge() {
            return age;
        }
    
        public void setAge(Integer age) {
            this.age = age;
        }
    
        public String getTel() {
            return tel;
        }
    
        public void setTel(String tel) {
            this.tel = tel;
        }
    
        @Override
        public String toString() {
            return "User{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", password='" + password + '\'' +
                    ", age=" + age +
                    ", tel='" + tel + '\'' +
                    '}';
        }
    }
    
    • 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
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67

    ⑦ 创建Dao 接口

    package com.dingjiaxiong.dao;
    
    import com.baomidou.mybatisplus.core.mapper.BaseMapper;
    import com.dingjiaxiong.domain.User;
    import org.apache.ibatis.annotations.Mapper;
    
    /**
     * ClassName: UserDao
     * date: 2022/9/23 20:40
     *
     * @author DingJiaxiong
     */
    
    @Mapper
    public interface UserDao extends BaseMapper<User> {
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    ⑧ 编辑引导类

    在这里插入图片描述

    说明:

    Dao接口要想被容器扫描到,有两种解决方案:

    • 方案一:在Dao接口上添加@Mapper注解,并且确保Dao处在引导类所在包或其子包中
      • 该方案的缺点是需要在每一Dao接口中添加注解
    • 方案二:在引导类上添加@MapperScan注解,其属性为所要扫描的Dao所在包
      • 该方案的好处是只需要写一次,则指定包下的所有Dao接口都能被扫描到,@Mapper就可以不写。

    ⑨ 编写测试类

    package com.dingjiaxiong;
    
    import com.dingjiaxiong.dao.UserDao;
    import com.dingjiaxiong.domain.User;
    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    
    import java.util.List;
    
    @SpringBootTest
    class MpDemoApplicationTests {
        
        @Autowired
        private UserDao userDao;
        
        @Test
        public void testGetAll(){
            List<User> userList = userDao.selectList(null);
            System.out.println(userList);
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    运行结果

    在这里插入图片描述

    OK。

    userDao注入的时候下面有红线提示的原因是什么?

    • UserDao是一个接口,不能实例化对象
    • 只有在服务器启动IOC容器初始化后,由框架创建DAO接口的代理对象来注入
    • 现在服务器并未启动,所以代理对象也未创建,IDEA查找不到对应的对象注入,所以提示报红
    • 一旦服务启动,就能注入其代理对象,所以该错误提示不影响正常运行。

    跟之前整合MyBatis相比,会发现不需要在DAO接口中编写方法和SQL语句了,只需要继承BaseMapper接口即可。整体来说简化很多

  • 相关阅读:
    如何使用编程旋转PDF页面并保存
    如何实现防抖、节流?
    stack与queue的简单封装
    前端实现页面文本复制/代码复制/mardown复制的几种方式
    图书管理系统
    C++ Reference: Standard C++ Library reference: C Library: cwctype: iswblank
    (PC+WAP)织梦模板娱乐新闻资讯类网站
    C# json序列化实体时,时间戳转Datetime
    LeetCode/LintCode 题解丨一周爆刷字符串:文字并排
    2022杭电多校第四场题解
  • 原文地址:https://blog.csdn.net/weixin_44226181/article/details/127419561