静态工具类注入mapper对象

简介: 项目中需要在一个utils工具类中,调用mapper对象进行查询并进行排序后选出数值最小的数值,然而静态方法里面直接注入会报空指针的错误。现在总结一下解决办法。

需求


项目中需要在一个utils工具类中,调用mapper对象进行查询并进行排序后选出数值最小的数值,然而静态方法里面直接注入会报空指针的错误。现在总结一下解决办法。


重要点


1若要进行排序,需要重写该实体类的compareTo()方法,实现Comparable接口。代码如下


@Override
  public int compareTo(Object o) {
    if(o instanceof TbHotelMenu){
      TbHotelMenu menu=(TbHotelMenu) o;
      int oP = (int) Double.parseDouble(menu.getMenuPrice());
      int tP = (int) Double.parseDouble(this.getMenuPrice());
      return tP-oP;//升序
//return tP-oP;//降序
    }
    return 0;
  }   


2、需要将该工具类注入到spring 。applicationContext-service.xml配置文件代码如下


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:context="http://www.springframework.org/schema/context"
     xmlns:p="http://www.springframework.org/schema/p"
     xmlns:aop="http://www.springframework.org/schema/aop"
     xmlns:tx="http://www.springframework.org/schema/tx"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-4.0.xsd
      http://www.springframework.org/schema/aop
      http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
      http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
      http://www.springframework.org/schema/util
      http://www.springframework.org/schema/util/spring-util-4.0.xsd
">
  <!-- 扫描包加载Service实现类 -->
  <context:component-scan base-package="com.test.server.service"></context:component-scan>
  <context:component-scan base-package="com.test.server.util"></context:component-scan>


3


autils类上面添加@Component注解


b@Autowired注入需要使用的mapper对象,注意不是静态的


c、创建一个静态的这个utils的对象


d、创建一个init方法,添加@PostConstruct注解,代码内容如下


@Component
public class LowestMenuPrice {
  @Autowired
    private  TbHotelMenuMapper menuMapper;
  private static LowestMenuPrice menuPrince; 
   @PostConstruct  
      public void init(){  
     menuPrince = this;  
     menuPrince.menuMapper = this.menuMapper;    
      }  
   //获取酒楼菜单价钱最低的菜单价格
    public static String getLowestPrince(long hotelId){
      TbHotelMenuExample example=new TbHotelMenuExample();
      TbHotelMenuExample.Criteria criteria = example.createCriteria();
      criteria.andHotelIdEqualTo(hotelId);
      List<TbHotelMenu> menuList = menuPrince.menuMapper.selectByExample(example);
      if(menuList!=null&&menuList.size()>0){
        Collections.sort(menuList);
        String menuPrice = menuList.get(0).getMenuPrice();
        Double valueOf = Double.valueOf(menuPrice);
      DecimalFormat Format=new DecimalFormat("#.00");  
          String price=Format.format(valueOf); 
          return price;
      }
      return "0.00";
    }   
}
相关文章
|
XML JSON Java
SpringBoot 实战:在 RequestBody 中优雅的使用枚举参数
本文先上实战,说一下如何实现。在 优雅的使用枚举参数 代码的基础上,我们继续实现。如果想要获取源码,可以关注公号「看山的小屋」,回复 spring 即可。
1805 0
SpringBoot 实战:在 RequestBody 中优雅的使用枚举参数
|
Java 关系型数据库 MySQL
MySQL中mybatis的大于等于小于等于的写法
MySQL中mybatis的大于等于小于等于的写法
416 3
|
8月前
|
关系型数据库 MySQL Linux
MySQL包安装 -- SUSE系列(SUSE资源库安装MySQL)
本文介绍了在openSUSE系统上通过SUSE资源库安装MySQL 8.0和8.4版本的完整步骤,包括配置国内镜像源、安装MySQL服务、启动并验证运行状态,以及修改初始密码等操作,适用于希望在SUSE系列系统中快速部署MySQL的用户。
784 3
MySQL包安装 -- SUSE系列(SUSE资源库安装MySQL)
|
Java 数据库连接 mybatis
成功解决: Invalid bound statement (not found) 在已经使用mybatis的项目里引入mybatis-plus,结果不能共存的解决
这篇文章讨论了在已使用MyBatis的项目中引入MyBatis-Plus后出现的"Invalid bound statement (not found)"错误,并提供了解决方法,主要是通过修改yml配置文件来解决MyBatis和MyBatis-Plus共存时的冲突问题。
成功解决: Invalid bound statement (not found) 在已经使用mybatis的项目里引入mybatis-plus,结果不能共存的解决
Invalid bound statement (not found)错误【已解决】
Invalid bound statement (not found)错误【已解决】
3524 1
|
前端开发 数据安全/隐私保护 开发者
Pic Smaller:开源神器,碾压TinyPNG!90%开发者不知道的免费图像压缩利器
嗨,大家好,我是小华同学。今天介绍一个强大的开源图像压缩工具——Pic Smaller(图小小)。它支持JPEG、PNG、WEBP等多种格式的智能压缩,具备本地压缩、自定义配置等特性,确保图像数据安全。用户只需上传图片,Pic Smaller便会自动执行压缩并提供详细信息。项目已部署在Vercel平台,Pic Smaller基于Vite和React构建,适合开发者、设计师及普通用户,帮助高效管理图像。
1762 9
|
Java 关系型数据库 数据库连接
mybatis中的useGeneratedKeys和keyProperty
在 MyBatis 中,`useGeneratedKeys` 和 `keyProperty` 是用于处理数据库自动生成主键的关键配置。通过这些配置,可以方便地获取和使用数据库生成的主键值,提高开发效率和代码可读性。确保正确配置和使用这两个属性,可以在应用程序中高效地进行数据库操作。
969 25
|
开发工具 git Windows
IDEA如何对比不同分支某个文件的差异
【9月更文挑战第28天】该指南介绍了在IDEA中使用Git工具窗口进行分支对比的方法。首先,通过底部工具栏或菜单打开Git窗口;接着,在“Branches”选项卡中查看所有分支;然后选择要对比的分支和文件,并通过右键菜单启动对比;最后,在“Diff”视图中查看详细差异,包括新增和删除内容的颜色标记。此外,还提供了使用内置终端执行`git diff`命令进行对比的可选方法。
4000 4
|
XML SQL Java
mybatis-plus异常记录:org.apache.ibatis.binding.BindingException Invalid bound statement
mybatis-plus异常记录:org.apache.ibatis.binding.BindingException Invalid bound statement
2941 0
mybatis-plus异常记录:org.apache.ibatis.binding.BindingException Invalid bound statement
|
域名解析 网络协议 算法

热门文章

最新文章