1.创建数据库及表
1.1.创建mp库
1.2.创建user表,建表语句如下:
CREATE TABLE `user` (
`id` bigint(20) 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 '创建时间',
PRIMARY KEY (`id`),
KEY `manager_fk` (`manager_id`),
CONSTRAINT `manager_fk` FOREIGN KEY (`manager_id`) REFERENCES `user` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1.3.添加如下测试数据:
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');
insert into `user` (`id`, `name`, `age`, `email`, `manager_id`, `create_time`) values('1088248166370832385','王天风','25','wtf@baomidou.com','1087982257332887553','2019-02-05 11:12:22');
insert into `user` (`id`, `name`, `age`, `email`, `manager_id`, `create_time`) values('1088250446457389058','李艺伟','28','lyw@baomidou.com','1088248166370832385','2019-02-14 08:31:16');
insert into `user` (`id`, `name`, `age`, `email`, `manager_id`, `create_time`) values('1094590409767661570','张雨琪','31','zjq@baomidou.com','1088248166370832385','2019-01-14 09:15:15');
insert into `user` (`id`, `name`, `age`, `email`, `manager_id`, `create_time`) values('1094592041087729666','刘红雨','32','lhm@baomidou.com','1088248166370832385','2019-01-14 09:48:16');
2.创建maven项目
2.1.添加如下依赖
pom.xml文件内容:
<?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.1.8.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.mp</groupId>
<artifactId>mybatisplus</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mybatisplus</name>
<description>mybatisplus</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- springboot web启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- mysql jdbc驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- lombok简化java代码 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<!-- mybatis-plus启动器 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.2.0</version>
</dependency>
<!-- commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>
<!-- springboot test启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.2.在resources包下添加配置文件
application.yml配置文件内容:
server:
port: 8088
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/MP?characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8
username: root
password: 123456
2.3.创建启动类
package com.mp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @Description 启动类
* @Author LL
* @Date 2020-03-22 12:21
*/
@MapperScan("com.mp.dao")
@SpringBootApplication
public class MpApplication {
public static void main(String[] args) {
SpringApplication.run(MpApplication.class, args);
}
}
2.4.创建实体类
package com.mp.entity;
import lombok.Data;
import java.time.LocalDateTime;
/**
* @Description 实体类
* @Author LL
* @Date 2020-03-22 12:22
*/
@Data
public class User {
private Long id;//主键
private String name;//姓名
private Integer age;//年龄
private String email;//邮箱
private Long managerId;//上级id
private LocalDateTime createTime;//创建时间
}
2.5.创建dao接口
package com.mp.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.mp.entity.User;
/**
* @Description 数据访问层
* @Author LL
* @Date 2020-03-22 12:31
*/
public interface UserMapper extends BaseMapper<User> {
}
3.编写测试类测试结果
3.1.测试类
package com.mp;
import com.mp.dao.UserMapper;
import com.mp.entity.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
/**
* @Description 测试类
* @Author LL
* @Date 2020-03-22 12:35
*/
@SpringBootTest
@RunWith(SpringRunner.class)
public class SimpleTest {
@Autowired
private UserMapper userMapper;
@Test
public void select(){
List<User> users = userMapper.selectList(null);
users.forEach(System.out::println);
}
}