【Plus】三、BaseMapper
三、BaseMapper
·
文章目录
- 1、BaseMapper接口
- 2、测试
- 2.1、`insert(T)`: int
- 2.2、`deleteById(Serializable)`: int
- 2.3、`deleteById(T)`: int
- 2.4、`deleteByMap(Map<String, Object>)`: int
- 2.5、`delete(Wrapper<T>)`: int
- 2.6、`deleteBatchIds(Collection<?>)`: int
- 2.7、`updateById(T)`: int
- 2.8、`update(T, Wrapper<T>)`: int
- 2.9、`selectById(Serializable)`: T
- 2.10、`selectBatchIds(Collection<? extends Serializable> idList)`: `List<T>`
- 2.11、`selectByMap(Map<String, Object>)`: `List<T>`
- 2.12、`selectOne(Wrapper<T>)`: T
- 2.13、`exists(Wrapper<T>)`: boolean
- 2.14、`selectCount(Wrapper<T>)`: Long
- 2.15、`selectList(Wrapper<T>)`: `List<T>`
- 2.16、`selectMaps(Wrapper<T>)`: `List<Map<String, Object>>`
- 2.17、`selectObjs(Wrapper<T>)`: `List<Object>`
- 2.18、`selectPage(P, Wrapper<T>)`: P
- 2.19、`selectMapsPage(P,Wrapper<T>) `: P
1、BaseMapper接口
点击 BaseMapper 访问官网!
/**
* Mapper 继承该接口后,无需编写 mapper.xml 文件,即可获得CRUD功能
* <p>这个 Mapper 支持 id 泛型</p>
*
* @author hubin
* @since 2016-01-23
*/
public interface BaseMapper<T> extends Mapper<T> {
/**
* 插入一条记录
*
* @param entity 实体对象
*/
int insert(T entity);
/**
* 根据 ID 删除
*
* @param id 主键ID
*/
int deleteById(Serializable id);
/**
* 根据实体(ID)删除
*
* @param entity 实体对象
* @since 3.4.4
*/
int deleteById(T entity);
/**
* 根据 columnMap 条件,删除记录
*
* @param columnMap 表字段 map 对象
*/
int deleteByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
/**
* 根据 entity 条件,删除记录
*
* @param queryWrapper 实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句)
*/
int delete(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
/**
* 删除(根据ID或实体 批量删除)
*
* @param idList 主键ID列表或实体列表(不能为 null 以及 empty)
*/
int deleteBatchIds(@Param(Constants.COLLECTION) Collection<?> idList);
/**
* 根据 ID 修改
*
* @param entity 实体对象
*/
int updateById(@Param(Constants.ENTITY) T entity);
/**
* 根据 whereEntity 条件,更新记录
*
* @param entity 实体对象 (set 条件值,可以为 null)
* @param updateWrapper 实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句)
*/
int update(@Param(Constants.ENTITY) T entity, @Param(Constants.WRAPPER) Wrapper<T> updateWrapper);
/**
* 根据 ID 查询
*
* @param id 主键ID
*/
T selectById(Serializable id);
/**
* 查询(根据ID 批量查询)
*
* @param idList 主键ID列表(不能为 null 以及 empty)
*/
List<T> selectBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);
/**
* 查询(根据 columnMap 条件)
*
* @param columnMap 表字段 map 对象
*/
List<T> selectByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
/**
* 根据 entity 条件,查询一条记录
* <p>查询一条记录,例如 qw.last("limit 1") 限制取一条记录, 注意:多条数据会报异常</p>
*
* @param queryWrapper 实体对象封装操作类(可以为 null)
*/
default T selectOne(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper) {
List<T> 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;
}
/**
* 根据 Wrapper 条件,判断是否存在记录
*
* @param queryWrapper 实体对象封装操作类
* @return
*/
default boolean exists(Wrapper<T> queryWrapper) {
Long count = this.selectCount(queryWrapper);
return null != count && count > 0;
}
/**
* 根据 Wrapper 条件,查询总记录数
*
* @param queryWrapper 实体对象封装操作类(可以为 null)
*/
Long selectCount(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
/**
* 根据 entity 条件,查询全部记录
*
* @param queryWrapper 实体对象封装操作类(可以为 null)
*/
List<T> selectList(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
/**
* 根据 Wrapper 条件,查询全部记录
*
* @param queryWrapper 实体对象封装操作类(可以为 null)
*/
List<Map<String, Object>> selectMaps(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
/**
* 根据 Wrapper 条件,查询全部记录
* <p>注意: 只返回第一个字段的值</p>
*
* @param queryWrapper 实体对象封装操作类(可以为 null)
*/
List<Object> selectObjs(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
/**
* 根据 entity 条件,查询全部记录(并翻页)
*
* @param page 分页查询条件(可以为 RowBounds.DEFAULT)
* @param queryWrapper 实体对象封装操作类(可以为 null)
*/
<P extends IPage<T>> P selectPage(P page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
/**
* 根据 Wrapper 条件,查询全部记录(并翻页)
*
* @param page 分页查询条件
* @param queryWrapper 实体对象封装操作类
*/
<P extends IPage<Map<String, Object>>> P selectMapsPage(P page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
}
2、测试
2.1、insert(T)
: int
User user = new User(null, "Ami", 20, "999@mm.com");
System.out.println("结果:" + userMapper.insert(user));
System.out.println(user);
==> Preparing: INSERT INTO user ( id, name, age, email ) VALUES ( ?, ?, ?, ? )
==> Parameters: 1583712489828356098(Long), Ami(String), 20(Integer), 999@mm.com(String)
<== Updates: 1
结果:1
User(id=1583712489828356098, name=Ami, age=20, email=999@mm.com)
第一次用的小伙伴肯定很好奇,我没有给表主键设置自增,我也没给主键赋值,为何主键会有值呢?
MP默认使用雪花算法给主键赋值
2.2、deleteById(Serializable)
: int
System.out.println("结果:" + userMapper.deleteById(1583712489828356098L));
==> Preparing: DELETE FROM user WHERE id=?
==> Parameters: 1583712489828356098(Long)
<== Updates: 1
结果:1
2.3、deleteById(T)
: int
User user = new User(1583717556941848577L, null, null, null);
System.out.println("结果:" + userMapper.deleteById(user));
结果与2.2如出一辙
2.4、deleteByMap(Map<String, Object>)
: int
Map<String, Object> map = new HashMap<>();
map.put("name", "张三");
map.put("age", 20);
System.out.println("结果:" + userMapper.deleteByMap(map));
==> Preparing: DELETE FROM user WHERE name = ? AND age = ?
==> Parameters: 张三(String), 20(Integer)
<== Updates: 0
结果:0
2.5、delete(Wrapper<T>)
: int
System.out.println("结果:" + userMapper.delete(new QueryWrapper<User>().eq("id", 1L)));
==> Preparing: DELETE FROM user WHERE (id = ?)
==> Parameters: 1(Long)
<== Updates: 1
结果:1
2.6、deleteBatchIds(Collection<?>)
: int
System.out.println("结果:" + userMapper.deleteBatchIds(Arrays.asList(1L, 2L, 3L)));
==> Preparing: DELETE FROM user WHERE id IN ( ? , ? , ? )
==> Parameters: 1(Long), 2(Long), 3(Long)
<== Updates: 3
结果:3
2.7、updateById(T)
: int
User user = new User();
user.setId(4L);
user.setName("嘎嘎嘎");
user.setAge(3);
user.setEmail("gagaga@gaga.com");
System.out.println("结果:" + userMapper.updateById(user));
==> Preparing: UPDATE user SET name=?, age=?, email=? WHERE id=?
==> Parameters: 嘎嘎嘎(String), 3(Integer), gagaga@gaga.com(String), 4(Long)
<== Updates: 1
结果:1
2.8、update(T, Wrapper<T>)
: int
User user = new User();
user.setId(400L);
user.setName("咕咕咕");
user.setAge(16);
user.setEmail("gugugu@gugu.com");
System.out.println("结果:" + userMapper.update(user,
new UpdateWrapper<User>().eq("id", 4L)));
==> Preparing: UPDATE user SET name=?, age=?, email=? WHERE (id = ?)
==> Parameters: 咕咕咕(String), 16(Integer), gugugu@gugu.com(String), 4(Long)
<== Updates: 1
结果:1
2.9、selectById(Serializable)
: T
System.out.println("结果:" + userMapper.selectById(4L));
==> Preparing: SELECT id,name,age,email FROM user WHERE id=?
==> Parameters: 4(Long)
<== Columns: id, name, age, email
<== Row: 4, 嘎嘎嘎, 3, gagaga@gaga.com
<== Total: 1
结果:User(id=4, name=嘎嘎嘎, age=3, email=gagaga@gaga.com)
2.10、selectBatchIds(Collection<? extends Serializable> idList)
: List<T>
userMapper.selectBatchIds(Arrays.asList(1L, 2L, 3L)).forEach(System.out::println);
==> Preparing: SELECT id,name,age,email FROM user WHERE id IN ( ? , ? , ? )
==> Parameters: 1(Long), 2(Long), 3(Long)
<== Columns: id, name, age, email
<== Row: 1, lili, 19, lili@lili.com
<== Row: 2, 小明, 12, xxx@xxx.com
<== Row: 3, 小黑, 20, hhi@hi.com
<== Total: 3
User(id=1, name=lili, age=19, email=lili@lili.com)
User(id=2, name=小明, age=12, email=xxx@xxx.com)
User(id=3, name=小黑, age=20, email=hhi@hi.com)
2.11、selectByMap(Map<String, Object>)
: List<T>
Map<String, Object> map = new HashMap<>();
map.put("name", "嘎嘎嘎");
map.put("age", 3);
userMapper.selectByMap(map).forEach(System.out::println);
==> Preparing: SELECT id,name,age,email FROM user WHERE name = ? AND age = ?
==> Parameters: 嘎嘎嘎(String), 3(Integer)
<== Columns: id, name, age, email
<== Row: 4, 嘎嘎嘎, 3, gagaga@gaga.com
<== Total: 1
User(id=4, name=嘎嘎嘎, age=3, email=gagaga@gaga.com)
2.12、selectOne(Wrapper<T>)
: T
System.out.println("结果:" + userMapper.selectOne(new QueryWrapper<User>().eq("id", 1L)));
==> Preparing: SELECT id,name,age,email FROM user WHERE (id = ?)
==> Parameters: 1(Long)
<== Columns: id, name, age, email
<== Row: 1, lili, 19, lili@lili.com
<== Total: 1
结果:User(id=1, name=lili, age=19, email=lili@lili.com)
2.13、exists(Wrapper<T>)
: boolean
System.out.println("结果:" + userMapper.exists(new QueryWrapper<User>().eq("id", 1L)));
==> Preparing: SELECT COUNT( * ) FROM user WHERE (id = ?)
==> Parameters: 1(Long)
<== Columns: COUNT( * )
<== Row: 1
<== Total: 1
结果:true
2.14、selectCount(Wrapper<T>)
: Long
System.out.println("结果:" + userMapper.selectCount(new QueryWrapper<User>().eq("id", 1L)));
==> Preparing: SELECT COUNT( * ) FROM user WHERE (id = ?)
==> Parameters: 1(Long)
<== Columns: COUNT( * )
<== Row: 1
<== Total: 1
结果:1
2.15、selectList(Wrapper<T>)
: List<T>
userMapper.selectList(new QueryWrapper<User>().eq("age", 20)).forEach(System.out::println);
==> Preparing: SELECT id,name,age,email FROM user WHERE (age = ?)
==> Parameters: 20(Integer)
<== Columns: id, name, age, email
<== Row: 3, 小黑, 20, hhi@hi.com
<== Row: 1583722107052756994, Ami, 20, 999@mm.com
<== Row: 1583722317866962946, 王五, 20, wangwu@mm.com
<== Total: 3
User(id=3, name=小黑, age=20, email=hhi@hi.com)
User(id=1583722107052756994, name=Ami, age=20, email=999@mm.com)
User(id=1583722317866962946, name=王五, age=20, email=wangwu@mm.com)
2.16、selectMaps(Wrapper<T>)
: List<Map<String, Object>>
userMapper.selectMaps(new QueryWrapper<User>().eq("age", 20)).forEach(System.out::println);
结果与2.15如出一辙
2.17、selectObjs(Wrapper<T>)
: List<Object>
userMapper.selectObjs(new QueryWrapper<User>().eq("age", 20)).forEach(System.out::println);
<== Columns: id, name, age, email
<== Row: 3, 小黑, 20, hhi@hi.com
<== Row: 1583722107052756994, Ami, 20, 999@mm.com
<== Row: 1583722317866962946, 王五, 20, wangwu@mm.com
<== Total: 3
3
1583722107052756994
1583722317866962946
2.18、selectPage(P, Wrapper<T>)
: P
userMapper.selectPage(new Page(1, 5), new QueryWrapper<User>().ne("id", 1L));
==> Preparing: SELECT id,name,age,email FROM user WHERE (id <> ?)
==> Parameters: 1(Long)
<== Columns: id, name, age, email
<== Row: 2, 小明, 12, xxx@xxx.com
<== Row: 3, 小黑, 20, hhi@hi.com
<== Row: 4, 咕咕咕, 16, gugugu@gugu.com
<== Row: 5, Billie, 24, test5@baomidou.com
<== Row: 1583722107052756994, Ami, 20, 999@mm.com
<== Row: 1583722178544787457, 张三, 22, zs@mm.com
<== Row: 1583722246144348161, 李四, 21, lisi@mm.com
<== Row: 1583722317866962946, 王五, 20, wangwu@mm.com
<== Row: 1583722690497302529, 赵六, 22, zliu@mm.com
<== Total: 9
2.19、selectMapsPage(P,Wrapper<T>)
: P
userMapper.selectMapsPage(new Page(1, 5), new QueryWrapper<User>().ne("id", 1L));
结果与2.18如出一辙
更多推荐
已为社区贡献1条内容
所有评论(0)