動態(tài)SQL
<select id="queryByCountryCity" resultType="com.text.pojo.Addresses">
select * from addresses
<where>
<if test="country != null and country !=''">
COUNTRY = #{country}
</if>
<if test="city != null and city !=''">
and
CITY = #{city}
</if>
</where>
</select>
<if>if 可以加入test=""進(jìn)行相關(guān)判斷
<where>where 可以去掉多余的前置and
<!--
功能:
根據(jù)傳入的對象動態(tài)的修改其中的值,
如果某個字段傳入的非空值,再去修改,否則不修改
set只能處理后置的
-->
<update id="update" parameterType="com.text.pojo.Addresses">
update addresses
<set>
<if test="city != null and city != ''">
city = #{city},
</if>
<if test="country != null and country != ''">
country = #{country},
</if>
<if test="street != null and street != ''">
street = #{street},
</if>
<if test="state != null and state != ''">
state = #{state},
</if>
<if test="zip != null and zip != ''">
zip = #{zip},
</if>
</set>
<where>
ADDR_ID = #{addrId}
</where>
</update>
<set>set 除去后面的逗號,
<select id="query" resultType="com.text.pojo.Addresses" parameterType="com.text.pojo.Addresses">
select * from addresses
<where>
<choose>
<when test="country != null">
and country = #{country}
</when>
<when test="state != null">
and state = #{state}
</when>
<otherwise>
and city = #{city}
</otherwise>
</choose>
</where>
</select>
<choose>choose 相當(dāng)于if.. else if
<otherwise>otherwise 相當(dāng)于else
<select id="queryTrim" resultType="com.text.pojo.Addresses" parameterType="com.text.pojo.Addresses">
select * from addresses
<trim prefix="WHERE" suffixOverrides="AND">
<if test="city != null and city != ''">
city = #{city} and
</if>
<if test="country != null and country != ''">
country = #{country} and
</if>
<if test="street != null and street != ''">
street = #{street} and
</if>
<if test="state != null and state != ''">
state = #{state} and
</if>
<if test="zip != null and zip != ''">
zip = #{zip} and
</if>
</trim>
</select>
<trim>trim 比較全能
可以添加屬性,prefix suffix代表前面或后面加額外的sql字段
prefixOverrides suffixOverrides代表前面和后面可能有多余的sql字段
mybatis可以根據(jù)屬性自己識別
<select id="queryByIds" resultType="com.text.pojo.Addresses">
select *
from addresses
<where>
addr_id in
<!--
collection代表集合類型
open代表拼接時以什么開頭
close代表拼接時以什么結(jié)尾
item代表遍歷的每個元素的名字,一個代號
separator代表分隔符
index 如果需要標(biāo)號也可以使用
-->
<foreach collection="list" open="(" close=")" item="item" separator=",">
#{item}
</foreach>
</where>
</select>
<foreach>foreach 可以進(jìn)行遍歷
有collection open close item separator index等多種屬性
<!--#{}內(nèi)不支持拼接
實現(xiàn)模糊查詢
解決方案1:在應(yīng)用層進(jìn)行拼接再傳進(jìn)mapper.xml
解決方案2:再xml中用字符串拼接 '%'+ #{city} + '%' 但是這樣有sql注入風(fēng)險
解決方案3:通過mysql函數(shù)concat(*,*,*)實現(xiàn)拼接
解決方案4:使用bind標(biāo)簽,對我們的變量進(jìn)行重新綁定,然后通過新綁定的變量進(jìn)行引用即可
-->
<select id="queryLike" resultType="com.text.pojo.Addresses">
<bind name="_city" value="'%'+city+'%'"/>
select * from addresses
<where>
city like #{_city}
</where>
</select>
<bind>bind 可以進(jìn)行自定義變量
<!--將最常用的sql語句變成片段,方便復(fù)用-->
<sql id="baseColumn">
country,state,city
</sql>
<select id="listAll" resultType="com.text.pojo.Addresses">
select
<include refid="baseColumn"/>
from addresses
</select>
<sql>sql 可以取出重復(fù)的sql片段進(jìn)行復(fù)用
<include> include 可以導(dǎo)入sql片段
兩個標(biāo)簽需要配合使用