Mybatis-Plus
wrapper介绍:
AbstractWrapper: 用于查询条件封装,生成sql的where条件
AbstractLambdaWrapper: Lambda语法使用Wrapper统一处理解析lambda获取column
QueryWrapper: Entity 对象封装操作类,不是用lambda
UpdateWrapper: Update条件封装,用于Entity对象更新操作
其中QueryWrapper是最常用的
Mapper CRUD接口
int insert (T entity)//插入一条记录
deleteById(Serializable id) //根据Id删除
deleteByMap( Map<String, Object> columnMap) // 根据 columMap条件删除记录
int delete( Wrapper<T> wrapper)//根据wrapper里面Entity条件删除
int deleteBatchIds( Collection<? extends Serializable> idList); //根据ID批量删除
int updateById(T entity);//根据ID修改
int update(T entity, Wrapper<T> updateWrapper);//entity作为set条件值,updateWrapprt里面的entity用于生成where条件值
T seleteById(String id) //根据id查询
List<T> selectBatchIds( Collection<? extends Serializable> idList);//根据id批量查询
List<T> selectByMap( Map<String, Object> columnMap);//根据map条件
T selectOne( Wrapper<T> queryWrapper);//根据wrapper里面的entity查找,如果不是唯一需要添加wrapper.last("limit 1")
Integer selectCount( Wrapper<T> queryWrapper);//根据wrapper条件查询总数
List<T> selectList(Wrapper<T> queryWrapper); //根据条件查询实体集合
List<Map<String, Object>> selectMaps( Wrapper<T> queryWrapper);//根据 Wrapper 条件,查询全部记录
List<Object> selectObjs( Wrapper<T> queryWrapper);//根据 Wrapper 条件,查询全部记录, 注意: 只返回第一个字段的值
Page<T> selectPage(IPage<T> page, Wrapper<T> queryWrapper); //返回实体分页对象
IPage<Map<String, Object>> selectMapsPage(IPage<T> page, Wrapper<T> queryWrapper);//返回字段映射对象 Map 分页对象;
Service CURD接口
boolean save(T entity);
boolean saveBatch(Collection<T> entityList);
boolean saveBatch(Collection<T> entityList, int batchSize);//batchSize每次的数量
boolean saveOrUpdateBatch(Collection<T> entityList);//批量修改插入
boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize);
boolean removeById(Serializable id);
boolean removeByMap(Map<String, Object> columnMap);
boolean remove(Wrapper<T> queryWrapper);//queryWrapper 实体包装类,根据entuty条件删除
boolean removeByIds(Collection<? extends Serializable> idList);
boolean updateById(T entity);
boolean update(T entity, Wrapper<T> updateWrapper);
boolean updateBatchById(Collection<T> entityList, int batchSize);//批量更新
boolean saveOrUpdate(T entity);//TableId 注解存在更新记录,否插入一条记录
T getById(Serializable id);//根据id查询
Collection<T> listByIds(Collection<? extends Serializable> idList);//查询(根据ID 批量查询)
Collection<T> listByMap(Map<String, Object> columnMap);
T getOne(Wrapper<T> queryWrapper, boolean throwEx);//throwEx 有多个 result 是否抛出异常
Map<String, Object> getMap(Wrapper<T> queryWrapper);//根据 Wrapper,查询一条记录
Object getObj(Wrapper<T> queryWrapper);//根据 Wrapper,查询一条记录
int count(Wrapper<T> queryWrapper);//根据 Wrapper 条件,查询总记录数
List<T> list(Wrapper<T> queryWrapper);//查询列表
IPage<T> page(IPage<T> page, Wrapper<T> queryWrapper);//page为翻页对象
List<Map<String, Object>> listMaps(Wrapper<T> queryWrapper);//查询列表
List<Object> listObjs(Wrapper<T> queryWrapper);//根据 Wrapper 条件,查询全部记录
IPage<Map<String, Object>> pageMaps(IPage<T> page, Wrapper<T> queryWrapper);
构造器方法
常用方法示例
修改指定值
UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>();
userUpdateWrapper.eq("name", "lqf");
int update = mapper.update(user, userUpdateWrapper);
查找不为空
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("name", "lqf");
queryWrapper.isNotNull("name");
查询为某列为空或等于某值/查询A列等于某值或B列等于某值
//查询班级Id为空或者为指定值
query.lambda().and(Obj -> Obj.eq(User::getClazzId, (String) params.get("clszzId")).or().isNull(User::getClazzId));
//查询A列等于某值或B列等于某值
//(id='columnId' 'or' parent_id='columnId') and 1=1
if(StringUtil.isNotBlank(columnId)) {
StringBuilder column = new StringBuilder("(id='");
column.append(columnId);
column.append("' or ");
column.append("parent_id = '");
column.append(columnId);
column.append("') and 1 ");
query.eq(column.toString(), "1"); //自己
}
根据时间区间查询
//这里前端采用vue的日期时间插件将值赋给orderTime,传到后台做日期区间查询
<el-date-picker
style="width: 30%;margin-right: 20px"
value-format="yyyy-MM-dd"
v-model="orderTime"
type="daterange"
range-separator="-"
start-placeholder="开始日期"
end-placeholder="结束日期">
</el-date-picker>
AbstractWrapper query = new QueryWrapper<>();
String beginOrderTime = (String) params.get("time[0]");
String endOrderTime = (String) params.get("time[1]");
if (StringUtil.isNotBlank(beginOrderTime) && StringUtil.isNotBlank(endOrderTime)) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date beginTime = formatter.parse(beginOrderTime);
Date endTime = formatter.parse(endOrderTime);
query.between("creation_time", beginTime, endTime);
}
and or
1
QueryWrapper userWrapper = new QueryWrapper();
userWrapper.eq("name", name);
userWrapper.eq("pwd", pwd).or().eq("phone", phone);
这种写法拼出来的SQL语句是这样的
select * from user where (name = ? and pwd= ? or phone = ?)
2
QueryWrapper userWrapper = new QueryWrapper();
userWrapper.eq("name", name);
userWrapper.and(wrapper -> wrapper.eq("pwd", pwd).or().eq("phone", phone));
这种写法拼出来的SQL语句是这样的
select * from user where name = ? and ( pwd= ? or phone = ?)
批量删除
String[] ids = ids.split(",");
List<String> idList = Arrays.asList(ids);
eventService.removeByIds(idList);
存在||不存在
AbstractWrapper queryTags = new QueryWrapper<>();
queryTags.in("id", ids);
List<PubTag> pubTagList = pubTagService.list(queryTags);
查询指定列
QueryWrapper query = new QueryWrapper();
query.select("id", "name");
查到指定条数数据
LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(User::getStatus,3)..last("limit 500");
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("type", "1").last(" limit 500 ");
通用 Mapper常用方法
Select
方法:List select(T record);
说明:根据实体中的属性值进行查询,查询条件使用等号
方法:T selectByPrimaryKey(Object key);
说明:根据主键字段进行查询,方法参数必须包含完整的主键属性,查询条件使用等号
方法:List selectAll();
说明:查询全部结果,select(null)方法能达到同样的效果
方法:T selectOne(T record);
说明:根据实体中的属性进行查询,只能有一个返回值,有多个结果是抛出异常,查询条件使用等号
方法:int selectCount(T record);
说明:根据实体中的属性查询总数,查询条件使用等号
Insert
方法:int insert(T record);
说明:保存一个实体,null的属性也会保存,不会使用数据库默认值
方法:int insertSelective(T record);
说明:保存一个实体,null的属性不会保存,会使用数据库默认值
方法:insertList(List<? extends T> recordList);
说明:批量插入,支持批量插入的数据库可以使用,例如MySQL,H2等 不支持主键策略,插入前需要设置好主键的值
Update
方法:int updateByPrimaryKey(T record);
说明:根据主键更新实体全部字段,null值会被更新
方法:int updateByPrimaryKeySelective(T record);
说明:根据主键更新属性不为null的值
Delete
方法:int delete(T record);
说明:根据实体属性作为条件进行删除,查询条件使用等号
方法:int deleteByPrimaryKey(Object key);
说明:根据主键字段进行删除,方法参数必须包含完整的主键属性
Example
方法:List selectByExample(Object example);
说明:根据Example条件进行查询
重点:这个查询支持通过Example类指定查询列,通过selectProperties方法指定查询列
方法:int selectCountByExample(Object example);
说明:根据Example条件进行查询总数
方法:int updateByExample(@Param(“record”) T record, @Param(“example”) Object example);
说明:根据Example条件更新实体record包含的全部属性,null值会被更新
方法:int updateByExampleSelective(@Param(“record”) T record, @Param(“example”) Object example);
说明:根据Example条件更新实体record包含的不是null的属性值
方法:int deleteByExample(Object example);
说明:根据Example条件删除数据