1、创建一个JPA全局继承接口MyBaseRepository
/**
* 通用DAO接口
* 使用@NoRepositoryBean标注,Spring Data Jpa在启动时就不会去实例化BaseRepository这个接口
* 继承JpaRepository,JpaSpecificationExecutor
*/
@NoRepositoryBean
public interface MyBaseRepository<T, ID extends Serializable> extends JpaSpecificationExecutor<T>, JpaRepository<T, ID> {
}
2、自定义基础业务封装
/**
* 基础业务封装
*/
public interface BaseService<T,ID extends Serializable> {
/**
* 不含查询条件的分页查询
* @param pageNo
* @param pageSize
* @return org.springframework.data.domain.Page<T>
*/
Page<T> findAll(int pageNo, int pageSize) throws ItemsNotFoundException;
/**
* 获取所有数据
* @return java.util.List<T>
*/
List<T> findAll() throws ItemsNotFoundException;
/**
* 根据ID获取数据
* @param id
* @return T
*/
T findOne(ID id) throws ItemsNotFoundException;
/**
* 新增指定数据
* @param entity
* @throws Exception
*/
void save(T entity) throws ItemSaveException;
/**
* 批量保存数据
* @param entitys
* @return void
*/
void saveAll(Iterable<T> entitys) throws ItemSaveException;
/**
* 新增并立即保存到数据库
*
* @param entity
* @return void
*/
void saveAndFlush(T entity) throws ItemSaveException;
/**
* 更新指定数据
* @param entity
* @throws Exception
*/
void update(T entity) throws ItemSaveException;
/**
* 删除指定数据
* @param id
* @throws Exception
*/
void delete(ID id) throws ItemDeleteException;
/**
* 根据主键判断是否存在
* @param id
* @return
* @throws Exception
*/
boolean exists(ID id) throws ItemsNotFoundException;
/**
* 获取记录总数
* @return long
*/
long count();
}
3、自定义封装业务实现
/**
* 基础业务封装
*/
public abstract class BaseServiceImpl<T,ID extends Serializable> implements BaseService<T,ID> {
@Autowired
private MyBaseRepository<T,ID> myBaseRepository;
/**
* 不含查询条件的分页查询
* @param pageNo
* @param pageSize
* @return
*/
@Override
public Page<T> findAll(int pageNo, int pageSize) throws ItemsNotFoundException {
return myBaseRepository.findAll(PageRequest.of(pageNo,pageSize));
}
/**
* 获取所有数据
* @return
*/
@Override
public List<T> findAll() throws ItemsNotFoundException {
return myBaseRepository.findAll();
}
/**
* 根据ID获取数据
* @param id
* @return
* @throws Exception
*/
@Override
public T findOne(ID id) throws ItemsNotFoundException {
return myBaseRepository.findById(id).orElse(null);
}
/**
* 新增指定数据
* @param entity
* @throws Exception
*/
@Override
@Transactional(rollbackFor = ItemSaveException.class)
@Modifying
public void save(T entity) throws ItemSaveException {
myBaseRepository.save(entity);
}
/**
* 批量保存数据
* @param entitys
* @return void
*/
@Override
@Transactional(rollbackFor = ItemSaveException.class)
@Modifying
public void saveAll(Iterable<T> entitys) throws ItemSaveException {
myBaseRepository.saveAll(entitys);
}
/**
* 新增并立即保存到数据库
* @param entity
* @return void
*/
@Override
@Transactional(rollbackFor = ItemSaveException.class)
@Modifying
public void saveAndFlush(T entity) throws ItemSaveException {
myBaseRepository.saveAndFlush(entity);
}
/**
* 更新指定数据
* @param entity
* @throws ItemSaveException
*/
@Override
@Transactional(rollbackFor = ItemSaveException.class)
@Modifying
public void update(T entity) throws ItemSaveException {
myBaseRepository.saveAndFlush(entity);
}
/**
* 删除指定数据
* @param id
* @throws ItemDeleteException
*/
@Override
@Transactional(rollbackFor = ItemDeleteException.class)
@Modifying
public void delete(ID id) throws ItemDeleteException {
myBaseRepository.deleteById(id);
}
/**
* 根据主键判断是否存在
* @param id
* @return
* @throws ItemsNotFoundException
*/
@Override
public boolean exists(ID id) throws ItemsNotFoundException {
return myBaseRepository.existsById(id);
}
/**
* 获取记录总数
* @return long
*/
@Override
public long count() {
return myBaseRepository.count();
}
}
4、调用自定义接口
@Repository
public interface StudentDao extends MyBaseRepository<Student, String> {
/**
* 通过Id查询第一条记录
*/
Student findFirstById(String id);
}
这里StudentDao就可以调用我们的自定义全局JPA接口了。
1万+




被折叠的 条评论
为什么被折叠?



