方法一:
- public interface userInfoMapper extends BaseMapper<UserInfo> {
- //小程序开发定制清空指定表
- @Update("truncate table user")
- void deleteUserTemp();
- }
方法二:
小程序开发定制直接使用自带的remove接口 ,同时使用QueryWrapper参数如:
userInfoTempService.remove(new QueryWrapper<>())
引申一下 Mybatis-plus小程序开发定制这个好用的框架:
我们知道 小程序开发定制是一个基于 java 小程序开发定制的持久层框架,小程序开发定制它内部封装了 jdbc,极大提高了我们的开发效率。
但是使用 Mybatis 开发也有很多痛点:
每个 Dao 接口都需要自己定义一堆增删改查方法:
- public interface UserDao {
- // 获取所有用户信息
- List<User> getUserList();
-
- // 根绝 id 获取用户信息
- User getUserById(int id);
-
- // 新增用户信息
- boolean add(User user);
-
- // 更新用户信息
- boolean update(User user);
-
- // 删除用户信息
- boolean delete(int id);
- }
每个 Mapper 文件都需要写一堆
基本的增删改查语句。
3.如果查询的列表需要分页,我们还需要给查询方法封装成分页对象。
你可能会说:Mybatis 还能有痛点?用着多方便!
对于小项目而言,用着确实还行。但是遇到大项目,光 Dao 接口都有几百个,如果还要手动定义一堆增删改查方法和 sql 语句,那也很浪费时间。
那有没有这样一个框架:
1.封装了 Mybatis,自带 CRUD 方法,我们不需要自己定义 CRUD 方法。
2.提供各种查询方法,不需要在 mapper 文件中写一些基础的 sql 语句。
3.封装了分页功能,让分页查询无比丝滑。
有的,MybatisPlus 闪亮登场。
依赖:
- <!-- mybatis-plus 依赖-->
- <dependency>
- <groupId>com.baomidou</groupId>
- <artifactId>mybatis-plus-boot-starter</artifactId>
- <version>3.4.0</version>
- </dependency>
-----------------------------------------------------------------------------------------------------------
MybatisPlus常用API-增删改查
Get
- // 根据 ID 查询
- T getById(Serializable id);
- // 根据 Wrapper,查询一条记录。结果集,如果是多个会抛出异常,随机取一条加上限制条件 wrapper.last("LIMIT 1")
- T getOne(Wrapper<T> queryWrapper);
- // 根据 Wrapper,查询一条记录
- T getOne(Wrapper<T> queryWrapper, boolean throwEx);
- // 根据 Wrapper,查询一条记录
- Map<String, Object> getMap(Wrapper<T> queryWrapper);
- // 根据 Wrapper,查询一条记录
- <V> V getObj(Wrapper<T> queryWrapper, Function<? super Object, V> mapper);
Save
- // 插入一条记录(选择字段,策略插入)
- boolean save(T entity);
- // 插入(批量)
- boolean saveBatch(Collection<T> entityList);
- // 插入(批量)
- boolean saveBatch(Collection<T> entityList, int batchSize);
SaveOrUpdate
- // TableId 注解存在更新记录,否插入一条记录
- boolean saveOrUpdate(T entity);
- // 根据updateWrapper尝试更新,否继续执行saveOrUpdate(T)方法
- boolean saveOrUpdate(T entity, Wrapper<T> updateWrapper);
- // 批量修改插入
- boolean saveOrUpdateBatch(Collection<T> entityList);
- // 批量修改插入
- boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize);
Remove
- // 根据 entity 条件,删除记录
- boolean remove(Wrapper<T> queryWrapper);
- // 根据 ID 删除
- boolean removeById(Serializable id);
- // 根据 columnMap 条件,删除记录
- boolean removeByMap(Map<String, Object> columnMap);
- // 删除(根据ID 批量删除)
- boolean removeByIds(Collection<? extends Serializable> idList);
Update
- // 根据 UpdateWrapper 条件,更新记录 需要设置
- boolean update(Wrapper<T> updateWrapper);
- // 根据 whereWrapper 条件,更新记录
- boolean update(T updateEntity, Wrapper<T> whereWrapper);
- // 根据 ID 选择修改
- boolean updateById(T entity);
- // 根据ID 批量更新
- boolean updateBatchById(Collection<T> entityList);
- // 根据ID 批量更新
- boolean updateBatchById(Collection<T> entityList, int batchSize);
List
- // 查询所有
- List<T> list();
- // 查询列表
- List<T> list(Wrapper<T> queryWrapper);
- // 查询(根据ID 批量查询)
- Collection<T> listByIds(Collection<? extends Serializable> idList);
- // 查询(根据 columnMap 条件)
- Collection<T> listByMap(Map<String, Object> columnMap);
- // 查询所有列表
- List<Map<String, Object>> listMaps();
- // 查询列表
- List<Map<String, Object>> listMaps(Wrapper<T> queryWrapper);
- // 查询全部记录
- List<Object> listObjs();
- // 查询全部记录
- <V> List<V> listObjs(Function<? super Object, V> mapper);
- // 根据 Wrapper 条件,查询全部记录
- List<Object> listObjs(Wrapper<T> queryWrapper);
- // 根据 Wrapper 条件,查询全部记录
- <V> List<V> listObjs(Wrapper<T> queryWrapper, Function<? super Object, V> mapper);
Page
- // 无条件分页查询
- IPage<T> page(IPage<T> page);
- // 条件分页查询
- IPage<T> page(IPage<T> page, Wrapper<T> queryWrapper);
- // 无条件分页查询
- IPage<Map<String, Object>> pageMaps(IPage<T> page);
- // 条件分页查询
- IPage<Map<String, Object>> pageMaps(IPage<T> page, Wrapper<T> queryWrapper);
Count
- // 查询总记录数
- int count();
- // 根据 Wrapper 条件,查询总记录数
- int count(Wrapper<T> queryWrapper);
query
- // 链式查询 普通
- QueryChainWrapper<T> query();
- // 链式查询 lambda 式。注意:不支持 Kotlin
- LambdaQueryChainWrapper<T> lambdaQuery();
-
- // 示例:
- query().eq("column", value).one();
- lambdaQuery().eq(Entity::getId, value).list();
update
- // 链式更改 普通
- UpdateChainWrapper<T> update();
-
- // 链式更改 lambda 式。注意:不支持 Kotlin
- LambdaUpdateChainWrapper<T> lambdaUpdate();
-
- // 示例:
- update().eq("column", value).remove();
- lambdaUpdate().eq(Entity::getId, value).update(entity);
Delete
- // 根据 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);
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);