Dynamic SQL Mybatis will be the


Foreword

Mybatis can be described as java developer will have a skill. One of the powerful features of MyBatis is its dynamic SQL. If you have experience with JDBC or other similar framework, you will be able to feel the pain, depending on conditions splicing SQL statement. For example, to ensure not forget to add the necessary space when splicing, but also pay attention to the last comma remove a list of column names. Dynamic SQL can use this feature to completely get rid of the pain.

Mybatis dynamic sql

mybatis dynamic SQL, by if, choose, when, otherwise, trim, where, set, foreach and other labels can be combined into a very flexible SQL statements, so while improving the accuracy of SQL statements, but also greatly enhance the developer effectiveness. This paper describes these dynamic SQL.

Specific examples

if tag field if the input is used to map a general judgment null and non-null judgment "."

<- Case 1:! Dynamic sql of if ->

<select id="selectUsersIf" parameterType="user" resultType="user">

select * from users where 1=1

<if test="uname!=null and uname!=''"> and uname like "%"#{uname}"%" </if>

<if test="sex!=null and sex!=''">and sex = #{sex} </if>

</select>

Dynamic SQL <where /> where keyword corresponding to <where /> can automatically process the first front and or or. When conditions are not where they will not add.

<- Case 2:! Dynamic sql where it can be automatically processed before the first and or or. When conditions are not where it will not add ->

<select id="selectUsersWhere" parameterType="user" resultType="user">

select * from users

<where>

<if test="uname!=null and uname!=''"> and uname like "%"#{uname}"%" </if>

<if test="sex!=null and sex!=''">and sex = #{sex} </if>

</where>

choose-when - when - otherwise when a plurality of otherwise only be similar to a switch case.

Requirements: Enter the user id in accordance with the user id pinpoint other conditions do not look do not enter a user name id fuzzy search queries are not, then the user id = 1

<- Case 3:! Dynamic sql of choose-when when otherwise ->

<select id="selectUsersChoose" parameterType="user" resultType="user">

select * from users

<where>

<choose>

<when test="uid!=null"> uid=#{uid}</when>

<when test="uname!=null and uname!=''"> uname like "%"#{uname}"%"</when>

<otherwise>uid=1</otherwise>

</choose>

</where>

</select>

Dynamic sql instead of set of keyword set label set can help us get rid of the last comma

<update id="updateSet" parameterType="user">

update users

<set>

<if test="uname!=null and uname!=''"> uname =#{uname},</if>

<if test="upwd!=null and upwd!=''"> upwd =#{upwd},</if>

<if test="sex!=null and sex!=''"> sex =#{sex},</if>

<if test="birthday!=null"> birthday =#{birthday},</if>

</set>

where uid=#{uid}

</update>

Trim, trim instead of where

<- Case 5:! Trim instead of the dynamic sql where ->

<select id="selectUsersTrimWhere" parameterType="user" resultType="user">

select * from users

<!--

prefix: add a prefix refers to modifications

suffix: suffix modified

prefixOverrides: removing the prefix modified

suffixOverrides: remove the suffix modified

-->

<trim prefix="where" prefixOverrides="and|or" >

<if test="uname!=null and uname!=''"> and uname like "%"#{uname}"%" </if>

<if test="sex!=null and sex!=''">and sex = #{sex} </if>

</trim>

</select>

Trim instead of set:

<- Case 6:! Dynamic sql instead of the trim set ->

<update id="updateTrimSet" parameterType="user">

update users

<trim prefix="set" suffixOverrides="," suffix="where uid=#{uid}" >

<if test="uname!=null and uname!=''"> uname =#{uname},</if>

<if test="upwd!=null and upwd!=''"> upwd =#{upwd},</if>

<if test="sex!=null and sex!=''"> sex =#{sex},</if>

<if test="birthday!=null"> birthday =#{birthday},</if>

</trim>

Foreach to traverse the collection

<- Case 7:! Foreach loop through the array of dynamic sql ->

<select id="selectUsersForeachArray" resultType="user">

select * from users where uid in

<!--

collection: to traverse the collection

item: the current variable name of the object being traversed

open: start traversing

close: End convenience

index: index

separator: split

-->

<foreach collection="array" item="item" open="(" close=")" index="index" separator=",">

#{item}

</foreach>

</select>


to sum up

Proficiency in more than Mysql dynamic SQL, we can be more handy to complete my function, write less code to achieve more functionality.


Reproduced in: https: //juejin.im/post/5d03471351882559ef78e707

Guess you like

Origin blog.csdn.net/weixin_33739627/article/details/93180568