基础增删改查的通用接口
程序员文章站
2022-06-12 18:14:12
...
通用接口
package com.yatsenglobal.core.support;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.RowBounds;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
public interface IService<T> {
/**
* 根据实体中的属性值进行查询, 查询条件使用等号 @param record the record
*
* @param record the record
*
* @return the list
*/
List<T> select(T record);
/**
* 根据主键字段进行查询, 方法参数必须包含完整的主键属性, 查询条件使用等号 @param key the key
*
* @param key the key
*
* @return the t
*/
T selectByKey(Object key);
/**
* 查询全部结果, select(null)方法能达到同样的效果 @return the list
*
* @return the list
*/
List<T> selectAll();
/**
* 根据实体中的属性进行查询, 只能有一个返回值, 有多个结果是抛出异常, 查询条件使用等号 @param record the record
*
* @param record the record
*
* @return the t
*/
T selectOne(T record);
/**
* 根据实体中的属性查询总数, 查询条件使用等号 @param record the record
*
* @param record the record
*
* @return the int
*/
int selectCount(T record);
/**
* 保存一个实体, null的属性不会保存, 会使用数据库默认值 @param record the record
*
* @param record the record
*
* @return the int
*/
int save(T record);
/**
* 批量保存 @param list the list
*
* @param list the list
*
* @return the int
*/
@Transactional(rollbackFor = Exception.class)
int batchSave(List<T> list);
/**
* 根据主键更新属性不为null的值 @param entity the entity
*
* @param entity the entity
*
* @return the int
*/
int update(T entity);
/**
* 根据实体属性作为条件进行删除, 查询条件使用等号 @param record the record
*
* @param record the record
*
* @return the int
*/
int delete(T record);
/**
* 批量删除 @param list the list
*
* @param list the list
*
* @return the int
*/
@Transactional(rollbackFor = Exception.class)
int batchDelete(List<T> list);
/**
* 根据主键字段进行删除, 方法参数必须包含完整的主键属性 @param key the key
*
* @param key the key
*
* @return the int
*/
int deleteByKey(Object key);
/**
* 这个查询支持通过Example类指定查询列, 通过selectProperties方法指定查询列 @param example the example
*
* @param example the example
*
* @return the list
*/
List<T> selectByExample(Object example);
/**
* 根据Example条件进行查询总数 @param example the example
*
* @param example the example
*
* @return the int
*/
int selectCountByExample(Object example);
/**
* 根据Example条件更新实体record包含的不是null的属性值 @param record the record
*
* @param record the record
* @param example the example
*
* @return the int
*/
int updateByExample(@Param("record") T record, @Param("example") Object example);
/**
* 根据Example条件删除数据 @param example the example
*
* @param example the example
*
* @return the int
*/
int deleteByExample(Object example);
/**
* 根据实体属性和RowBounds进行分页查询 @param record the record
*
* @param record the record
* @param rowBounds the row bounds
*
* @return the list
*/
List<T> selectByRowBounds(T record, RowBounds rowBounds);
/**
* 根据example条件和RowBounds进行分页查询 @param example the example
*
* @param example the example
* @param rowBounds the row bounds
*
* @return the list
*/
List<T> selectByExampleAndRowBounds(Object example, RowBounds rowBounds);
}
通用接口实现类
package com.yatsenglobal.core.support;
import com.yatsenglobal.base.exception.BusinessException;
import com.yatsenglobal.core.generator.IncrementIdGenerator;
import com.yatsenglobal.core.generator.UniqueIdGenerator;
import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.common.Mapper;
import java.util.List;
public abstract class BaseService<T> implements IService<T> {
/**
* The Logger.
*/
protected final Logger logger = LoggerFactory.getLogger(this.getClass());
/**
* The Mapper.
*/
@Autowired
protected Mapper<T> mapper;
/**
* Gets mapper.
*
* @return the mapper
*/
public Mapper<T> getMapper() {
return mapper;
}
/**
* Select list.
*
* @param record the record
*
* @return the list
*/
@Override
public List<T> select(T record) {
return mapper.select(record);
}
/**
* Select by key t.
*
* @param key the key
*
* @return the t
*/
@Override
public T selectByKey(Object key) {
return mapper.selectByPrimaryKey(key);
}
/**
* Select all list.
*
* @return the list
*/
@Override
public List<T> selectAll() {
return mapper.selectAll();
}
/**
* Select one t.
*
* @param record the record
*
* @return the t
*/
@Override
public T selectOne(T record) {
return mapper.selectOne(record);
}
/**
* Select count int.
*
* @param record the record
*
* @return the int
*/
@Override
public int selectCount(T record) {
return mapper.selectCount(record);
}
/**
* Select by example list.
*
* @param example the example
*
* @return the list
*/
@Override
public List<T> selectByExample(Object example) {
return mapper.selectByExample(example);
}
/**
* Save int.
*
* @param record the record
*
* @return the int
*/
@Override
public int save(T record) {
return mapper.insertSelective(record);
}
/**
* Batch save int.
*
* @param list the list
*
* @return the int
*/
@Override
public int batchSave(List<T> list) {
int result = 0;
for (T record : list) {
int count = mapper.insertSelective(record);
result += count;
}
return result;
}
/**
* Update int.
*
* @param entity the entity
*
* @return the int
*/
@Override
public int update(T entity) {
return mapper.updateByPrimaryKeySelective(entity);
}
/**
* Delete int.
*
* @param record the record
*
* @return the int
*/
@Override
public int delete(T record) {
return mapper.delete(record);
}
/**
* Delete by key int.
*
* @param key the key
*
* @return the int
*/
@Override
public int deleteByKey(Object key) {
return mapper.deleteByPrimaryKey(key);
}
/**
* Batch delete int.
*
* @param list the list
*
* @return the int
*/
@Override
public int batchDelete(List<T> list) {
int result = 0;
for (T record : list) {
int count = mapper.delete(record);
if (count < 1) {
logger.error("删除数据失败");
throw new BusinessException("删除数据失败!");
}
result += count;
}
return result;
}
/**
* Select count by example int.
*
* @param example the example
*
* @return the int
*/
@Override
public int selectCountByExample(Object example) {
return mapper.selectCountByExample(example);
}
/**
* Update by example int.
*
* @param record the record
* @param example the example
*
* @return the int
*/
@Override
public int updateByExample(T record, Object example) {
return mapper.updateByExampleSelective(record, example);
}
/**
* Delete by example int.
*
* @param example the example
*
* @return the int
*/
@Override
public int deleteByExample(Object example) {
return mapper.deleteByPrimaryKey(example);
}
/**
* Select by row bounds list.
*
* @param record the record
* @param rowBounds the row bounds
*
* @return the list
*/
@Override
public List<T> selectByRowBounds(T record, RowBounds rowBounds) {
return mapper.selectByRowBounds(record, rowBounds);
}
/**
* Select by example and row bounds list.
*
* @param example the example
* @param rowBounds the row bounds
*
* @return the list
*/
@Override
public List<T> selectByExampleAndRowBounds(Object example, RowBounds rowBounds) {
return mapper.selectByExampleAndRowBounds(example, rowBounds);
}
protected long generateId() {
return UniqueIdGenerator.getInstance(IncrementIdGenerator.getServiceId()).nextId();
}
}
转载于:https://www.jianshu.com/p/2cf6ca2d17a9
上一篇: dwr开发流程(一)
下一篇: 正则表达式中的“”转义的有关问题