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','[email protected]',NULL,'2019-01-11 14:20:20');
insert into `user` (`id`, `name`, `age`, `email`, `manager_id`, `create_time`) values('1088248166370832385','王天风','25','[email protected]','1087982257332887553','2019-02-05 11:12:22');
insert into `user` (`id`, `name`, `age`, `email`, `manager_id`, `create_time`) values('1088250446457389058','李艺伟','28','[email protected]','1088248166370832385','2019-02-14 08:31:16');
insert into `user` (`id`, `name`, `age`, `email`, `manager_id`, `create_time`) values('1094590409767661570','张雨琪','31','[email protected]','1088248166370832385','2019-01-14 09:15:15');
insert into `user` (`id`, `name`, `age`, `email`, `manager_id`, `create_time`) values('1094592041087729666','刘红雨','32','[email protected]','1088248166370832385','2019-01-14 09:48:16');
2.创建maven项目
2.1.添加如下依赖
pom.xml文件内容:
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.8.RELEASE
com.mp
mybatisplus
0.0.1-SNAPSHOT
mybatisplus
mybatisplus
1.8
org.springframework.boot
spring-boot-starter-web
mysql
mysql-connector-java
runtime
org.projectlombok
lombok
provided
com.baomidou
mybatis-plus-boot-starter
3.2.0
org.apache.commons
commons-lang3
3.9
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
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 {
}
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 users = userMapper.selectList(null);
users.forEach(System.out::println);
}
}