MyBatis Dynamic SQL

Source: Internet
Author: User

MyBatis Dynamic sql:

Dynamic SQL is a programming method for generating and executing SQL at run time

Dynamic is relative to static. Static SQL refers to the well-defined fixed SQL statements that have been included in the code at the time of Code compilation

How is dynamic SQL set in MyBatis? How do I use it?

MyBatis Dynamic SQL is based on OGNL expression, which is judged by expression, and the SQL is flexibly spliced and assembled.

Common nodes are: if, choose when otherwise, where, set, trim, foreach, SQL fragment, etc.

  1.1 If

    <select id= "Selectactiveblogbytitle" parametertype= "Blog" resultmap= "Blogresultmap" >                SELECT *  From blog        WHERE state = ' ACTIVE '        <if test= "title! = null and Title! = '" "> and            title like #{titl e}        </if>            </select>    
1.2 Choose when otherwise
    <!--several query conditions mutually exclusive--    <select id= "Selectactiveblogbytitleorstyle" parametertype= "Blog" resultmap= " Blogresultmap ">        select * from blog         WHERE state = ' ACTIVE '                <choose>            <when test=" Title! = NULL and Title! = "" > and                lower (title) like Lower (#{title})            </when>            <when test= " Style! = NULL and style! = "" > and                style = #{style}            </when>            <otherwise>  True </otherwise> </choose> </select>     
1.3 Where
<!--whereAutomatically patching query criteria, using the WHERE keyword in the query statement<where> Label Substitution Note: You cannot omit the and OR or keywords--<select id= "Selectblogbycondition" Parameterty Pe= "Blog" resultmap= "Blogresultmap" >select * FROM blog <where> <if test= "state! = null and state! =" "> state = #{state} </if> <if test=" title! = null and Title! = ' "" > and Lower (title) Li Ke Lower (#{title}) </if> <if test= "Featured! = NULL" > and featured = #{featured} </  if> </where> </select>            

1. 4 Set

<!--set:will automatically remove the extra comma---<update id= "upadteblogbycondition" parametertype= "Blog" after the statement >UPDATE ' blog ' <set> <if test= ' title! = null ' > ' title ' = #{title},< ;/if> <if test= "Authorid! = null" > ' author_id ' = #{authorid},</if> <if test= "sta Te! = null "> ' state ' = #{state},</if> <if test=" Featured! = null "> ' Featured ' = #{featured},</< C8>if> <if test= "style! = null" > ' style ' = #{style} </if> </set> WHERE ' id ' = #{id} </update>           

1. 4 Trim

<!--TrimTrim can implement set and where functions--<select id= "Selectblogbyconditiontrim" parametertype= "Blog" resultmap= "Blogresultmap ">select * from blog <trim prefix= "where" prefixoverrides= "and | or" > <if te St= "state! = null and state! =" "> state = #{state} </if> <if test=" title! = NULL and title! = "" > and lower (title) like Lower (#{title}) </if> <if test= "Featured! = NULL" > and featur ed = #{featured} </if> </trim> </select>        

1.5 foreach

    <delete id= "deletebloglist" parametertype= "list" > delete from the        blog where ID in        <foreach collection= "List" item= "Item" open= "(" close= ")" separator= "," >            #{item}        </foreach>    </ Delete> 

1.6 SQL Fragment

    <!-- SQL Clips--    <sql id= "Aliascolumn" >            </sql>                <!--references    --< Include refid= "Aliascolumn" >        </include> 

MyBatis Dynamic SQL

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.