Problems encountered with the IF statement in MyBatis

Source: Internet
Author: User

Recently in the company learned to write project code with MyBatis, before also did not use MyBatis, originally just contact a little hibernate. This project uses the SPRINGMVC framework to write the operation statements of the database using the Mybaitis. The following is an issue that is encountered in using the IF process. The following code has been rewritten and is intended to be the same.

Say the process, the front-end click into the initialization interface, this time to the database query records and then return to the front, generate tables to show the data. A DTO class was received and nothing was entered when initializing. Here's the code:

public class tempdto{
   private long tempid;
   Private String tempname;
   private int frequence;

   public void Settempid (long tempid) {
        this.tempid=tempid;
}
   Public long Gettempid () {
        return tempid;
}
   public void Settempname (String tempname) {
        this.tempname=tempname;
}
   Public String GetTempName () {
        return tempname;
}
   public void setfrequence (int frequence) {
        this.frequence=frequence;
}
   public int getfrequence () {
        return frequence;
}
}


Then there is the XML code that the controller passes tempdto here, where the initialization query is out of the question. It took me quite a while to find out the mistake.

<select id= "SelectList" resulttype= "Tempdto" parametertype= "tempdto" >
      select
            tempid as  Tempid,
            tempname as Tempname,
            frequence as frequence from
      temp
      where 1=1 <if
      test= "Tempname! = Null "> and
      tempname like '% ' | | #{tempname,jdbctype=varchar}| | ' % '
       </if>
       <if test= "Frequence!=null" > and
       frequence=#{frequence}
       </if>

The code for the above XML is to query the records that satisfy the two query conditions of tempname and frequence, which is the default assignment of the system when initialized. Direct query, is out of the results. Thought for a long time, tested the default value of frequence, the result is 0. So tempdto is not assigned a value, the system defaults to the value of the variable of type int is 0. And the Frequencede value in my project is 1-8. So my restrictions on frequence are not perfect. This time change the IF statement to the following code:

<if test= "Frequence!=null and Frequence!=0" > and
frequence=#{frequence}
</if>
This will not enter the IF statement at the time of initialization, thus querying all records.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.