一、MyBatisPlus架构图
Mybatis-Plus(简称MP)是一个 Mybatis 的增强工具,在 Mybatis 的基础上只做增强不做改变,为简化开发、提高效率而生。这是官方给的定义,关于mybatis-plus的更多介绍及特性,可以参考mybatis-plus官网。那么它是怎么增强的呢?其实就是它已经封装好了一些crud方法,我们不需要再写xml了,直接调用这些方法就行,就类似于JPA。
二、SpringBoot第一个简单应用
1.数据库建表
#创建用户表
CREATE TABLE user (
id BIGINT(20) PRIMARY KEY NOT NULL COMMENT '主键',
name VARCHAR(30) DEFAULT NULL COMMENT '姓名',
age INT(11) DEFAULT NULL COMMENT '年龄',
email VARCHAR(50) DEFAULT NULL COMMENT '邮箱',
manager_id BIGINT(20) DEFAULT NULL COMMENT '直属上级id',
create_time DATETIME DEFAULT NULL COMMENT '创建时间',
CONSTRAINT manager_fk FOREIGN KEY (manager_id)
REFERENCES user (id)
) ENGINE=INNODB CHARSET=UTF8;
#初始化数据:
INSERT INTO user (id, name, age, email, manager_id
, create_time)
VALUES (1087982257332887553, 'Boss', 40, 'boss@baomidou.com', NULL
, '2019-01-11 14:20:20'),
(1088248166370832385, '王台风', 25, 'wtf@baomidou.com', 1087982257332887553
, '2019-02-05 11:12:22'),
(1088250446457389058, '杨宗伟', 28, 'lyw@baomidou.com', 1088248166370832385
, '2019-02-14 08:31:16'),
(1094590409767661570, '离雨', 33, 'zjq@baomidou.com', 1088248166370832385
, '2019-01-14 09:15:15'),
(1094592041087729666, '张雨风', 36, 'lhm@baomidou.com', 1088248166370832385
, '2019-01-14 09:48:16');
2.maven依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.2</version>
</dependency>
3.yml配置文件
server:
port: 8080
spring:
datasource:
url: jdbc:mysql://localhost/csdn_demo?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&serverTimezone=UTC
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
maxActive: 20
initialSize: 5
maxWait: 60000
minIdle: 5
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
connectionProperties:
druid.stat.mergeSql: true
druid.stat.slowSqlMillis: 5000
maxOpenPreparedStatements: 20
filters: stat,wall,log4j
logSlowSql: true
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
mapper-locations: classpath*:mapper/*.xml
type-aliases-package: com.example.springboot.model
4.目录结构
5.entity包创建User类:
package com.example.klg.entity;
import lombok.Data;
import java.time.LocalDateTime;
@Data
public class User {
private Long id;
private String name;
private Integer age;
private String email;
private Long managerId;
private LocalDateTime createTime;
}
6.mapper包创建UserMapper接口,并集成mybatisPlus的BaseMapper
package com.example.klg.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.klg.entity.User;
import org.springframework.stereotype.Repository;
@Repository
public interface UserMapper extends BaseMapper<User> {
}
7.在springboot启动类添加@MapperScan扫描mapper包下面的接口
package com.example.klg;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan("com.example.klg.mapper")
@SpringBootApplication
public class KlgApplication {
public static void main(String[] args) {
SpringApplication.run(KlgApplication.class, args);
}
}
8.在service包下面创建UserService类
package com.example.klg.service;
import com.example.klg.entity.User;
import java.util.List;
public interface UserService {
List<User> select();
}
9.在impl包下面创建UserService类
package com.example.klg.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.klg.entity.User;
import com.example.klg.mapper.UserMapper;
import com.example.klg.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
UserMapper userMapper