通用 Service CRUD 提供了IService接口并提供了一个默认的实现类ServiceImpl, Service CRUD进一步封装了Mapper CRUD。Service提供了一定的命名规则持久化方法: get 查询单行 remove 删除 list 查询集合 page 分页
1. Save
boolean save(T entity
);
boolean saveBatch(Collection
<T> entityList
);
boolean saveBatch(Collection
<T> entityList
, int batchSize
);
2. SaveOrUpdate
boolean saveOrUpdate(T entity
);
boolean saveOrUpdate(T entity
, Wrapper
<T> updateWrapper
);
boolean saveOrUpdateBatch(Collection
<T> entityList
);
boolean saveOrUpdateBatch(Collection
<T> entityList
, int batchSize
);
3. Remove
boolean remove(Wrapper
<T> queryWrapper
);
boolean removeById(Serializable id
);
boolean removeByMap(Map
<String, Object> columnMap
);
boolean removeByIds(Collection
<? extends Serializable> idList
);
4. Update
boolean update(Wrapper
<T> updateWrapper
);
boolean update(T entity
, Wrapper
<T> updateWrapper
);
boolean updateById(T entity
);
boolean updateBatchById(Collection
<T> entityList
);
boolean updateBatchById(Collection
<T> entityList
, int batchSize
);
6. Get
T
getById(Serializable id
);
T
getOne(Wrapper
<T> queryWrapper
);
T
getOne(Wrapper
<T> queryWrapper
, boolean throwEx
);
Map
<String, Object> getMap(Wrapper
<T> queryWrapper
);
<V> V
getObj(Wrapper
<T> queryWrapper
, Function
<? super Object
, V
> mapper
);
7. List
List
<T> list();
List
<T> list(Wrapper
<T> queryWrapper
);
Collection
<T> listByIds(Collection
<? extends Serializable> idList
);
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
);
List
<Object> listObjs(Wrapper
<T> queryWrapper
);
<V> List
<V> listObjs(Wrapper
<T> queryWrapper
, Function
<? super Object
, V
> mapper
);
8. 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
);
9. Count
int count();
int count(Wrapper
<T> queryWrapper
);
10. Chain
QueryChainWrapper
<T> query();
LambdaQueryChainWrapper
<T> lambdaQuery();
List
<T> list();
T
one();
UpdateChainWrapper
<T> update();
LambdaUpdateChainWrapper
<T> lambdaUpdate();
boolean update();
boolean remove();
11. 使用通用Service
接口继承IService< T >, 实现类继承ServiceImpl
public interface UserService extends IService<User> {
}
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
}
@SpringBootTest
class SpringbootMybatisplusApplicationTests {
@Autowired
private UserService userService
;
@Test
void testMyBatisPlus() {
QueryWrapper
<User> queryWrapper
= new QueryWrapper<>();
queryWrapper
.eq("id", 1);
User user
= userService
.getOne(queryWrapper
);
List
<User> list
= userService
.lambdaQuery().gt(User
::getAge
, 30).list();
}
}