mybatis using pit encountered

Working on this record it encountered during use mybatis

Problem Description:

mybatis codes in sql
We can see the figure above shows the mybatis code I write, a very simple query, encapsulates several sub-query statistics only. The figure is a specific sub-query:
Sql subqueries
a single-table query statistics sql, where the label contains the query. The figure is another piece query sql:
Sql subqueries
you can see here is no different queries, and now talk about my problems now.

  1. This query statement contains a number of sub-queries, each sub-query is a single form of statistical data, that count operation;
  2. As we all know, count operation returns the number of records that meet the conditions, if the conditions are not met then the record count is 0;
  3. Then I encountered the problem is that the return of the object field is mapped to the Javabean actually returned null, this is very weird situation I investigation for a long time, because some of the field is not null, and some are null, not even query the data or at least return 0, but it is unreasonable return null;
  4. I checked through the code, usually this happens, it should be no return to the field and lead to a corresponding javabean, but I confirm the code, not the problem, debug sql view through print, after the implementation of the return is indeed ah 0 it should not be null fishes;
  5. Repeated examination, I finally found the problem, see the first chart, look closely, you will notice that I added a few lines of comments in the code, the operation of each sub-query, but not the same as comments a bit, actually there are two forms of comments are:
(1) <!--  -->
(2) /*  */
  1. Notes written in the xml file we should all know, is the first form, the second form of comment is the content, the difference between these two comments in java is, at run time, it is included in the first comment sql not be carried to the execution, while the second will follow comments sql code execution together. So the question arises here, the present data returns 0 when mapping failed to javabean, javabean so returned a null value when returning the front end.

The same problem I have encountered in another form

Another problem of the use of annotations

  • You can see in the figure above, the compiler gives an error message saying choose the label should contain when, otherwise the label, but I blind? I wrote it not when and otherwise tags that? The idea compiler ventilation skeptical, I hesitate to start a project, that should not affect the compiled project run it.
  • As a result, I do not want to see what happened, to start compiling a direct failure, suggesting the console log sql tag syntax errors, I looked at the code is puzzling, contrast with other similar sql code viewer, nothing wrong ah , why has it prompted me wrong, tangled for a long time, finally found, the code exists comments:
/*  */
  • That is, it affects the entire compiled code, leading to compilation is not passed, it will change the comment form:
<!--  -->
  • This form can be, or simply deleted the comment, but for code readability, or modify it.

to sum up

The root cause of these problems is to understand the syntax xml thorough enough, there is why I write this comment form then, obviously have to use the code in this format, how to use different forms of the comment it? Think about it, I used the built-in annotation tools ide shortcuts to generate comments, and some ide If not manually modified settings, it will generate inconsistent with the comment syntax, so in the process of using the development tool Note also that this issue, the problems encountered above is my own idea used in mac, so after this question I immediately change the setting shortcuts.

Published 42 original articles · won praise 10 · views 7058

Guess you like

Origin blog.csdn.net/MCJPAO/article/details/100052323