//////////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
}