Mybatis-Generator generates answers to <if test="criteria.valid"> in Mapper files

write in front

"Docker+SpringBoot+Mybatis+thymeleaf's Java blog system is open source"

Due to the open source project, many friends who use the My Blog project will contact me to solve problems when they encounter problems. Some of them leave the problem in the project's issue, some leave a message in my private blog, and some are Add my qq directly to talk to me about the problems you have encountered. Some problems are relatively simple and straightforward to solve, and the solution records of some problems are also kept in the issue records. If time permits, I will make a separate blog to answer.

Problem Description

Chat log at the time:
question

Code mentioned in the screenshot (excerpt):

ContentVoMapper.xml:

 <sql id="Example_Where_Clause">
    <where>
      <foreach collection="oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
CommentVoExample:

    protected abstract static class GeneratedCriteria {
        protected List<Criterion> criteria;

        protected GeneratedCriteria() {
            super();
            criteria = new ArrayList<Criterion>();
        }

        public boolean isValid() {
            return criteria.size() > 0;
        }

        public List<Criterion> getAllCriteria() {
            return criteria;
        }

        public List<Criterion> getCriteria() {
            return criteria;
        }
        ...

Problem sorting: There is no valid attribute in the GeneratedCriteria class, there is only one isValid()method, but in the grammar of mybatis in the Mapper file <if test>, there criteria.validare expressions, and the program can run normally, what is going on?

idea arrangement

First of all, I was a little confused when I first saw this question, because I didn't actually write the code. The Mapper file was automatically generated by me through Mybatis-Generator, so I was a little unfamiliar with this code, hahahaha.

But after reading the code, I think that mybatis should automatically find the isValid() method according to the valid attribute, and then perform a logical judgment. Of course, this is all personal feeling, there is no basis, and I vaguely feel that it should be so. But, after all, this friend is here to ask a question, I can't simply reply with such a sentence, and it is an answer that even I am not sure about.

jingxi

Doubtful questions include:

  • I don't know if mybatis is this execution process;
  • Even if it is the above process, then why is the attribute that is not at all parsed by mybatis normally;
  • Why does mybatis execute the isValid()method and not execute other methods.

solution process

With the above questions and uncertainty in my heart, the only thing I can do is to check the source code of this part of the process, and finally got the answer as expected. I got the execution process of the code through the debug function of IDEA, and I can check it by myself. the whole process:

  • In the IfSqlNodeclass, get the if testvalue of the expression in the tag: criteria.valid;

IfSqlNode

  • Then it is ObjectPropertyAccessorparsed into the attribute value Criteriaclass that needs to be operated in the class valid;

ObjectPropertyAccessor

  • Then OgnlRuntimeget the Method method corresponding to the execution of the expression in the class isValid().

OgnlRuntime

The next step is to execute the method and get the return value, no more screenshots.

The first two questions above have answers:

  • The execution flow from <if test="criteria.valid">the execution isValid()method to the execution method is found. Although there are many processes, several important nodes are the above three points. Obtain the class name and attribute value in the Mapper expression, and then obtain the method to be executed, and finally realize the entire function.
  • mybatis does not pay attention to whether this property exists, but finds the corresponding method according to the property and executes it.

As for the third question, I also made some extensions. If other methods with valid strings will also be executed, the result is a positive answer, as shown in the figure:

will be isValid()changed togetValid()

getValid

OgnlRuntimegetValid()The method method of the corresponding execution is obtained in the class

OgnlRuntime-get

When both methods are present, the isValid()method is executed because if testa boolean return value is required, and when only the getValid()method is present, it is executed getValid().

Epilogue

First published on my personal blog .

If you have any questions or have some good ideas, please leave me a message, and thanks to the friends who pointed out the problems in the project to me. Regarding this article, I would like to thank you very much @libinghui.

The code and this problem are all in the My Blog project. If you want to continue to understand the project, you can check the entire series of articles Java open source blog My-Blog (SpringBoot+Docker) series articles , you can also go to my GitHub repository or open source China View the source code and detailed deployment process and usage documentation in the code repository .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325774886&siteId=291194637