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.