Package io.mybatis.service
Class AbstractService<T,I extends Serializable,M extends BaseMapper<T,I>>
- java.lang.Object
-
- io.mybatis.service.AbstractService<T,I,M>
-
- Type Parameters:
T- 实体类类型I- 主键类型M- Mapper类型
- All Implemented Interfaces:
BaseService<T,I>,EntityService<T,I>,ExampleService<T,I>
public abstract class AbstractService<T,I extends Serializable,M extends BaseMapper<T,I>> extends Object implements BaseService<T,I>
基础方法实现,推荐自己的实现类继承该类例如自己的接口:
public interface UserService { User save(User user); }对应的实现类:public class UserServiceImpl extends AbstractService<User, Long, UserMapper> implements UserService { //由于 User save(User user); 和默认的 T save(T entity) 方法一致,所以不需要提供实现,可以用默认方法 }- Author:
- liuzh
-
-
Field Summary
Fields Modifier and Type Field Description protected MbaseMapper
-
Constructor Summary
Constructors Constructor Description AbstractService()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description longcount(Example<T> example)根据 example 查询总数longcount(T entity)以当前类作为条件查询总数intdelete(Example<T> example)根据 example 条件批量删除intdelete(T entity)根据当前类的值作为条件进行删除(注意:当所有字段都没有值时可能会清库)<F> intdeleteByFieldList(Fn<T,F> field, Collection<F> fieldValueList)根据指定字段集合删除intdeleteById(I id)根据主键进行删除List<T>findAll()查询全部<F> List<T>findByFieldList(Fn<T,F> field, Collection<F> fieldValueList)根据指定字段集合查询TfindById(I id)根据指定的主键查询List<T>findList(Example<T> example)根据 example 条件查询List<T>findList(T entity)以当前类作为条件查询TfindOne(Example<T> example)根据 example 条件查询一个,当结果多于1个时出错TfindOne(T entity)以当前类作为条件查询一个,当结果多于1个时出错booleanpkHasValue(T entity)主键是否有值Tsave(T entity)保存(所有字段)TsaveOrUpdate(T entity)保存或更新(全部字段),当主键不存在时保存,存在时更新TsaveOrUpdateSelective(T entity)保存或更新(非空字段),当主键不存在时保存,存在时更新TsaveSelective(T entity)保存(非空字段,空的字段会使用数据库设置的默认值,但是不会字段反写)voidsetBaseMapper(M baseMapper)Tupdate(T entity)更新(所有字段)intupdate(T entity, Example<T> example)根据 example 查询条件批量更新(所有字段)Tupdate(T entity, Fn<T,Object>... updateFields)更新(指定字段)TupdateSelective(T entity)更新(非空字段)intupdateSelective(T entity, Example<T> example)根据 example 查询条件批量更新(非空字段)TupdateSelective(T entity, Fn<T,Object>... forceUpdateFields)更新(非空字段),指定的强制更新字段不区分是否为空ExampleWrapper<T,I>wrapper()-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface io.mybatis.service.ExampleService
example
-
-
-
-
Field Detail
-
baseMapper
protected M extends BaseMapper<T,I> baseMapper
-
-
Method Detail
-
setBaseMapper
@Autowired public void setBaseMapper(M baseMapper)
-
wrapper
public ExampleWrapper<T,I> wrapper()
- Specified by:
wrapperin interfaceBaseService<T,I extends Serializable>
-
save
public T save(T entity)
Description copied from interface:EntityService保存(所有字段)- Specified by:
savein interfaceEntityService<T,I extends Serializable>- Parameters:
entity- 实体类- Returns:
- 返回保存成功后的实体,远程服务调用时,由于序列化和反序列化,入参和返回值不是同一个对象
-
saveSelective
public T saveSelective(T entity)
Description copied from interface:EntityService保存(非空字段,空的字段会使用数据库设置的默认值,但是不会字段反写)- Specified by:
saveSelectivein interfaceEntityService<T,I extends Serializable>- Parameters:
entity- 实体类- Returns:
- 返回保存成功后的实体,远程服务调用时,由于序列化和反序列化,入参和返回值不是同一个对象
-
update
public T update(T entity)
Description copied from interface:EntityService更新(所有字段)- Specified by:
updatein interfaceEntityService<T,I extends Serializable>- Parameters:
entity- 实体类- Returns:
- 返回更新成功后的实体,远程服务调用时,由于序列化和反序列化,入参和返回值不是同一个对象
-
update
public T update(T entity, Fn<T,Object>... updateFields)
Description copied from interface:EntityService更新(指定字段)- Specified by:
updatein interfaceEntityService<T,I extends Serializable>- Parameters:
entity- 实体类updateFields- 需要更新的字段- Returns:
- 返回更新成功后的实体,远程服务调用时,由于序列化和反序列化,入参和返回值不是同一个对象
-
updateSelective
public T updateSelective(T entity)
Description copied from interface:EntityService更新(非空字段)- Specified by:
updateSelectivein interfaceEntityService<T,I extends Serializable>- Parameters:
entity- 实体类- Returns:
- 返回更新成功后的实体,远程服务调用时,由于序列化和反序列化,入参和返回值不是同一个对象
-
updateSelective
public T updateSelective(T entity, Fn<T,Object>... forceUpdateFields)
Description copied from interface:EntityService更新(非空字段),指定的强制更新字段不区分是否为空- Specified by:
updateSelectivein interfaceEntityService<T,I extends Serializable>- Parameters:
entity- 实体类forceUpdateFields- 强制更新的字段,不区分字段是否为 null- Returns:
- 返回更新成功后的实体,远程服务调用时,由于序列化和反序列化,入参和返回值不是同一个对象
-
pkHasValue
public boolean pkHasValue(T entity)
Description copied from interface:EntityService主键是否有值- Specified by:
pkHasValuein interfaceEntityService<T,I extends Serializable>- Parameters:
entity- 实体类- Returns:
- true有值,false为空
-
saveOrUpdate
public T saveOrUpdate(T entity)
Description copied from interface:EntityService保存或更新(全部字段),当主键不存在时保存,存在时更新- Specified by:
saveOrUpdatein interfaceEntityService<T,I extends Serializable>- Parameters:
entity- 实体类- Returns:
- 返回保存或更新成功后的实体,远程服务调用时,由于序列化和反序列化,入参和返回值不是同一个对象
-
saveOrUpdateSelective
public T saveOrUpdateSelective(T entity)
Description copied from interface:EntityService保存或更新(非空字段),当主键不存在时保存,存在时更新- Specified by:
saveOrUpdateSelectivein interfaceEntityService<T,I extends Serializable>- Parameters:
entity- 实体类- Returns:
- 返回保存或更新成功后的实体,远程服务调用时,由于序列化和反序列化,入参和返回值不是同一个对象
-
delete
public int delete(T entity)
Description copied from interface:EntityService根据当前类的值作为条件进行删除(注意:当所有字段都没有值时可能会清库)- Specified by:
deletein interfaceEntityService<T,I extends Serializable>- Parameters:
entity- 实体类- Returns:
- 返回大于 1成功,0失败
-
deleteById
public int deleteById(I id)
Description copied from interface:EntityService根据主键进行删除- Specified by:
deleteByIdin interfaceEntityService<T,I extends Serializable>- Parameters:
id- 指定的主键- Returns:
- 返回 1成功,0失败抛出异常
-
deleteByFieldList
public <F> int deleteByFieldList(Fn<T,F> field, Collection<F> fieldValueList)
Description copied from interface:EntityService根据指定字段集合删除- Specified by:
deleteByFieldListin interfaceEntityService<T,I extends Serializable>- Type Parameters:
F- 字段值类型- Parameters:
field- 字段fieldValueList- 字段值集合- Returns:
- 删除数据的条数
-
findById
public T findById(I id)
根据指定的主键查询- Specified by:
findByIdin interfaceEntityService<T,I extends Serializable>- Parameters:
id- 主键- Returns:
- 实体
-
findOne
public T findOne(T entity)
以当前类作为条件查询一个,当结果多于1个时出错- Specified by:
findOnein interfaceEntityService<T,I extends Serializable>- Parameters:
entity- 实体类- Returns:
- 实体
-
findList
public List<T> findList(T entity)
以当前类作为条件查询- Specified by:
findListin interfaceEntityService<T,I extends Serializable>- Parameters:
entity- 实体类- Returns:
- 实体集合
-
findByFieldList
public <F> List<T> findByFieldList(Fn<T,F> field, Collection<F> fieldValueList)
根据指定字段集合查询- Specified by:
findByFieldListin interfaceEntityService<T,I extends Serializable>- Type Parameters:
F- 字段值类型- Parameters:
field- 字段fieldValueList- 字段值集合- Returns:
- 实体集合
-
findAll
public List<T> findAll()
查询全部- Specified by:
findAllin interfaceEntityService<T,I extends Serializable>- Returns:
- 实体集合
-
count
public long count(T entity)
以当前类作为条件查询总数- Specified by:
countin interfaceEntityService<T,I extends Serializable>- Parameters:
entity- 实体类- Returns:
- 实体集合
-
delete
public int delete(Example<T> example)
根据 example 条件批量删除- Specified by:
deletein interfaceExampleService<T,I extends Serializable>- Parameters:
example- 查询条件- Returns:
- 返回大于0成功,0失败
-
update
public int update(T entity, Example<T> example)
根据 example 查询条件批量更新(所有字段)- Specified by:
updatein interfaceExampleService<T,I extends Serializable>- Parameters:
entity- 实体类example- 查询条件- Returns:
- 返回大于0成功,0失败
-
updateSelective
public int updateSelective(T entity, Example<T> example)
根据 example 查询条件批量更新(非空字段)- Specified by:
updateSelectivein interfaceExampleService<T,I extends Serializable>- Parameters:
entity- 实体类example- 查询条件- Returns:
- 返回大于0成功,0失败
-
findOne
public T findOne(Example<T> example)
根据 example 条件查询一个,当结果多于1个时出错- Specified by:
findOnein interfaceExampleService<T,I extends Serializable>- Parameters:
example- 查询条件- Returns:
- 实体
-
findList
public List<T> findList(Example<T> example)
根据 example 条件查询- Specified by:
findListin interfaceExampleService<T,I extends Serializable>- Parameters:
example- 查询条件- Returns:
- 实体集合
-
count
public long count(Example<T> example)
根据 example 查询总数- Specified by:
countin interfaceExampleService<T,I extends Serializable>- Parameters:
example- 查询条件- Returns:
- 总数
-
-