Mybatis Plus学习教程(1):环境搭建

217 阅读1分钟

测试环境版本

  1. JDK 1.8
  2. MySQL5.7
  3. Spring Boot
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
  1. Mybatis Plus
<dependency>
   <groupId>com.baomidou</groupId>
   <artifactId>mybatis-plus-boot-starter</artifactId>
   <version>3.2.0</version>
</dependency>

5.配置数据源和mapper-locations

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/mybatis_plus_demo?useSSL=false&serverTimezone=GMT%2B8&characterEncoding=UTF-8
    username: root
    password: 123456
mybatis-plus:
  mapper-locations: classpath:/mapper/*.xml

6.设置项目目录机构

项目目录结构

7.启动类上添加注解

@MapperScan("com.marco.mybatisplusexample.mapper")

数据准备

准备部门表

create table sys_dept
(
    id          bigint auto_increment comment '部门主键'
        primary key,
    dept_name   varchar(32)   not null comment '部门名称',
    location    varchar(32)   not null comment '地点',
    dept_remark varchar(1024) null comment '部门备注',
    tenant_id   bigint        not null comment '租户id'
)
    charset = utf8;

INSERT INTO mybatis_plus_demo.sys_dept (id, dept_name, location, dept_remark, tenant_id) VALUES (1, '财务部', '北京', '管很多钱的财务部', 1);
INSERT INTO mybatis_plus_demo.sys_dept (id, dept_name, location, dept_remark, tenant_id) VALUES (2, '法务部', '北京', '懂很多法律的法务部', 1);
INSERT INTO mybatis_plus_demo.sys_dept (id, dept_name, location, dept_remark, tenant_id) VALUES (3, 'IT部', '北京', '有一群牛逼的程序猿', 1);
INSERT INTO mybatis_plus_demo.sys_dept (id, dept_name, location, dept_remark, tenant_id) VALUES (4, '人事部', '北京', '管很多人的人事部', 1);
INSERT INTO mybatis_plus_demo.sys_dept (id, dept_name, location, dept_remark, tenant_id) VALUES (5, '财务部', '上海', '管很多钱的财务部', 2);
INSERT INTO mybatis_plus_demo.sys_dept (id, dept_name, location, dept_remark, tenant_id) VALUES (6, '法务部', '上海', '懂很多法律的法务部', 2);
INSERT INTO mybatis_plus_demo.sys_dept (id, dept_name, location, dept_remark, tenant_id) VALUES (7, 'IT部', '上海', '有一群牛逼的程序猿', 2);
INSERT INTO mybatis_plus_demo.sys_dept (id, dept_name, location, dept_remark, tenant_id) VALUES (8, '人事部', '上海', '管很多人的人事部', 2);
INSERT INTO mybatis_plus_demo.sys_dept (id, dept_name, location, dept_remark, tenant_id) VALUES (9, '财务部', '深圳', '管很多钱的财务部', 3);
INSERT INTO mybatis_plus_demo.sys_dept (id, dept_name, location, dept_remark, tenant_id) VALUES (10, '法务部', '深圳', '懂很多法律的法务部', 3);
INSERT INTO mybatis_plus_demo.sys_dept (id, dept_name, location, dept_remark, tenant_id) VALUES (11, 'IT部', '深圳', '有一群牛逼的程序猿', 3);
INSERT INTO mybatis_plus_demo.sys_dept (id, dept_name, location, dept_remark, tenant_id) VALUES (12, '人事部', '深圳', '管很多人的人事部', 3);