- 一、通用Mapper
- 二、使用方式
- 1、源码分析
- :one:Insert 插入方法
- :two:Delete 删除方法
- :three:Update 修改方法
- :four: Select 查询方法
一、通用Mapper
在MyBatis-Plus里面,可以实现对数据库的CRUD操作,官方对通用Mapper的解释如下说明:
通用 CRUD 封装BaseMapper (opens new window)接口,为 Mybatis-Plus 启动时自动解析实体表关系映射转换为 Mybatis 内部对象注入容器,
1、泛型 T 为任意实体对象,
2、参数 Serializable 为任意类型主键 Mybatis-Plus 不推荐使用复合主键约定每一张表都有自己的唯一 id 主键,
3、对象 Wrapper 为 条件构造器
这张图的大致含义就是 MyBatis-PLUS利用注解,核心模块和生成器等组件,来完成对实体的扫描,自动映射解析实体对象,完成表名与列名的对应关系。通过封装的SQL语句,最终完成 CRUD操作。
在Mapper的接口上需要继承一个BaseMapper的接口,其泛型为操作的数据库表对应的pojo类。
@Repository("userMapper")
public interface UserMapper extends BaseMapper{
}
1、源码分析
BaseMapper的源码如下所示,
public interface BaseMapper1️⃣Insert 插入方法extends Mapper { int insert(T entity); int deleteById(Serializable id); int deleteById(T entity); int deleteByMap(@Param(Constants.COLUMN_MAP) Map columnMap); int delete(@Param(Constants.WRAPPER) Wrapper queryWrapper); int deleteBatchIds(@Param(Constants.COLLECTION) Collection> idList); int updateById(@Param(Constants.ENTITY) T entity); int update(@Param(Constants.ENTITY) T entity, @Param(Constants.WRAPPER) Wrapper updateWrapper); T selectById(Serializable id); List selectBatchIds(@Param(Constants.COLLECTION) Collection extends Serializable> idList); List selectByMap(@Param(Constants.COLUMN_MAP) Map columnMap); default T selectOne(@Param(Constants.WRAPPER) Wrapper queryWrapper) { List ts = this.selectList(queryWrapper); if (CollectionUtils.isNotEmpty(ts)) { if (ts.size() != 1) { throw ExceptionUtils.mpe("One record is expected, but the query result is multiple records"); } return ts.get(0); } return null; } default boolean exists(Wrapper queryWrapper) { Long count = this.selectCount(queryWrapper); return null != count && count > 0; } Long selectCount(@Param(Constants.WRAPPER) Wrapper queryWrapper); List selectList(@Param(Constants.WRAPPER) Wrapper queryWrapper); List
参数说明:
| 类型 | 参数名 | 描述 |
|---|---|---|
| T | Entity | 实体对象 |
案例演示:
@Test
public void test2() {
// 新增用户信息 INSERT INTO user ( id, name, age, email ) VALUES ( ?, ?, ?, ? )
User user = new User("joker",23,"joker@qq.com");
int insert = userMapper.insert(user);
System.out.println("成功插入"+insert+"条数据");
// 在MyBatis里默认是使用雪花算法创建id 不会默认使用数据库里面的自增主键
System.out.println("id:"+user.getId());
}
2️⃣Delete 删除方法注意:如果未设置主键,MyBatis-plus则会使用雪花算法自动生成一个随机的Long型的的主键
方法介绍:
// 根据 entity 条件,删除记录 int delete(@Param(Constants.WRAPPER) Wrapperwrapper); // 删除(根据ID 批量删除) int deleteBatchIds(@Param(Constants.COLLECTION) Collection extends Serializable> idList); // 根据 ID 删除 int deleteById(Serializable id); // 根据 columnMap 条件,删除记录 int deleteByMap(@Param(Constants.COLUMN_MAP) Map columnMap);
参数说明:
| 类型 | 参数名 | 描述 |
|---|---|---|
| Wrapper | wrapper | 实体对象封装操作类(可以为 null) |
| Collection extends Serializable> | idList | 主键 ID 列表(不能为 null 以及 empty) |
| Serializable | id | 主键 ID |
| Map | columnMap | 表字段 map 对象 |
案例演示:
1、delete
@Test
public void test() {
// 根据 entity 条件,删除记录
//int delete(@Param(Constants.WRAPPER) Wrapper wrapper);
User user = new User();
user.setId(13L);
Wrapper wrapper = new UpdateWrapper<>(user);
int delete = userMapper.delete(wrapper);
System.out.println("删除了"+delete+"条数据");
}
2、deleteBatchIds:
@Test
public void test5() {
// 批量删除 DELETE FROM user WHERe id IN ( ? , ? )
Collection ids = new ArrayList<>();
ids.add(5L);
ids.add(2L);
int deleteBatchIds = userMapper.deleteBatchIds(ids);
System.out.println("删除了"+deleteBatchIds+"条数据");
}
3、deleteById
@Test
public void test3() {
// 根据id删除 DELETE FROM user WHERe id=?
int deleteById = userMapper.deleteById(1529380500983910401L);
System.out.println("成功删除"+deleteById);
}
4、deleteByMap
@Test
public void test4() {
// 根据条件删除数据 可以删除多个 DELETE FROM user WHERe name = ? AND age = ?
Map map = new HashMap<>();
map.put("name", "张三");
map.put("age", 23);
int deleteByMap = userMapper.deleteByMap(map);
System.out.println("删除了"+deleteByMap);
}
3️⃣Update 修改方法
// 根据 whereWrapper 条件,更新记录 int update(@Param(Constants.ENTITY) T updateEntity, @Param(Constants.WRAPPER) WrapperwhereWrapper); // 根据 ID 修改 int updateById(@Param(Constants.ENTITY) T entity);
参数说明:
| 类型 | 参数名 | 描述 |
|---|---|---|
| T | entity | 实体对象 (set 条件值,可为 null) |
| Wrapper | updateWrapper | 实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句) |
案例演示:
1、Update
@Test
public void testupdate() {
// 根据id修改数据 UPDATE user SET name=?, age=?, email=? WHERe id=?
User user = new User(1L,"koppy",25,"koppy@163.com");
User wrapperUser = new User();
wrapperUser.setId(1L);
Wrapper wrapper = new UpdateWrapper<>(wrapperUser);
int updateById = userMapper.update(user, wrapper);
System.out.println("修改了"+updateById+"条数据");
}
2、updateById
@Test
public void test6() {
// 根据id修改数据 UPDATE user SET name=?, age=?, email=? WHERe id=?
User user = new User(1L,"koppy",45,"koppy@163.com");
int updateById = userMapper.updateById(user);
System.out.println("修改了"+updateById+"条数据");
}
4️⃣ Select 查询方法
// 根据 ID 查询 T selectById(Serializable id); // 根据 entity 条件,查询一条记录 T selectOne(@Param(Constants.WRAPPER) WrapperqueryWrapper); // 查询(根据ID 批量查询) List selectBatchIds(@Param(Constants.COLLECTION) Collection extends Serializable> idList); // 根据 entity 条件,查询全部记录 List selectList(@Param(Constants.WRAPPER) Wrapper queryWrapper); // 查询(根据 columnMap 条件) List selectByMap(@Param(Constants.COLUMN_MAP) Map columnMap); // 根据 Wrapper 条件,查询全部记录 List
参数说明:
| 类型 | 参数名 | 描述 |
|---|---|---|
| Serializable | id | 主键 ID |
| Wrapper | queryWrapper | 实体对象封装操作类(可以为 null) |
| Collection extends Serializable> | idList | 主键 ID 列表(不能为 null 以及 empty) |
| Map | columnMap | 表字段 map 对象 |
| IPage | page | 分页查询条件(可以为 RowBounds.DEFAULT) |
案例演示:
1、selectById
@Test
public void testSelect() {
User user = userMapper.selectById(1L);
System.out.println(user);
}
2、selectOne
@Test
public void TestSelectOne() {
User user =new User();
user.setAge(18);
Wrapper queWrapper =new QueryWrapper<>(user);
User selectOne = userMapper.selectOne(queWrapper);
System.out.println(selectOne);
}
注意:在使用SelectOne的方法的时候,需要注意的是!!!selectOne中方法如果查出多条数据,则会抛出一个警告,但是不会报错,原因就是SelectOne的底层中,对查询结果进行了一个判断,如果将结果>1则会抛出warn,否则会返回第一个查询结果
selectOne的底层源码:
default T selectOne(@Param(Constants.WRAPPER) Wrapper queryWrapper) {
List ts = this.selectList(queryWrapper);
if (CollectionUtils.isNotEmpty(ts)) {
if (ts.size() != 1) {
throw ExceptionUtils.mpe("One record is expected, but the query result is multiple records");
}
return ts.get(0);
}
return null;
}
**还要额外说明一点:我用的是MP的最新版本,不会像网上说的抛出警告,可能旧版本会吧,这个还需要看源码
3、selectByPage 不带条件的查询
@Test
public void selectByPage() {
Page page = new Page<>();
page.setCurrent(1);
page.setSize(5);
User user = new User();
user.setAge(15);
Wrapper wrapper =new QueryWrapper<>();
Page page2 = userMapper.selectPage(page, null);
List list = page2.getRecords();
for (User user2 : list) {
System.out.println(user2);
}
}
selectMapsPage带条件的查询
@Test
public void testSelectMapsPage() {
QueryWrapper queryWrapper = new QueryWrapper<>();
// 返回指定列,这里是数据库的字段,而不是实体类的属性
queryWrapper.select("id", "name", "age", "email");
Map paramMaps = new HashMap<>();
paramMaps.put("age",21);
queryWrapper.allEq(paramMaps);
Page



