java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > SpringBoot在自定义类中调用service层mapper层

SpringBoot在自定义类中调用service层mapper层方式

作者:liudachu

这篇文章主要介绍了SpringBoot在自定义类中调用service层mapper层方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

SpringBoot在自定义类中调用service层mapper层

最近在整合webscoket,因为在websocket中需要自定义websocket类,而在后端发送的信息的时候,需要调用service层mapper层的代码,或者自己编写一个工具类,这里在自定义类中使用 @Autowired会报空指针异常,所以不能使用普通的注入方式,百度上能用的教程很多,我这里写一个我尝试过能用的。

解决方案

1.上代码

@Component
public class ServerHandler extends IoHandlerAdapter {
    @Autowired
    protected HealthDataService healthDataService;
    private static ServerHandler  serverHandler ;
    @PostConstruct //通过@PostConstruct实现初始化bean之前进行的操作
    public void init() {  
        serverHandler = this;  
        serverHandler.healthDataService = this.healthDataService;        
        // 初使化时将已静态化的testService实例化
    }  
    //测试调用
    public void test(){
        serverHandler.healthDataService.<你的service层方法>;
    }

2.说明

导入mapper层同上

/**
	我自己编写的工具类
*/
@Component
public class GetChartDataUtils {

    private static GetChartDataUtils getChartDataUtils ;

    @Autowired
    private OrderMapper orderMapper;

    @PostConstruct //通过@PostConstruct实现初始化bean之前进行的操作
    public void init() {
        getChartDataUtils = this;
        getChartDataUtils.orderMapper=this.orderMapper;
        // 初使化时将已静态化的orderMapper实例化
    }

    public static Trend<PosMonth> getOrgMonthTrend() {
        Trend<PosMonth> posMonthTrend = new Trend<>();
        posMonthTrend.setTitle("客源分析趋势");
        posMonthTrend.setBase(300);
        posMonthTrend.setUnit("万");
        List<PosMonth> posMonths = new ArrayList<PosMonth>();
        //封装每个出发地点每个月的数据
        PosMonth zzgs = new PosMonth("郑州工商学院",getChartDataUtils.orderMapper.queryOriginMonthCount("郑州工商学院"));//出发地为郑州工商学院每个月的数据
        PosMonth zzjt = new PosMonth("河南交通学院",getChartDataUtils.orderMapper.queryOriginMonthCount("河南交通学院"));//出发地为河南交通学院每个月的数据
        PosMonth zkzx = new PosMonth("周口中心站",getChartDataUtils.orderMapper.queryOriginMonthCount("周口中心站"));//出发地为周口中心站每个月的数据
        PosMonth zzly = new PosMonth("郑州旅游学院",getChartDataUtils.orderMapper.queryOriginMonthCount("郑州旅游学院"));//出发地为郑州旅游学院每个月的数据
        //数据封装进趋势图
        posMonths.add(zzgs);
        posMonths.add(zzjt);
        posMonths.add(zkzx);
        posMonths.add(zzly);
        posMonthTrend.setData(posMonths);
        return posMonthTrend;
    }

    public static Trend<PosMonth> getDisMonthTrend() {
        Trend<PosMonth> posMonthTrend = new Trend<>();
        posMonthTrend.setTitle("去向分析趋势");
        posMonthTrend.setBase(300);
        posMonthTrend.setUnit("万");
        List<PosMonth> posMonths = new ArrayList();
        //封装每个去向地点每个月的数据
        PosMonth zzgs = new PosMonth("周口火车站",getChartDataUtils.orderMapper.queryDestinationMonthCount("周口火车站"));//目的地为周口火车站每个月的数据
        PosMonth zzjt = new PosMonth("周口市中心站",getChartDataUtils.orderMapper.queryDestinationMonthCount("周口市中心站"));//目的地为周口市中心站每个月的数据
        PosMonth zkzx = new PosMonth("周口市淮阳",getChartDataUtils.orderMapper.queryDestinationMonthCount("周口市淮阳"));//目的地为周口市淮阳每个月的数据
        PosMonth zzly = new PosMonth("周口市项城",getChartDataUtils.orderMapper.queryDestinationMonthCount("周口市项城"));//目的地为周口市项城每个月的数据
        //数据封装进趋势图
        posMonths.add(zzgs);
        posMonths.add(zzjt);
        posMonths.add(zkzx);
        posMonths.add(zzly);
        posMonthTrend.setData(posMonths);
        return posMonthTrend;
    }
}

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文