项目实训:虚拟现实环境下的远程教育和智能评估系统(五)

主要工作为课程功能的服务实现类的编写,实现了对分类对象的数据库操作,并通过接口方法暴露了这些操作给上层调用。

@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列表查询分类记录列表。
  • 业务逻辑处理:在一些方法中进行了一些业务逻辑的处理。

 还定义了对课程信息进行数据库操作的方法接口


/**
 * 课程信息 服务类
 *
 * @author wujing
 * @date 2022-08-25
 */
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);
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值