整合 idea + SpringBoot + MybatisPlus
环境 idea 2020.3 + SpringBoot 2.4.1 + MybatisPlus 3.4.1 + MySQL 5.6
- 【第一步】新建一个 SpringBoot 工程。
- 【第二步】将 Maven 更换为阿里云仓库。
<!-- 修改 maven 根目录下的 conf 文件夹中的 setting.xml 文件 -->
<mirrors>
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
<!-- 配置pom.xml 文件 -->
<repositories>
<repository>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
- 【第三步】pom 文件导入依赖
<!-- MybatisPlus 依赖 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
<!-- MySQL 依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- lombok 依赖 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
- 【第四步】配置 yaml 文件
MySQL 配置
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
# 配置时区: serverTimezone=GMT%2B8 表示北京时间,UTC 表示全球标准时间,比北京晚了 8 小时。
# 是否指定 SSL连接: useSSL=false 表示不使用。
url: jdbc:mysql://localhost:3306/cooper_mybatisplus?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8
username: root
password: mysql123
- 【第五步】MySQL 数据库中创建 sys_user 表。
-- 创建数据库 cooper_mybatisplus ,默认字符集 latin1
CREATE DATABASE IF NOT EXISTS cooper_mybatisplus DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
-- 创建数据表 sys_user
CREATE TABLE `sys_user` (
`id` BIGINT(20) NOT NULL COMMENT '主键ID',
`name` VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
`age` INT(11) NULL DEFAULT NULL COMMENT '年龄',
`email` VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户表';
-- 添加数据源
INSERT INTO sys_user (id, name, age, email) VALUES
(1, 'Jone', 18, 'test1@baomidou.com'),
(2, 'Jack', 20, 'test2@baomidou.com'),
(3, 'Tom', 28, 'test3@baomidou.com'),
(4, 'Sandy', 21, 'test4@baomidou.com'),
(5, 'Billie', 24, 'test5@baomidou.com');
- 【第六步】创建 JavaBean ,任务就是: Write once, run anywhere, reuse everywhere
package com.example.cooper_mybatisplus.sys.pojo.entity;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@Data
@TableName("sys_user")
public class SysUserEntity {
private Long id;
private String name;
private Integer age;
private String email;
}
- 【第七步】创建 Mapper
package com.example.cooper_mybatisplus.sys.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.cooper_mybatisplus.sys.pojo.entity.SysUserEntity;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface SysUserMapper extends BaseMapper<SysUserEntity> {
}
- 【第八步】测试
package com.example.cooper_mybatisplus;
import com.example.cooper_mybatisplus.sys.mapper.SysUserMapper;
import com.example.cooper_mybatisplus.sys.pojo.entity.SysUserEntity;
import org.junit.Assert;
import org.junit.jupiter.api.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;
@RunWith(SpringRunner.class)
@SpringBootTest
class CooperMybatisplusApplicationTests {
@Autowired
private SysUserMapper sysUserMapper;
@Test
public void testSelect() {
System.out.println(("----- selectAll method test ------"));
List<SysUserEntity> userList = sysUserMapper.selectList(null);
Assert.assertEquals(5, userList.size());
userList.forEach(System.out::println);
}
}
- 控制台输出
SysUserEntity(id=1, name=Jone, age=18, email=test1@baomidou.com)
SysUserEntity(id=2, name=Jack, age=20, email=test2@baomidou.com)
SysUserEntity(id=3, name=Tom, age=28, email=test3@baomidou.com)
SysUserEntity(id=4, name=Sandy, age=21, email=test4@baomidou.com)
SysUserEntity(id=5, name=Billie, age=24, email=test5@baomidou.com)