# 1.MyBatis-Plus-入门案例
# 1.数据准备
创建数据库表
CREATE DATABASE `mybatis_plus` /*!40100 DEFAULT CHARACTER SET utf8mb4 */;
use `mybatis_plus`;
CREATE TABLE `user` (
`id` bigint(20) NOT NULL COMMENT '主键ID',
`name` varchar(30) DEFAULT NULL COMMENT '姓名',
`age` int(11) DEFAULT NULL COMMENT '年龄',
`email` varchar(50) DEFAULT NULL COMMENT '邮箱',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;.com',0,35);
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
添加数据
INSERT INTO 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');
1
2
3
4
5
6
2
3
4
5
6
创建springboot项目引入依赖
<dependencies>
<!--web依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--test测试依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
配置application.yml
server:
port: 8088
spring:
datasource:
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/mybatis_plus?serverTimezone=GMT%2B8&characterEncoding=utf-8&useSSL=false
username: root
password: 1234
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
实体类(成员变量使用包装类定义,因为普通类型有默认值,影响非空判断)
package com.oracle.pojo;
import lombok.Data;
@Data //lombok注解
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
添加mapper
public interface UserMapper extends BaseMapper<User> {
}
1
2
3
2
3
测试
package com.oracle;
import com.oracle.mapper.UserMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class MybatisPlusTest {
@Autowired
private UserMapper userMapper;
@Test
public void testSelectList(){
//selectList()根据MP内置的条件构造器查询一个list集合,null表示没有条件,即查询所有
userMapper.selectList(null).forEach(System.out::println);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
添加日志
# 配置MyBatis日志
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
1
2
3
4
2
3
4
# 2.基本CRUD
1.插入
@Test
public void testInsert(){
User user = new User(null, "张三", 23, "zhangsan@atguigu.com");
//INSERT INTO user ( id, name, age, email ) VALUES ( ?, ?, ?, ? )
int result = userMapper.insert(user);
System.out.println("受影响行数:"+result);
//2028791277338980354
System.out.println("id自动获取:"+user.getId());
}
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
最终执行的结果,所获取的id为1761203470926962690
这是因为MyBatis-Plus在实现插入数据时,会默认基于雪花算法的策略生成id
2.删除
根据id删除记录
@Test
public void testDeleteById(){
// 通过id删除用户信息
// DELETE FROM user WHERE id=?
int result = userMapper.deleteById(2028791277338980354L);
System.out.println("受影响行数:"+result);
}
1
2
3
4
5
6
7
2
3
4
5
6
7
根据id批量删除记录
@Test
public void testDeleteBatchIds(){
//通过多个id批量删除
//DELETE FROM user WHERE id IN ( ? , ? , ? )
List<Long> idList = Arrays.asList(1L, 2L, 3L);
int result = userMapper.deleteBatchIds(idList);
System.out.println("受影响行数:"+result);
}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
根据map条件删除
@Test
public void testDeleteByMap(){
// 根据map集合中所设置的条件删除记录
// DELETE FROM user WHERE name = ? AND age = ?
Map<String, Object> map = new HashMap<>();
map.put("age", 23);
map.put("name", "张三");
int result = userMapper.deleteByMap(map);
System.out.println("受影响行数:"+result);
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
3.修改
@Test
public void testUpdateById(){
User user = new User(4L, "admin", 22, null);
//UPDATE user SET name=?, age=? WHERE id=?
int result = userMapper.updateById(user);
System.out.println("受影响行数:"+result);
}
1
2
3
4
5
6
7
2
3
4
5
6
7
4.查询
根据id查询用户信息
@Test
public void testSelectById(){
//根据id查询用户信息
//SELECT id,name,age,email FROM user WHERE id=?
User user = userMapper.selectById(4L);
System.out.println(user);
}
1
2
3
4
5
6
7
2
3
4
5
6
7
根据id查询多个用户信息
@Test
public void testSelectBatchIds(){
//根据多个id查询多个用户信息
//SELECT id,name,age,email FROM user WHERE id IN ( ? , ? )
List<Long> idList = Arrays.asList(4L, 5L);
List<User> list = userMapper.selectBatchIds(idList);
list.forEach(System.out::println);
}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
根据map条件查询
@Test
public void testSelectByMap(){
//通过map条件查询用户信息
//SELECT id,name,age,email FROM user WHERE name = ? AND age = ?
Map<String, Object> map = new HashMap<>();
map.put("age", 22);
map.put("name", "admin");
List<User> list = userMapper.selectByMap(map);
list.forEach(System.out::println);
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
5、自定义功能

定义在resource/mapper下即可,这是mp定义的映射文件路径
# 3.Service
前缀命名方式区分 Mapper 层与Service层
| 操作 | service | mapper |
|---|---|---|
| 查询 | get/list | select |
| 修改 | update | update |
| 删除 | remove | delete |
| 增加 | save | insert |
| 分页 | page | selectPage |
1、IService
MyBatis-Plus中有一个接口 IService和其实现类 ServiceImpl,封装了常见的业务层逻辑
详情查看源码IService和ServiceImpl
2、创建Service接口和实现类
/**
* UserService继承IService模板提供的基础功能
*/
public interface UserService extends IService<User> {
}
1
2
3
4
5
6
2
3
4
5
6
package com.oracle.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.oracle.mapper.UserMapper;
import com.oracle.pojo.User;
import com.oracle.service.UserService;
import org.springframework.stereotype.Service;
/**
* ServiceImpl实现了IService,提供了IService中基础功能的实现
* 若ServiceImpl无法满足业务需求,则可以使用自定的UserService定义方法,并在实现类中实现
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16