主要工作为课程功能的服务实现类的编写,实现了对分类对象的数据库操作,并通过接口方法暴露了这些操作给上层调用。
@Repository
@RequiredArgsConstructor
public class CourseDaoImpl extends AbstractBaseJdbc implements CourseDao {
@NotNull
private final CourseMapper mapper;
@Override
public int save(Course record) {
if (record.getId() == null) {
record.setId(IdWorker.getId());
}
return this.mapper.insertSelective(record);
}
@Override
public int deleteById(Long id) {
return this.mapper.deleteByPrimaryKey(id);
}
@Override
public int updateById(Course record) {
record.setGmtCreate(null);
record.setGmtModified(null);
return this.mapper.updateByPrimaryKeySelective(record);
}
@Override
public Course getById(Long id) {
return this.mapper.selectByPrimaryKey(id);
}
@Override
public Page<Course> page(int pageCurrent, int pageSize, CourseExample example) {
int count = this.mapper.countByExample(example);
pageSize = PageUtil.checkPageSize(pageSize);
pageCurrent = PageUtil.checkPageCurrent(count, pageSize, pageCurrent);
int totalPage = PageUtil.countTotalPage(count, pageSize);
example.setLimitStart(PageUtil.countOffset(pageCurrent, pageSize));
example.setPageSize(pageSize);
return new Page<>(count, totalPage, pageCurrent, pageSize, this.mapper.selectByExampleWithBLOBs(example));
}
@Override
public List<Course> listByExample(CourseExample example) {
return this.mapper.selectByExample(example);
}
@Override
public int countByExample(CourseExample example) {
return this.mapper.countByExample(example);
}
@Override
public List<Course> listByIds(List<Long> courseIds) {
if (CollUtil.isEmpty(courseIds)) {
return Collections.emptyList();
}
CourseExample example = new CourseExample();
example.createCriteria().andIdIn(courseIds);
example.setOrderByClause("field(id," + courseIds.stream().map(String::valueOf).collect(Collectors.joining(",")) + ")");
return this.mapper.selectByExample(example);
}
@Override
public void addCountBuy(int countBuy, Long id) {
String sql = "update course set count_buy=count_buy+? where id=?";
this.jdbcTemplate.update(sql, countBuy, id);
}
@Override
public void addCountStudy(int countStudy, Long id) {
String sql = "update course set count_study=count_study+? where id=?";
this.jdbcTemplate.update(sql, countStudy, id);
}
}
上述代码为课程信息的服务实现类代码编写,其他类逻辑同上,在上述代码中共进行了以下工作:
- 数据访问接口的实现:该接口定义了对分类对象进行数据库操作的方法,如插入、删除、更新、查询等。
- 依赖注入:使用了 @RequiredArgsConstructor 注解,通过构造函数注入了一个 CategoryMapper 对象。这个 CategoryMapper 是一个其他持久化框架生成的数据访问对象,用于执行具体的数据库操作。
- 数据库操作方法:实现了一系列的数据库操作方法,包括:
- save(Category record): 插入一条分类记录到数据库中。
- deleteById(Long id): 根据分类ID从数据库中删除一条分类记录。
- updateById(Category record): 根据分类ID更新一条分类记录的信息。
- getById(Long id): 根据分类ID从数据库中获取一条分类记录的信息。
- page(int pageCurrent, int pageSize, CategoryExample example): 分页查询符合条件的分类记录。
- listByExample(CategoryExample example): 根据条件查询分类记录列表。
- countByExample(CategoryExample example): 统计符合条件的分类记录数量。
- listByIds(List<Long> categoryIdList): 根据分类ID列表查询分类记录列表。
- 业务逻辑处理:在一些方法中进行了一些业务逻辑的处理。
还定义了对课程信息进行数据库操作的方法接口
/**
* 课程信息 服务类
*
*/
public interface CourseDao {
/**
* 保存课程信息
*
* @param record 课程信息
* @return 影响记录数
*/
int save(Course record);
/**
* 根据ID删除课程信息
*
* @param id 主键ID
* @return 影响记录数
*/
int deleteById(Long id);
/**
* 修改课程信息
*
* @param record 课程信息
* @return 影响记录数
*/
int updateById(Course record);
/**
* 根据ID获取课程信息
*
* @param id 主键ID
* @return 课程信息
*/
Course getById(Long id);
/**
* 课程信息--分页查询
*
* @param pageCurrent 当前页
* @param pageSize 分页大小
* @param example 查询条件
* @return 分页结果
*/
Page<Course> page(int pageCurrent, int pageSize, CourseExample example);
/**
* 课程信息--条件列出
*
* @param example 查询条件
* @return 课程信息列表
*/
List<Course> listByExample(CourseExample example);
/**
* 课程信息--条件统计
*
* @param example 统计条件
* @return 课程信息数量
*/
int countByExample(CourseExample example);
List<Course> listByIds(List<Long> courseIds);
void addCountBuy(int countBuy, Long id);
void addCountStudy(int countStudy, Long id);
}