文章目录

一、

1. pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.gblfy</groupId>
<artifactId>springboot-mybatisplus</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-mybatisplus</name>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!--Mybatis-Plus 启动器-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.1.tmp</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jmock/jmock-junit4 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>

</project>

2. application.yml

#数据库库配置信息
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/ssm?useSSL=false&serverTimezone=GMT%2B8
username: root
password: root

#实体扫描,多个package用逗号或者分号分隔
mybatis-plus:
typeAliasesPackage: com.gblfy.springboot.mybatisplus.entity
mapper-locations:
- classpath*:com/gblfy/springboot/**/mapping/*.xml

# 日志级别 日志内容 换行
logging:
level:
root: warn
com.gblfy.springboot.mybatisplus.mapper: trace
pattern:
console: '%p%m%n'

3. dao层接口集成baseMapper

package com.gblfy.springboot.mybatisplus.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gblfy.springboot.mybatisplus.entity.User;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface UserMapper extends BaseMapper<User> {}

4. 启动类上+扫描

package com.gblfy.springboot.mybatisplus;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.gblfy.springboot.mybatisplus.mapper")
public class SpringbootMybatisplusApplication {

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

5. 创建数据库

数据库名:ssm
编码:utf-8

6. 初始化表结构

/*
Navicat Premium Data Transfer

Source Server : 127.0.0.1
Source Server Type : MySQL
Source Server Version : 50721
Source Host : 127.0.0.1:3306
Source Schema : ssm

Target Server Type : MySQL
Target Server Version : 50721
File Encoding : 65001

Date: 14/03/2020 20:23:26
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` bigint(20) NOT NULL COMMENT '主键ID',
`name` varchar(30) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '姓名',
`age` int(11) NULL DEFAULT NULL COMMENT '年龄',
`email` varchar(50) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '邮箱',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_bin ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES (1, 'Jone', 18, 'test1@baomidou.com');
INSERT INTO `user` VALUES (2, 'Jack', 20, 'test2@baomidou.com');
INSERT INTO `user` VALUES (3, 'Tom', 28, 'test3@baomidou.com');
INSERT INTO `user` VALUES (4, '郭轩', 26, 'test4@baomidou.com');
INSERT INTO `user` VALUES (5, '郭振', 26, 'test5@baomidou.com');
INSERT INTO `user` VALUES (1238735228017610754, '郭雨昕', 2, 'gblfy@qq.com');
INSERT INTO `user` VALUES (1238735794525503489, '郭雨泽', 22, 'gblfy@qq.com');

SET FOREIGN_KEY_CHECKS = 1;

7. 浏览器验证

​http://localhost:8080/user/queryList​

SpringBoot 集成 mybatisPlus_mysql


SpringBoot 集成 mybatisPlus_mysql_02

8. 源码地址

​https://github.com/gb-heima/springboot-mybatisplus​