Mybatis dynamic sql usage

Dynamic, as the name implies, means that the data is not static or hard-coded, it is an SQL statement that can be changed at any time according to needs. For example, in terms of query, the needs of users are ever-changing, but the methods provided by Mybatis can be realized uniformly. This is the knowledge of dynamic Sql. This article uses the SqlMapper.xml file to demonstrate various tags used.

1. if element judgment statement, single conditional branch judgment

         <if test="field != null">
             and field=#{field}
         </if>
         
         <if test="field !=0">
             and field=#{field}
         </if>

2. where 

<where>
             <if test="ssex != null">
                 ssex=#{ssex}
             </if>
             <if test="classid !=0">
                 and classid=#{classid}
             </if>
         </where>

Notice:

 1. If no condition is met,
 there will be no where and and 2. If one condition is met, where will be added automatically. The and of the first condition can be added without the first and where will be added automatically.  
 3. If multiple conditions are met, it will be added automatically where and the second and onwards

3. Set the tag specially used in update
             <set>
             <if test="field 1 != null">
                 field 1=#{field 1},
             </if>
         
             <if test="field 2 !=0">
                 field2 = #{field2},
             </if>
         </set>

4. trim universal label

<trim prefix="" suffix="" suffixOverrides="" prefixOverrides=""></trim>

prefix: the words to be spliced ​​at the beginning

suffix: end the sentence to be spliced

suffixOverrides: the words to be removed at the end

prefixOverrides: the words to be removed at the beginning

<trim prefix="where" prefixOverrides="and">
             <if test="field 1!= null">
                 and field 1=#{field 1}
             </if>
             <if test="field 2!=0">
                 and field2=#{field2}
             </if>
         </trim>

5. Usage of foreach loop

<foreach collection="list" item="item" index="index" open="" close="" separator=";">

item: the element in the loop

collection: The parameters passed by the method, array or collection

open: what symbol to start wrapping

close: What symbol ends the package

separator: the interval symbol of each element

index: the subscript of the loop element

        <foreach collection="array (array) list (collection)" item="field" open="(" close=")" separator=","> #{field
             }
         </foreach>

6.bind is mainly used for fuzzy query

First, introduce several fuzzy queries without bind;

The first method: select * from student where sanme like #{v}, this method cannot prevent illegal SQL injection, nor can it blur the front and back

The second method: select * from student where sanme like concat(#{v},'%'), belongs to my favorite method, because concat can also be used to concatenate strings in the database

The third type: select * from student where sanme like '${v}%', can fuzzy query, but cannot prevent illegal SQL injection

The fourth type: select * from student where sanme like "%"#{v}"%", which can fuzzy query and prevent injection

The fifth type: <bind name="aa" value="_parameter+'%'"/>
                 select * from student where sname like #{aa} is the usage of bind, where the meaning of value is the fuzzy meaning after adding parameters, Be careful not to forget the underscore that must be added.


 The above are the tags commonly used in Mybatis dynamic sql.

end of article
     

Guess you like

Origin blog.csdn.net/m0_48011056/article/details/126337066