1. MybatisPlus的CRUD接口
MybatisPlus提供了很多CRUD接口,可以直接使用这些接口来操作数据库。而不用像Mybatis那样写大量的XML文件及SQL语句。
Mapper CRUD接口
主要关键是继承BaseMapper<T>,其中T是实体类。
使用案例
Mapper层继承BaseMapper接口
@Mapper
public interface StudentMapper extends BaseMapper<StudentPO> {}
测试使用
@SpringBootTest
@Slf4j
public class TestController {@Autowiredprivate StudentMapper studentMapper;@Testpublic void testQueryStudentList(){List<StudentPO> studentList = studentMapper.selectList(null);log.info("studentList========>"+studentList);}
}
BaseMapper接口中定义了足够我们CRUD的操作方法: Insert、Delete、Update、Select。
1.1 Insert
// 插入一条记录
int insert(T entity);
案例
@SpringBootTest
public class TestController {@Autowiredprivate StudentMapper studentMapper;@Testpublic void testInsert(){studentMapper.insert(new StudentPO(null,"ls",20,"", SexEnum.WOMAN,1,null));}
}
1.2 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);
案例
@Test
public void testDelete(){// 根据 entity 条件,删除记录Wrapper<StudentPO> wrapper = new QueryWrapper<StudentPO>().eq("name", "ls");studentMapper.delete(wrapper);// 删除(根据ID 批量删除)List<String> ids = new ArrayList<>();ids.add("1");ids.add("2");studentMapper.deleteBatchIds(ids);// 根据 ID 删除studentMapper.deleteById("1746460971291541505");// 根据 columnMap 条件,删除记录Map<String, Object> map = new HashMap<>();map.put("name","Tom");map.put("id",3);studentMapper.deleteByMap(map);
}
1.3 Update
// 根据 whereWrapper 条件,更新记录
int update(@Param(Constants.ENTITY) T updateEntity, @Param(Constants.WRAPPER) Wrapper<T> whereWrapper);
// 根据 ID 修改
int updateById(@Param(Constants.ENTITY) T entity);
案例
@Test
public void testUpdate(){StudentPO studentPO = new StudentPO();studentPO.setId(3L);studentPO.setName("sss");studentMapper.updateById(studentPO);studentMapper.update(studentPO,new QueryWrapper<StudentPO>().eq("id",4));
}
1.4 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);
2. Service CRUD 接口
Service CRUD 封装IService (opens new window ) 接口,进一步封装 CRUD 采用 get 查询单行 remove 删除 list 查询集合 page分页 前缀命名方式区分 Mapper 层避免混淆
使用案例
新增StuentService接口及实现类
接口继承 IService<T>, 泛型为 StudentPO
public interface StudentService extends IService<StudentPO> {
}
实现类继承ServiceImpl,并指定Mapper和实体类。
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, StudentPO> implements StudentService {}
测试使用
注入StudentServiceImpl,并调用Service CRUD 接口中的方法,用法和Mapper CRUD 接口中差不多。
@SpringBootTest
@Slf4j
public class TestController {@Autowiredprivate StudentServiceImpl studentService;@Testpublic void testQuery(){List<StudentPO> list = studentService.list();log.info("studentList========>"+list);}
}
2.1 Save
// 插入一条记录(选择字段,策略插入)
boolean save(T entity);
// 插入(批量)
boolean saveBatch(Collection<T> entityList);
// 插入(批量)
boolean saveBatch(Collection<T> entityList, int batchSize);
2.2 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);
2.3 Remove
// 根据 queryWrapper 设置的条件,删除记录
boolean remove(Wrapper<T> queryWrapper);
// 根据 ID 删除
boolean removeById(Serializable id);
// 根据 columnMap 条件,删除记录
boolean removeByMap(Map<String, Object> columnMap);
// 删除(根据ID 批量删除)
boolean removeByIds(Collection<? extends Serializable> idList);
2.4 Update
// 根据 UpdateWrapper 条件,更新记录 需要设置sqlset
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);
2.5 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);
2.6 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);
2.7 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);
2.8 Count
// 查询总记录数
int count();
// 根据 Wrapper 条件,查询总记录数
int count(Wrapper<T> queryWrapper);
2.9 Chain
Chain为链式封装
query (查询)
// 链式查询 普通
QueryChainWrapper<T> query();
// 链式查询 lambda 式。注意:不支持 Kotlin
LambdaQueryChainWrapper<T> lambdaQuery();
使用案例
以下四个查询效果相等,只是写法不同。
@Test
public void testQuery2(){List<StudentPO> list1 = studentService.list(new QueryWrapper<StudentPO>().eq("name", "ls").eq("age", 20));log.info("studentList========>"+list1);List<StudentPO> list2 = studentService.list(new LambdaQueryWrapper<StudentPO>().eq(StudentPO::getName, "ls").eq(StudentPO::getAge, 20));log.info("studentList========>"+list2);List<StudentPO> list3 = new QueryChainWrapper<>(studentMapper).eq("name", "ls").eq("age", 20).list();log.info("studentList========>"+list3);List<StudentPO> list4 = new LambdaQueryChainWrapper<>(studentMapper).eq(StudentPO::getName, "ls").eq(StudentPO::getAge, 20).list();log.info("studentList========>"+list4);
}
update(更新)
// 链式更改 普通
UpdateChainWrapper<T> update();
// 链式更改 lambda 式。注意:不支持 Kotlin
LambdaUpdateChainWrapper<T> lambdaUpdate();
使用案例
以下四个更新效果相等,只是写法不同。
@Test
public void testUpdate2(){StudentPO studentPO = new StudentPO();studentPO.setId(3L);studentPO.setName("sss");studentService.update(studentPO,new QueryWrapper<StudentPO>().eq("name", "ls").eq("age", 20));studentService.update(studentPO,new LambdaQueryWrapper<StudentPO>().eq(StudentPO::getName, "ls").eq(StudentPO::getAge, 20));new UpdateChainWrapper<>(studentMapper).eq("name", "ls").eq("age", 20).update(studentPO);new LambdaUpdateChainWrapper<>(studentMapper).eq(StudentPO::getName, "ls").eq(StudentPO::getAge, 20).update(studentPO);
}
3. ActiveRecord 模式
- 实体类只需继承 Model 类即可进行强大的CRUD 操作
- 需要项目中已注入对应实体的BaseMapper
使用案例
继承Model类
@Data
@TableName(value = "student",excludeProperty = {"age"})
public class StudentPO extends Model<StudentPO> {@OrderBy(sort = 1)@TableId(value = "id",type = IdType.ASSIGN_ID)private Long id;@OrderBy(asc = true,sort = 2)@TableField(value = "name",condition = SqlCondition.LIKE)private String name;private Integer age;@TableField(exist = false)private String email;private SexEnum sex;@Versionprivate Integer version;@TableField("is_delete")@TableLogic(value = "0",delval = "1")private Integer isDelete = 0;
}
调用CRUD方法
@Test
public void testActiveRecord(){StudentPO studentPO = new StudentPO(null,"ls",20,"", SexEnum.WOMAN,1,null);studentPO.insert();studentPO.insert();studentPO.selectAll();studentPO.updateById();studentPO.deleteById();
}
4. SImpleQuery 工具类
- 对selectList查询后的结果用Stream流进行了一些封装,使其可以返回一些指定结果,简洁了api的调用
- 需要项目中已注入对应实体的BaseMapper
4.1 keyMap
// 查询表内记录,封装返回为Map<属性,实体>
Map<A, E> keyMap(LambdaQueryWrapper<E> wrapper, SFunction<E, A> sFunction, Consumer<E>... peeks);
// 查询表内记录,封装返回为Map<属性,实体>,考虑了并行流的情况
Map<A, E> keyMap(LambdaQueryWrapper<E> wrapper, SFunction<E, A> sFunction, boolean isParallel, Consumer<E>... peeks);
使用案例
@Test
public void testQuery3(){Map<String, StudentPO> map = SimpleQuery.keyMap(new LambdaQueryWrapper<StudentPO>().in(StudentPO::getName,"Tom","Sandy"),StudentPO::getName);log.info("map========>"+map);
}/*** 输出结果:map========>* {* Tom=StudentPO(id=3, name=Tom, age=null, email=null, sex=null, version=null, isDelete=0), * Sandy=StudentPO(id=4, name=Sandy, age=null, email=null, sex=null, version=null, isDelete=0)* }*/
4.2 map
// 查询表内记录,封装返回为Map<属性,属性>
Map<A, P> map(LambdaQueryWrapper<E> wrapper, SFunction<E, A> keyFunc, SFunction<E, P> valueFunc, Consumer<E>... peeks);
// 查询表内记录,封装返回为Map<属性,属性>,考虑了并行流的情况
Map<A, P> map(LambdaQueryWrapper<E> wrapper, SFunction<E, A> keyFunc, SFunction<E, P> valueFunc, boolean isParallel, Consumer<E>... peeks);
使用案例
@Test
public void testQuery4(){Map<Long, String> map = SimpleQuery.map(new LambdaQueryWrapper<StudentPO>().in(StudentPO::getName,"Tom","Sandy"),StudentPO::getId,StudentPO::getName);log.info("map========>"+map);
}
// 输出结果:map========>{3=Tom, 4=Sandy}
4.3 group
// 查询表内记录,封装返回为Map<属性,List<实体>>
Map<K, List<T>> group(LambdaQueryWrapper<T> wrapper, SFunction<T, A> sFunction, Consumer<T>... peeks);
// 查询表内记录,封装返回为Map<属性,List<实体>>,考虑了并行流的情况
Map<K, List<T>> group(LambdaQueryWrapper<T> wrapper, SFunction<T, K> sFunction, boolean isParallel, Consumer<T>... peeks);
// 查询表内记录,封装返回为Map<属性,分组后对集合进行的下游收集器>
M group(LambdaQueryWrapper<T> wrapper, SFunction<T, K> sFunction, Collector<? super T, A, D> downstream, Consumer<T>... peeks);
// 查询表内记录,封装返回为Map<属性,分组后对集合进行的下游收集器>,考虑了并行流的情况
M group(LambdaQueryWrapper<T> wrapper, SFunction<T, K> sFunction, Collector<? super T, A, D> downstream, boolean isParallel, Consumer<T>... peeks);
使用案例
@Test
public void testQuery5(){Map<Long, List<StudentPO>> map = SimpleQuery.group(new LambdaQueryWrapper<StudentPO>().in(StudentPO::getName,"Tom","Sandy"),StudentPO::getId);log.info("map========>"+map);
}
/*** 输出结果:map========>* {* 3=[StudentPO(id=3, name=Tom, age=null, email=null, sex=null, version=null, isDelete=0)], * 4=[StudentPO(id=4, name=Sandy, age=null, email=null, sex=null, version=null, isDelete=0)]* }*/
4.4 list
// 查询表内记录,封装返回为List<属性>
List<A> list(LambdaQueryWrapper<E> wrapper, SFunction<E, A> sFunction, Consumer<E>... peeks);
// 查询表内记录,封装返回为List<属性>,考虑了并行流的情况
List<A> list(LambdaQueryWrapper<E> wrapper, SFunction<E, A> sFunction, boolean isParallel, Consumer<E>... peeks);
使用案例
@Test
public void testQuery6(){List<SexEnum> list = SimpleQuery.list(new LambdaQueryWrapper<StudentPO>(),StudentPO::getSex);log.info("list========>"+list);
}// 输出结果:list========>[WOMAN, WOMAN, null, null, null, null, WOMAN, MAN]@Test
public void testQuery7(){List<SexEnum> list = SimpleQuery.list(new LambdaQueryWrapper<StudentPO>(), StudentPO::getSex, new Consumer<StudentPO>() {@Overridepublic void accept(StudentPO studentPO) {// 如果性别为空,则设置为MANstudentPO.setSex(Optional.ofNullable(studentPO.getSex()).orElse(SexEnum.MAN));}});log.info("list========>"+list);
}// 输出结果:list========>[WOMAN, WOMAN, MAN, MAN, MAN, MAN, WOMAN, MAN]
5. Db类
- 使用静态调用的方式,执行CRUD方法,避免Spring环境下Service循环注入,简洁代码,提升效率
- 需要项目中已注入对应实体的BaseMapper
Db类所有静态方法
public static <T> boolean save(T entity);public static <T> boolean saveBatch(Collection<T> entityList);public static <T> boolean saveBatch(Collection<T> entityList, int batchSize);public static <T> boolean saveOrUpdateBatch(Collection<T> entityList);public static <T> boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize);public static <T> boolean removeById(Serializable id, Class<T> entityClass);public static <T> boolean removeById(T entity);public static <T> boolean remove(AbstractWrapper<T, ?, ?> queryWrapper);public static <T> boolean updateById(T entity);public static <T> boolean update(AbstractWrapper<T, ?, ?> updateWrapper);public static <T> boolean update(T entity, AbstractWrapper<T, ?, ?> updateWrapper);public static <T> boolean updateBatchById(Collection<T> entityList);public static <T> boolean updateBatchById(Collection<T> entityList, int batchSize);public static <T> boolean removeByIds(Collection<? extends Serializable> list, Class<T> entityClass);public static <T> boolean removeByMap(Map<String, Object> columnMap, Class<T> entityClass);public static <T> boolean saveOrUpdate(T entity);public static <T> T getById(Serializable id, Class<T> entityClass);public static <T> T getOne(AbstractWrapper<T, ?, ?> queryWrapper);public static <T> T getOne(T entity);public static <T> T getOne(T entity, boolean throwEx);public static <T> T getOne(AbstractWrapper<T, ?, ?> queryWrapper, boolean throwEx);public static <T> List<T> listByMap(Map<String, Object> columnMap, Class<T> entityClass);public static <T> List<T> listByIds(Collection<? extends Serializable> idList, Class<T> entityClass);public static <T> Map<String, Object> getMap(AbstractWrapper<T, ?, ?> queryWrapper);public static <T> Map<String, Object> getMap(T entity);public static <T> long count(Class<T> entityClass);public static <T> long count(T entity);public static <T> long count(AbstractWrapper<T, ?, ?> queryWrapper);public static <T> List<T> list(AbstractWrapper<T, ?, ?> queryWrapper);public static <T> List<T> list(IPage<T> page, AbstractWrapper<T, ?, ?> queryWrapper);public static <T> List<T> list(Class<T> entityClass);public static <T> List<T> list(IPage<T> page, Class<T> entityClass);public static <T> List<T> list(T entity);public static <T> List<T> list(IPage<T> page, T entity);
public static <T> List<Map<String, Object>> listMaps(AbstractWrapper<T, ?, ?> queryWrapper);public static <T> List<Map<String, Object>> listMaps(IPage<? extends Map<String, Object>> page, AbstractWrapper<T, ?, ?> queryWrapper);public static <T> List<Map<String, Object>> listMaps(Class<T> entityClass);public static <T> List<Map<String, Object>> listMaps(IPage<? extends Map<String, Object>> page, Class<T> entityClass);public static <T> List<Map<String, Object>> listMaps(T entity);
public static <T> List<Map<String, Object>> listMaps(IPage<? extends Map<String, Object>> page, T entity);public static <T> List<T> listObjs(Class<T> entityClass);public static <E, T> List<E> listObjs(AbstractWrapper<T, ?, ?> queryWrapper);public static <T, V> List<V> listObjs(AbstractWrapper<T, ?, ?> queryWrapper, SFunction<? super T, V> mapper);public static <T, V> List<V> listObjs(Class<T> entityClass, SFunction<? super T, V> mapper);public static <T, E extends IPage<Map<String, Object>>> E pageMaps(E page, Class<T> entityClass);public static <T, E extends IPage<Map<String, Object>>> E pageMaps(E page, AbstractWrapper<T, ?, ?> queryWrapper);public static <T> IPage<T> page(IPage<T> page, Class<T> entityClass);public static <T> IPage<T> page(IPage<T> page, AbstractWrapper<T, ?, ?> queryWrapper);public static <T> QueryChainWrapper<T> query(Class<T> entityClass);public static <T> KtQueryChainWrapper<T> ktQuery(Class<T> entityClass);public static <T> LambdaQueryChainWrapper<T> lambdaQuery(Class<T> entityClass);public static <T> UpdateChainWrapper<T> update(Class<T> entityClass);public static <T> KtUpdateChainWrapper<T> ktUpdate(Class<T> entityClass);public static <T> LambdaUpdateChainWrapper<T> lambdaUpdate(Class<T> entityClass);public static <T> boolean saveOrUpdate(T entity, AbstractWrapper<T, ?, ?> updateWrapper);public static <T, V> V getObj(AbstractWrapper<T, ?, ?> queryWrapper, SFunction<? super T, V> mapper);protected static <T> Class<T> getEntityClass(Collection<T> entityList);protected static <T> Class<T> getEntityClass(AbstractWrapper<T, ?, ?> queryWrapper);protected static <T> Class<T> getEntityClass(T entity);protected static <T> TableInfo getTableInfo(Class<T> entityClass);
使用案例
@Test
public void testQuery8(){List<StudentPO> list = Db.list(Wrappers.lambdaQuery(StudentPO.class).in(StudentPO::getName,"Tom","Sandy"));log.info("list========>"+list);StudentPO byId = Db.getById(1, StudentPO.class);log.info("byId========>"+byId);
}