准备工作:
数据库:
CREATE DATABASE IF NOT EXISTS day01;//建库
USE day01;//使用当前库
CREATE TABLE IF NOT EXISTS 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)
);//建表
INSERT INTO USER (id, NAME, age, email) VALUES
(1, 'Stone', 18, 'test1@xxx.com'),
(2, 'Jack', 20, 'test2@xxx.com'),
(3, 'Rose', 28, 'test3@xxx.com'),
(4, 'Sam', 21, 'test4@xxx.com'),
(5, 'Polo', 24, 'test5@xxx.com');//插入数据
SELECT * FROM USER;//查询插入数据
省略SpringBoot依赖配置pom文件及application.yaml配置文件
启动类:位于com.xxx下
@MapperScan("com.xxx.mapper")
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class,args);
}
}
实体类:位于com.xxx.pojo
@Data
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
UserMapper接口:位于com.xxx.mapper下
public interface UserMapper extends BaseMapper<User> {
}
测试类:位于src.test.java.com.xxx.test下
将后文各操作替换至注释处即可。
@SpringBootTest
public class MyBatisPlusTest {
@Autowired
private UserMapper userMapper;
//insert测试方法
//delete测试方法
//update测试方法
//select测试方法
}
下述操作均是使用MyBatis提供的BaseMapper中的方法。
1.insert操作
// 插入一条记录
// T 就是要插入的实体对象
// 默认主键生成策略为雪花算法
int insert(T entity);
类型 | 参数名 | 描述 |
---|---|---|
T | entity | 实体对象 |
@Test
public void test_insert(){
User user = new User();
user.setAge(25);
user.setName("张龙");
user.setEmail("123456789@qq.com");
int rows = userMapper.insert(user);
System.out.println(rows);
}
insert操作较为简单,没有其他的多余注意事项。
2.delete操作
delete操作,MyBatis-Plus提供了多种可选的方法:
// 根据 entity 条件,删除记录
int delete(@Param(Constants.WRAPPER) Wrapper<T> wrapper);
// 删除(根据ID 批量删除)
int deleteBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);
// 根据 ID 删除
int deleteById(Serializable id);
// 根据 columnMap 条件,删除记录
int deleteByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
类型 | 参数名 | 描述 |
---|---|---|
Wrapper<T> | wrapper | 实体对象封装操作类(可以为 null) |
Collection<? extends Serializable> | idList | 主键 ID 列表(不能为 null 以及 empty) |
Serializable | id | 主键 ID |
Map<String, Object> | columnMap | 表字段 map 对象 |
@Test
public void test_delete(){
//根据id删除
int rows = userMapper.deleteById(1687124343556005889L);
System.out.println("rows = " + rows);
//根据age = 20
Map param = new HashMap();
param.put("age",20); // age = 20 and name = xx,只需将条件put到map中即可完成条件拼接
int i = userMapper.deleteByMap(param);
System.out.println("i = " + i);
//wrapper 条件封装对象,无限的封装条件
//userMapper.delete(wrapper);
}
delete操作时,如果有多个查询条件且不根据主键删除,那么就需要使用deleteByMap()方法,该方法需要传入一个map来做参数,map中存储的就是delete操作所需的条件,key表示属性(列名),value为该属性的值,多条件下只需将条件put到map中即可。
3.update操作
update操作也较为简单
// 根据 whereWrapper 条件,更新记录
int update(@Param(Constants.ENTITY) T updateEntity,
@Param(Constants.WRAPPER) Wrapper<T> whereWrapper);
// 根据 ID 修改 主键属性必须值
int updateById(@Param(Constants.ENTITY) T entity);
类型 | 参数名 | 描述 |
---|---|---|
T | entity | 实体对象 (set 条件值,可为 null) |
Wrapper<T> | updateWrapper | 实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句) |
@Test
public void test_update(){
//TODO: update 当属性值为null的时候,不修改!
//updatebyId 实体类id必须有值
//user id = 1 age改为30
User user = new User();
user.setId(1L);
user.setAge(30);
// update user set age = 30 where id = 1
int i = userMapper.updateById(user);
System.out.println("i = " + i);
//将所有人的年龄改为22
//update 实体类可以没有id值
User user1 = new User();
user1.setAge(22);
int rows = userMapper.update(user,null); //null表示无条件
System.out.println("rows = " + rows);
}
应当注意:
update 操作当属性值为null的时候,不修改!即:id字段在实体类中设置为了Integer类型,当该值为null时,即使满足更新条件,也不会修改其值,但如果id字段在实体类中设置为int类型,则满足条件时就会被修改。使用updatebyId()方法更新数据时,实体类id必须有值,即更新操作查询的条件必须有值。
4.select操作
MyBatis-Plus提供了丰富的select方法来满足各种查询需求:
// 根据 ID 查询
T selectById(Serializable id);
// 根据 entity 条件,查询一条记录
T selectOne(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
// 查询(根据ID 批量查询)
List<T> selectBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);
// 根据 entity 条件,查询全部记录
List<T> selectList(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
// 查询(根据 columnMap 条件)
List<T> selectByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
// 根据 Wrapper 条件,查询全部记录
List<Map<String, Object>> selectMaps(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
// 根据 Wrapper 条件,查询全部记录。注意: 只返回第一个字段的值
List<Object> selectObjs(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
// 根据 entity 条件,查询全部记录(并翻页)
IPage<T> selectPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
// 根据 Wrapper 条件,查询全部记录(并翻页)
IPage<Map<String, Object>> selectMapsPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
// 根据 Wrapper 条件,查询总记录数
Integer selectCount(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
类型 | 参数名 | 描述 |
---|---|---|
Serializable | id | 主键 ID |
Wrapper<T> | queryWrapper | 实体对象封装操作类(可以为 null) |
Collection<? extends Serializable> | idList | 主键 ID 列表(不能为 null 以及 empty) |
Map<String, Object> | columnMap | 表字段 map 对象 |
IPage<T> | page | 分页查询条件(可以为 RowBounds.DEFAULT) |
@Test
public void test_select(){
User user = userMapper.selectById(1);
System.out.println("user = " + user);
//ids集合查询
List<Long> ids = new ArrayList<>();
ids.add(1L); ids.add(2L);
List<User> users = userMapper.selectBatchIds(ids);
System.out.println("users = " + users);
}
select操作时,和delete操作一样,需要注意多条件场景下的方法参数,即所需的条件。
完整测试类:
package com.xxx.test;
import com.xxx.mapper.UserMapper;
import com.xxx.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@SpringBootTest
public class MyBatisPlusTest {
@Autowired
private UserMapper userMapper;
@Test
public void test_insert(){
User user = new User();
user.setAge(25);
user.setName("张三");
user.setEmail("xxxx@xxx.xxx");
//baseMapper提供的数据库插入方法
int row = userMapper.insert(user);
}
@Test
public void test_delete(){
//根据id删除
int rows = userMapper.deleteById(1687124343523455889L);
System.out.println("rows = " + rows);
//根据age = 20
Map param = new HashMap();
param.put("age",20); // age = 20 and name = xx
int i = userMapper.deleteByMap(param);
System.out.println("i = " + i);
//wrapper 条件封装对象,无限的封装条件
//userMapper.delete(wrapper);
}
@Test
public void test_update(){
//TODO: update 当属性值为null的时候,不修改!
//updatebyId 实体类id必须有值
//user id = 1 age改为30
User user = new User();
user.setId(1L);
user.setAge(30);
// update user set age = 30 where id = 1
int i = userMapper.updateById(user);
System.out.println("i = " + i);
//将所有人的年龄改为22
//update 实体类可以没有id值
User user1 = new User();
user1.setAge(22);
int rows = userMapper.update(user,null); //null没条件
System.out.println("rows = " + rows);
}
@Test
public void test_select(){
User user = userMapper.selectById(1);
System.out.println("user = " + user);
//ids集合查询
List<Long> ids = new ArrayList<>();
ids.add(1L); ids.add(2L);
List<User> users = userMapper.selectBatchIds(ids);
System.out.println("users = " + users);
}
}