大类 | 作用 |
---|---|
save | 插入 |
saveorupdate | 有则改无则插入 |
remove | 删除 |
update | 修改 |
get | 返回一条记录 |
list | 返回多条记录 |
page | 用于分页查询 |
count | 返回记录总数 |
query | 查询 |
lambda | 内部支持lambda写法 |
Service
通用 Service CRUD 封装IService (opens new window)接口,进一步封装 CRUD 采用 get 查询单行 remove 删除 list 查询集合 page 分页 前缀命名方式区分 Mapper 层避免混淆
Service CRUD接口官方文档
在controller层随便调用一个IService的方法就可以进入源码查看方法
Save
// 插入一条记录(选择字段,策略插入)
boolean save(T entity);
// 插入(批量)
boolean saveBatch(Collection<T> entityList);
// 插入(批量),指定批次数量
boolean saveBatch(Collection<T> entityList, int batchSize);
参数说明
类型 | 参数名 | 描述 |
---|---|---|
T | entity | 实体对象 |
Collection | entityList | 实体对象集合 |
int | batchSize | 插入批次数量(默认1000) |
案例
Device device = new Device();
device.setWorkshops("车间4");
device.setDeviceType("生产设备");
device.setDeviceNumber("1001");
device.setDeviceName("砖头");
device.setDeviceModel("ST001");
device.setFactory("工厂");
iDeviceService.save(device);
List<Device> list = new ArrayList<>();
for (Long i = 1l; i < 5; i++) {
Device device = new Device();
device.setWorkshops("车间"+i);
device.setDeviceType("生产设备");
device.setDeviceNumber("1001111"+i);
device.setDeviceName("砖头"+i);
device.setDeviceModel("ST0000"+i);
device.setFactory("工厂");
list.add(device);
}
iDeviceService.saveBatch(list);
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);
参数说明
类型 | 参数名 | 描述 |
---|---|---|
Wrapper | updateWrapper | 实体对象封装操作类 UpdateWrapper |
T | entity | 实体对象 |
Collection | entityList | 实体对象集合 |
int | batchSize | 插入批次数量(默认1000) |
案例
List<Device> list = new ArrayList<>();
Device device = new Device();
device.setWorkshops("车间5");
device.setDeviceType("生产设备");
device.setDeviceNumber("10011115");
device.setDeviceName("砖头");
device.setDeviceModel("ST00005");
device.setFactory("工厂");
list.add(device);
iDeviceService.saveOrUpdate(device);
List<Device> list = new ArrayList<>();
Device device1 = new Device();
device1.setWorkshops("车间1");
device1.setDeviceType("生产设备");
device1.setDeviceNumber("10010111");
device1.setDeviceName("砖头");
device1.setDeviceModel("ST00101");
device1.setFactory("工厂");
list.add(device1);
Device device2 = new Device();
device2.setWorkshops("车间1");
device2.setDeviceType("生产设备");
device2.setDeviceNumber("10010131");
device2.setDeviceName("砖头");
device2.setDeviceModel("ST00102");
device2.setFactory("工厂");
list.add(device2);
iDeviceService.saveOrUpdateBatch(list);
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);
参数说明
类型 | 参数名 | 描述 |
---|---|---|
Wrapper | queryWrapper | 实体包装类 QueryWrapper |
Serializable | id | 主键 ID |
Map<String, Object> | columnMap | 表字段 map 对象 |
Collection<? extends Serializable> | idList | 主键 ID 列表 |
Collection<? extends Serializable>:一种包含未知类型的Collection,必须是Serializable或其子类
案例
UpdateWrapper<Device> duw = new UpdateWrapper<>();
Map<String, Object> map = new HashMap<>();
map.put("deviceName","砖头");
duw.allEq(map);
iDeviceService.remove(duw);
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);
参数说明
类型 | 参数名 | 描述 |
---|---|---|
Wrapper | queryWrapper | 实体包装类 QueryWrapper |
T | entity | 实体对象 |
Collection | entityList | 实体对象集合 |
int | batchSize | 插入批次数量(默认1000) |
Get
用于返回一条记录
// 根据 ID 查询
T getById(Serializable id);
// 根据 Wrapper,查询一条记录。结果集,如果是多个会抛出异常,随机取一条加上限制条件 wrapper.last("LIMIT 1")
T getOne(Wrapper<T> queryWrapper);
// 根据 Wrapper,查询一条记录,自定义有多个结果是否需要抛异常,true抛出异常。false返回结果集中第一条记录
T getOne(Wrapper<T> queryWrapper, boolean throwEx);
// 根据 Wrapper,查询一条记录用map保存
Map<String, Object> getMap(Wrapper<T> queryWrapper);
// 根据 Wrapper,查询一条记录
<V> V getObj(Wrapper<T> queryWrapper, Function<? super Object, V> mapper);
参数说明
类型 | 参数名 | 描述 |
---|---|---|
Wrapper | queryWrapper | 实体对象封装操作类 QueryWrapper |
T | entity | 实体对象 |
Serializable | id | 主键 ID |
boolean | throwEx | 有多个 result 是否抛出异常 |
Function<? super Object, V> | mapper | 转换函数 |
List
用于返回多条记录
// 查询所有
List<T> list();
// 根据queryWrapper条件查询列表
List<T> list(Wrapper<T> queryWrapper);
// 查询(根据ID 批量查询)
Collection<T> listByIds(Collection<? extends Serializable> idList);
// 查询(根据 columnMap 条件)
Collection<T> listByMap(Map<String, Object> columnMap);
// 查询所有列表返回List<Map>集合
List<Map<String, Object>> listMaps();
// 根据操作对象传递的条件查询列表返回List<Map>集合
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);
参数说明
类型 | 参数名 | 描述 |
---|---|---|
Wrapper | queryWrapper | 实体对象封装操作类 QueryWrapper |
Collection<? extends Serializable> | idList | 主键 ID 列表 |
Function<? super Object, V> | mapper | 转换函数 |
Map<String, Object> | columnMap | 表字段 map 对象 |
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);
封装的数据类型不一样,但是都返回page对象
参数说明
类型 | 参数名 | 描述 |
---|---|---|
Wrapper | queryWrapper | 实体对象封装操作类 QueryWrapper |
IPage | page | 翻页对象 |
Count
用于返回记录总数
// 查询总记录数
int count();
// 根据 Wrapper 条件,查询总记录数
int count(Wrapper<T> queryWrapper);
参数说明
类型 | 参数名 | 描述 |
---|---|---|
Wrapper | queryWrapper | 实体对象封装操作类 QueryWrapper |
Chain链式方法,先条件拼接再调用特定的改查方法
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);