https://blog.csdn.net/MinggeQingchun/article/details/110957384
1、查詢語句
-
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String" >
-
selec...
-
</select>
2、插入語句
-
< insert id="insert" parameterType="pojo.OrderTable" >
-
insert into ordertable(...)
-
values(...)
-
</ insert>
3、刪除語句
-
< delete id="deleteByPrimaryKey" parameterType="java.lang.String" >
-
delete from ordertable
-
where order_id = #{orderId,jdbcType=VARCHAR}
-
</ delete>
4、修改語句
-
<update id= "updateByPrimaryKey" parameterType="pojo.OrderTable" >
-
update ordertable
-
set cid = #{cid,jdbcType=VARCHAR},
-
address = #{address,jdbcType=VARCHAR},
-
create_date = #{createDate,jdbcType=TIMESTAMP},
-
orderitem_id = #{orderitemId,jdbcType=VARCHAR}
-
where order_id = #{orderId,jdbcType=VARCHAR}
-
</update>
需要配置的屬性
- id="xxxx" 表示此段SQL執行語句的唯一標識,也是接口的方法名稱【必須一致才能找到】
- parameterType="xxxx" 表示SQL語句中需要傳入的參數,類型要與對應的接口方法的類型一致
- resultMap="xxx" 定義出參,調用已定義的映射管理器的id的值
- resultType="xxxx" 定義出參,匹配普通Java類型或自定義的pojo 【出參類型若不指定,將為語句類型默認類型,如語句返回值為int】
if標簽
用法:
-
< select id="listProduct" resultType="Product">
-
select * from product_
-
< if test="name!=null">
-
where name like concat('%',#{name},'%')
-
</if>
-
</select>
where標簽
作用:
標簽會進行自動判斷:
如果任何條件都不成立,那么就在sql語句里就不會出現where關鍵字(重點)
如果有任何條件成立,會自動去掉多出來的 and 或者 or。(就不需要我們追加1=1之類的入侵性代碼了)
用法:
-
< select id="listProduct" resultType="Product">
-
select * from product_
-
< where>
-
< if test="name!=null">
-
and name like concat('%',#{name},'%')
-
</if>
-
<if test="price!=null and price!=0">
-
and price >
-
</ if>
-
</ where>
-
</ select>
set標簽
作用是:
與where標簽類似的,在update語句里也會碰到多個字段相關的問題。 在這種情況下,就可以使用set標簽。
其效果與where標簽類似,有數據的時候才進行設置。
用法:
-
<update id="updateProduct" parameterType="Product" >
-
update product_
-
<set>
-
<if test="name != null">name=#{name},</if>
-
<if test="price != null">price=#{price}</if>
-
</set>
-
where id=#{id}
-
</update>
trim標簽
作用是:
trim 用來定制想要的功能,比如where標簽就可以用
用法:
-
<select id="listProduct" resultType="Product">
-
select *from product_
-
<trim prefix="WHERE" prefixOverrides="AND |OR ">
-
<if test="name!=null">
-
and name like concat('%',#{name},'%')
-
</if>
-
<if test="price!=null and price!=0">
-
and price > #{price}
-
</if>
-
</trim>
-
</select>
-
-
<update id="updateProduct" parameterType="Product" >
-
update product_
-
<trim prefix="SET" suffixOverrides=",">
-
<if test="name != null">name=#{name},</if>
-
<if test="price != null">price=#{price}</if>
-
</trim>
-
where id=#{id}
-
</update>
- trim 用來定制想要的功能,比如where標簽就可以用
-
<trim prefix="WHERE" prefixOverrides="AND |OR ">
-
...
-
</trim>
來替換
set標簽就可以用
-
<trim prefix="SET" suffixOverrides=",">
-
...
-
</trim>
來替換
運行set標簽中的代碼,其效果是一樣的。
choose when otherwise 標簽
作用是:
有任何任何條件符合,就進行條件查詢,
否則就只使用id>1這個條件(即前面的標簽都不符合條件)。
也就相當於if···········else
Mybatis里面沒有else標簽,但是可以使用when otherwise標簽來達到這樣的效果。
用法:
-
-
-
-
-
<mapper namespace="com.myjava.pojo">
-
<select id="listProduct" resultType="Product">
-
SELECT * FROM product_
-
<where>
-
<choose>
-
<when test="name != null">
-
and name like concat('%',#{name},'%')
-
</when>
-
<when test="price !=null and price != 0">
-
and price > #{price}
-
</when>
-
<otherwise>
-
and id >1
-
</otherwise>
-
</choose>
-
</where>
-
</select>
-
</mapper>
foreach標簽
作用是:
foreach標簽通常用於in 這樣的語法里。
用法(接收一個List集合參數):
-
-
-
-
-
<mapper namespace="com.myjava.pojo">
-
<select id="listProduct" resultType="Product">
-
SELECT * FROM product_
-
WHERE ID in
-
<foreach item="item" index="index" collection="list"
-
open="(" separator="," close=")">
-
#{item}
-
</foreach>
-
</select>
-
</mapper>
bind標簽
bind標簽就像是再做一次字符串拼接,網上也有說叫綁定,差不多意思,只是方便后續的使用。
用法:
-
-
-
-
-
<mapper namespace="com.myjava.pojo">
-
-
<select id="listProduct" resultType="Product">
-
<bind name="likename" value="'%' + name + '%'" />
-
select * from product_ where name like #{likename}
-
</select>
-
-
</mapper>
sql片段標簽
作用是:
通過該標簽可定義能復用的sql語句片段,在執行sql語句標簽中直接引用即可。
這樣既可以提高編碼效率,還能有效簡化代碼,提高可讀性。
用法:
-
<!--定義sql片段-->
-
<sql id="orderAndItem">
-
o.order_id,o.cid,o.address,o.create_date,o.orderitem_id,i.orderitem_id,i.product_id,i.count
-
</sql>
-
<select id="findOrderAndItemsByOid" parameterType="java.lang.String" resultMap="BaseResultMap">
-
select
-
<!--引用sql片段-->
-
<include refid="orderAndItem" />
-
from ordertable o
-
join orderitem i on o.orderitem_id = i.orderitem_id
-
where o.order_id = #{orderId}
-