Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of SUM Mapping in MyBatis

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

In this article Xiaobian for you to introduce in detail "MyBatis SUM mapping problem how to solve", the content is detailed, the steps are clear, the details are handled properly, I hope this article "MyBatis SUM mapping problem how to solve" article can help you solve doubts, the following follow the editor's ideas slowly in-depth, together to learn new knowledge.

SUM mapping problem

When we make statistics according to the category, the returned data type is HashMap, and it is easy to get the numerical type.

Java.math.BigDecimal cannot be cast to java.lang.Integer

The scene is as follows:

/ / Mapper layer SELECT SUM (flag) as flags,taskid FROM qcdata GROUP BY taskid// interface List selectInfoByTest (); / / call code List result = qcDao.selectInfoByTest (); int flags= (Integer) result.get (0). Get ("flags"); / / error return JSONResult.ok (flags); reason

The return value of sum () in sql is returned as BigDecimal in mybatis, and cannot be received with Integer

Solution method

Can be converted to a string, and then converted to the int type, in the conversion process, can not use (String) this way to force, this is not a String type, you can use toString (), you can also use String.valueOf (), a simpler way is to use an empty string to convert

Public JSONResult test () {List result = qcDao.selectInfoByTest (); / / toString int flags=Integer.parseInt (result.get (0). Get ("flags"). ToString (); / / String.valueOf int flags2=Integer.parseInt (String.valueOf (result.get (0). Get ("flags")); / / empty string int flags3=Integer.parseInt (result.get (0). Get ("flags") + "") Return JSONResult.ok (flags+flags2+flags3);}

It should be noted that before the strong turn, it is best to judge whether it is empty, the empty string and the type match to avoid the failure of the strong turn.

Sum return mapping problem (returned by sum report statistics API) MyBatis sum return value mapping

Mapper.xml code

Select sum (com.thinkmoney*ord.commnumber) as totalPrice, com.category as category from commodity com,orders ord where com.commid=ord.commid and ord.orderstatus=1 GROUP BY com.category

Pojo

Private static final long serialVersionUID = 1L; / * * order id * / private String id; / * * order No. * / private String ordernumber; / * time to place the order * / private Date ordertime; / * Product name * / private String commname / * Commodity id * / private String commid; / * Commodity description * / private String commdesc; / * purchase quantity * / private Integer commnumber; / * Commodity unit price * / private BigDecimal price; / * receiving address * / private String useraddress / * * order status 0 not paid 1 normal 2 Delete * / private Integer orderstatus; / * * consignee * / private String username; / * * consignee Mobile number * / private String mobilephone; / * Shipping status 0 unshipped 1 shipped 2 confirmed receipt * / private Integer kdstatus / * Express No. * / private String kdnumber; / * * buyer id * / private String buyuserid; / * * seller id * / private String selluserid; private Commodity commodity; private BigDecimal totalPrice

Controller

/ * administrator home pie chart * * / @ GetMapping ("/ echars/piechart") public String piechart (HttpSession session,HttpServletRequest request) {List result = ordersService.pieChart (); List totalPriceList= new ArrayList (); List categoryList= new ArrayList (); for (Map mapList: result) {totalPriceList.add (mapList.get ("totalPrice"). ToString ()) CategoryList.add (mapList.get ("category"). ToString ());} session = request.getSession (); System.out.println ("totalPriceList:" + totalPriceList+ ", categoryList:" + categoryList "); session.setAttribute (" totalPriceList ", totalPriceList); session.setAttribute (" categoryList ", categoryList); return" / admin/echars/piechart " } after reading this, the article "how to solve the SUM Mapping problem of MyBatis" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report