静态工具类注入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";
    }   
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

熟透的蜗牛

永远满怀热爱,永远热泪盈眶

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值