【Mybatis-plus】2. Mybatis-plus 常见CURD

Mybatis-plus 常见CURD

select

如 实体类 User,映射类为 userMapper。


查询一行:selectOne(queryWrapper)

查询多行:selectList(null)、selectObjs()

查询数量:selectCount(queryWrapper)

根据 ID 查询:selectById(1L)

部分查询:selectBatchIds(Arrays.asList(1L, 2L, 3L))

条件查询:selectByMap(map)

分页查询:
Page<User> page=new Page<>(1,5);
//current:当前页
//size:页面大小
userMapper.selectPage(page,null);

Wrapper 的应用:
wrapper = new QueryWrapper<>();

条件查询:

某字段不为空:     .isNotNull()
某字段大于xx:     .ge()
某字段等于xx:     .eq()
某字段在 [a, b]:  .between()

模糊查询:

某字段不含某字符:  .notLike()
某字段含某字符:    .like()
某字段右查询:      .likeRight()

子查询:

某字段在子查询中:   .inSql()
按某字段升序:      .orderByAsc()
按某字段降序:      .orderByDesc()

insert


设置对象所有属性:insert(user)

设置对象部分属性:
User user = new User();
user.setAge(21);
user.setName("ac");
user.setEmail("ac@baomidou.com");
int i = userMapper.insert(user);


update


根据 Id 更新:
updateById(user)

delete


根据 Id 删除:
deleteById(id)

根据 Id 批量删除(batchIds):
deleteBatchIds(Arrays.asList(4L,5L,6L))
底层 sql 语句是 ... WHERE xx IN(...)

根据 Id 批量删除(map):
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("name", "Jone");
deleteByMap(map)

物理删除 和 逻辑删除

物理删除:从数据库实际删除。

逻辑删除:理论上已经删除,实际上还保留,只是状态为已删除。

如 User 表:

User字段
id
name
age
email
deleted

Mybatis-plus 高版本支持逻辑删除。

总结:

批量插入、批量更新 和 批量删除,本质上都是:insertBatchIds() || insertByMap()。

参考

https://www.cnblogs.com/kzyuan/p/12642488.html

posted @ 2021-09-24 23:40  acchris  阅读(122)  评论(0)    收藏  举报