An example Analysis of mybatis sum column names as parameters
In this article, the editor introduces in detail the "case analysis of mybatis sum listing as a parameter", with detailed content, clear steps and proper handling of details. I hope that this "case analysis of mybatis sum listing as a parameter" article can help you solve your doubts.
Sum (parameter) column name as parameter
There are many fields in the project, and when the field is empty, average the column and assign a value to the field. Such as:
Idnameage
When I need to insert a piece of data, the age of the data is empty, so I need to query the average of the age from the database and then assign the value to the current data. When there are many fields, you need to query the column name as a parameter.
Mapper.xml
Select avg (${columnName}) from tb_chuai where ${columnName} > 0
DAO
BigDecimal getGtAvg (@ Param ("columnName") String columnName); notes
1. Aggregate functions, such as sum, avg, etc. ResultType are bigDecimal or integer.
2. ${} is generally used to transfer database table names, field names, etc., and cannot use # {}
3. Varchar in mysql can perform operations such as sum,avg, but there may be problems when calculating the average value of avg when the Chinese character is taken as 0.
Mybatis annotation sql sum (parameter) transfer parameter writing method
It has been verified for a long time that the parameter mode # {parameter} in sum () has not worked all the time. The log shows that the parameter has been passed but cannot be added. The value returned is always 0.
Then change it to $(parameter).
@ Select ("select sum (${type}) from" + TABLE_HOUR + "where date_format (`date`,'% Ymuri% mmi% d') = # {date}") public int selectDay2 (@ Param ("date") String date,@Param ("type") String type) After reading this, the article "case Analysis of mybatis sum listing as a Parameter" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself to understand it. If you want to know more about related articles, welcome to follow the industry information channel.