MyBatis(two)

//////////2016-12-22 ~ 2016-12-23///////////

int study_data(){

MyBatis select語句擴(kuò)展

查詢部分信息方法1:

? ? <sql id="selectFileds(customName)">column1,column2...</sql>

? ? <select id="userColumns(customName)" resultMap="BaseResultMap">

? ? ? ? select <include refid="sql-customName" /> from tableName

? ? </select>

?查詢部分信息方法2:

? ? <sql id="customName">${alias}.propertyName,${alias}.propertyName...</sql>

? ? <select id="aliasGetUser(customName)" resultMap="BaseResultMap">

? ? ? ? select <include refid="userColumns(customName)">?

? ? ? ? <property name="alias(上方${}中的字符串)" value="className(代替上方的${alias})"/>

? ? ? ? </include> from tableName

? ? </select>

動(dòng)態(tài)SQL

if:

<select id="customName" parameterType="inputType" resultMap="BaseResultMap">

? ? select * from tableName

? ? <if test="(condition)">?

? ? ? ? where (condition)

? ? </if>

</select>

choose,when,otherwise:

<select id="customName" parameterType="inputType" resultMap="BaseResultMap">

? ? select * from tableName

? ? <choose>

? ? ? ? <when test="(condition)">

? ? ? ? where (condition)

? ? ? ? </when>

? ? ? ? <when test="(condition)">

? ? ? ? where (condition)

? ? ? ? <otherwise>

? ? ? ? (condition)

? ? ? ? </otherwise>

? ? </choose>

</select>

trim(達(dá)到if else的效果):

<select id="customName" parameterType="inputType" resultMap="BaseResultMap">

? ? select * from tableName

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

? ? ? ? <if test="(condition)">

? ? ? ? ? ? (condition)

? ? ? ? </if>

? ? ? ? <if test="(condition)">

? ? ? ? ? ? (condition)

? ? ? ? </if>

? ? </trim>

</select>

在最后生成的sql語句中where后無條件符合,則不執(zhí)行,若where緊跟著and|or則刪除緊跟的那個(gè)and|or來保證sql語句的正確性

foreach:

<select id="customName" parameterType="inputType" resultMap="BaseResultMap">

? ? select * from tableName where column in

? ? <foreach collection="inputType" item="item" index="index" open="("

? ? separator="," close=")">

? ? ? ? #{item}

? ? </foreach>

</select>

可以利用標(biāo)簽實(shí)現(xiàn)sql條件的循環(huán),可完成類似批量的sql

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容