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.说明

  • 将需要调用Spring的Service层的类通过@Component注解为组件加载;
  • 同样通过@Autowired获取Service层的Bean对象;
  • 为类声明一个静态变量,方便下一步存储bean对象;
  • 划重点:通过注解@PostConstruct ,在初始化的时候初始化静态对象和它的静态成员变量healthDataService,原理是拿到service层bean对象,静态存储下来,防止被释放。

导入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;
    }
}

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我来解答你的问题。 针对你的需求,对于 SpringBoot 框架下的 Controller、ServiceMapper 实现增删改查通用功能,我们可以使用 MyBatis Plus 的基类以及代码生成器来完成。 首先,引入 MyBatis Plus 的依赖: ```xml <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.2</version> </dependency> ``` 在 MyBatis Plus 中提供了一个 `BaseMapper` 接口,我们可以自定义一个 `BaseMapper` 实现增删改查通用功能。代码如下: ```java public interface MyBaseMapper<T> extends BaseMapper<T> { default List<T> selectAll() { return selectList(null); } } ``` 接着,我们可以通过 MyBatis Plus 的代码生成器来生成对应的 Mapper、Entity 类和 Service。需要配置以下信息以便生成对应模板: ```java // 数据源配置 mpGenerator.setDataSource(dataSourceConfig); // swagger2配置 mpGenerator.setSwagger2(true); // 全局配置 GlobalConfig globalConfig = new GlobalConfig(); globalConfig.setOutputDir(outputDir); globalConfig.setAuthor(author); globalConfig.setBaseResultMap(true); globalConfig.setBaseColumnList(true); globalConfig.setFileOverride(true); mpGenerator.setGlobalConfig(globalConfig); // 策略配置 GeneratorStrategyConfig strategy = new GeneratorStrategyConfig(); strategy.setInclude(tableNames); strategy.setNaming(NamingStrategy.underline_to_camel); strategy.setColumnNaming(NamingStrategy.underline_to_camel); strategy.setEntityLombokModel(false); strategy.setEntityBuilderModel(true); strategy.setChainModel(true); strategy.setSuperMapperClass(MyBaseMapper.class); // 设置自定义Mapper类 mpGenerator.setStrategy(strategy); // 包配置 PackageConfig packageConfig = new PackageConfig(); packageConfig.setParent(parentPackage); mpGenerator.setPackageInfo(packageConfig); mpGenerator.execute(); ``` 生成后的具体方法可以参考 MyBatis Plus 的 API 文档,这里就不展开了。在 Controller、Service 中,直接调用 `BaseMapper` 提供的方法即可。 以上就是利用 MyBatis Plus 和代码生成器实现 SpringBoot 框架下 Controller、ServiceMapper 的增删改查通用功能的方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值