JiluService 类:记录管理服务实现

该代码段定义了一个 JiluService 类,包含了对 JiluMapper 的注入和对 Jilu 对象的增删改查等基本操作方法。

类结构:

package com.dian.service;

import com.dian.domain.Jilu;
import com.dian.mapper.JiluMapper;
import com.dian.util.DateUtil;
import com.dian.vo.Tongji;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.List;
import java.util.Map;

@Service
public class JiluService {

    @Resource
    private JiluMapper jiluMapper;

    // 删除记录
    public boolean deleteByPrimaryKey(Integer id) {
        return jiluMapper.deleteByPrimaryKey(id) == 1;
    }

    // 插入记录
    public boolean insert(Jilu entify) {
        return jiluMapper.insert(entify) == 1;
    }

    // 插入记录(选择性插入)
    public boolean insertSelective(Jilu entify) {
        return jiluMapper.insertSelective(entify) == 1;
    }

    // 根据主键查询记录
    public Jilu selectByPrimaryKey(Integer id) {
        return jiluMapper.selectByPrimaryKey(id);
    }

    // 更新记录(选择性更新)
    public boolean updateByPrimaryKeySelective(Jilu entify) {
        return jiluMapper.updateByPrimaryKeySelective(entify) == 1;
    }

    // 更新记录
    public boolean updateByPrimaryKey(Jilu entify) {
        return jiluMapper.updateByPrimaryKey(entify) == 1;
    }

    // 分页查询记录
    public Page<Jilu> selectByPage(Integer startId, Integer pageIndex, Integer pageSize, String searchKey) {
        Page<Jilu> pageVenderList = PageHelper.startPage(pageIndex, pageSize);
        jiluMapper.query(pageIndex, pageSize, searchKey);
        return pageVenderList;
    }

    // 统计最近几天的记录
    public Tongji getTongjiByDay(Integer days) {
        Tongji tongji = new Tongji();
        List<Map<String, Double>> jilus = jiluMapper.queryByDay(days);
        if(null != jilus && jilus.size() > 0){
            String[] x = new String[jilus.size()];
            String[] y = new String[jilus.size()];
            for(int i =0;i<jilus.size();i++){
                Map<String, Double> map = jilus.get(i);
                   try {
                       x[i] = DateUtil.formatWeb(map.get('riqi')+"",DateUtil.WEB_FORMAT);
                       y[i] = map.get('danjia')+"";
                   } catch (ParseException e) {
                       e.printStackTrace();
                   }
            }
            tongji.setY(y);
            tongji.setX(x);
        }
        return tongji;
    }
}

方法说明:

  • deleteByPrimaryKey(Integer id): 根据主键删除记录。
  • insert(Jilu entify): 插入记录。
  • insertSelective(Jilu entify): 选择性插入记录。
  • selectByPrimaryKey(Integer id): 根据主键查询记录。
  • updateByPrimaryKeySelective(Jilu entify): 选择性更新记录。
  • updateByPrimaryKey(Jilu entify): 更新记录。
  • selectByPage(Integer startId, Integer pageIndex, Integer pageSize, String searchKey): 分页查询记录,使用 PageHelper 插件进行分页。
  • getTongjiByDay(Integer days): 统计最近几天的记录,并将结果封装成 Tongji 对象返回。Tongji 对象包含两个数组 xy,分别表示日期和单价。

依赖:

  • JiluMapper:记录数据访问层接口。
  • PageHelper:分页查询插件。
  • DateUtil:日期工具类。
  • Tongji:统计数据对象。

说明:

该服务类提供了对记录数据的基本操作,包括增删改查和分页查询。getTongjiByDay 方法可以用于统计最近几天的记录数据,并以图表的形式展示。开发者可以根据实际需求扩展该类,实现更多功能。

JiluService 类:记录管理服务实现

原文地址: https://www.cveoy.top/t/topic/kLUd 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录