MyBatis(七) 動態SQL
if元素和where元素
if元素根據入參的條件進行SQL的拼接,where 元素則是在條件語句存在的情況下, 才會有where
<select id="queryStudentInfoByStudent" resultType="student" >
select stu_id as stuId,
stu_name as stuName,
stu_sex as stuSex,
stu_age as stuAge
from student_info
<where>
<if test="stuId!=null and stuId!=''">
and stu_id = #{stuId}
</if>
<if test="stuName!=null and stuName!=''">
and stu_name = #{stuName}
</if>
</where>
</select>
choose when 條件判斷
相當於sql中的case when
<select id="queryStudentInfoByStudent" resultType="student" >
select stu_id as stuId,
stu_name as stuName,
stu_sex as stuSex,
stu_age as stuAge
from student_info
<where>
<choose>
<when test="stuId!=null and stuId!=''">
and stu_id = #{stuId}
</when>
<when test="stuName!=null and stuName!=''">
and stu_name = #{stuName}
</when>
<otherwise>
and stu_sex = '1'
</otherwise>
</choose>
</where>
</select>
trim元素
trim將字串去除,prefix是加上字首,suffix是加上的字尾,prefixOverrides是去除的字首字串,suffixOverrieds去除的字尾字串
<select id="queryStudentInfoByStudent" resultType="student" >
select stu_id as stuId,
stu_name as stuName,
stu_sex as stuSex,
stu_age as stuAge
from student_info
<trim prefix="where" prefixOverrides="and">
<if test="stuId!=null and stuId!=''">
and stu_id = #{stuId}
</if>
</trim>
</select>
update+set+if元素實現選擇更新set元素會自動將末尾的逗號消除
<update id="updateStudentInfo" parameterType="student">
update student_info
<set>
<if test="stuName!=null and stuName!=''">
stu_name = #{stuName},
</if>
<if test="stuSex!=null and stuSex!=''">
stu_sex = #{stuSex},
</if>
<if test="stuAge!=null and stuAge!=''">
stu_age = #{stuAge}
</if>
</set>
where stu_id = #{stuId}
</update>
上面的可以是使用trim元素替換
<update id="updateStudentInfo" parameterType="student">
update student_info
<trim prefix="set" suffixOverrides=",">
<if test="stuName!=null and stuName!=''">
stu_name = #{stuName},
</if>
<if test="stuSex!=null and stuSex!=''">
stu_sex = #{stuSex},
</if>
<if test="stuAge!=null and stuAge!=''">
stu_age = #{stuAge},
</if>
</trim>
where stu_id = #{stuId}
</update>
foreach元素
在sql片段中實現引數的遍歷和組裝功能
屬性:collection是傳入的引數名稱,可以是set,list和陣列
open 、close、separate: 拼接字串的前後和分隔字元
item:當前元素
index:當前索引
public Student queryStudentInfoByStudent(Map<String,Object> map);
<select id="queryStudentInfoByStudent" resultType="student" parameterType="map">
select stu_id as stuId,
stu_name as stuName,
stu_sex as stuSex,
stu_age as stuAge
from student_info
<trim prefix="where" prefixOverrides="and">
<if test="id!=null and id!=''">
and stu_id = #{id}
</if>
<if test="itemlist!=null and itemlist!=''">
and stu_sex in
<foreach collection="itemlist"
item="obj" index="index"
open="(" separator="," close=")">
#{obj}
</foreach>
</if>
</trim>
</select>
Map<String, Object> map = new HashMap<String, Object>();
String[] strs = {"1","0"};
map.put("id", 5);
map.put("itemlist", strs);
Student qbs = mapper.queryStudentInfoByStudent(map);
System.out.println(qbs);
日誌:
DEBUG 2018-07-09 16:19:06,754 org.apache.ibatis.logging.jdbc.BaseJdbcLogger:==> Preparing: select stu_id as stuId, stu_name as stuName, stu_sex as stuSex, stu_age as stuAge from student_info where stu_id = ? and stu_sex in ( ? , ? )
DEBUG 2018-07-09 16:19:06,786 org.apache.ibatis.logging.jdbc.BaseJdbcLogger:==> Parameters: 5(Integer), 1(String), 0(String)
DEBUG 2018-07-09 16:19:06,804 org.apache.ibatis.logging.jdbc.BaseJdbcLogger:<== Total: 1
bind元素
定義一個元素,在上下文中使用
<select id="queryStudentInfoByStudent" resultType="student" parameterType="map">
<bind name="pattern_stuName" value="'%'+stuName+'%'"/>
select stu_id as stuId,
stu_name as stuName,
stu_sex as stuSex,
stu_age as stuAge
from student_info
<trim prefix="where" prefixOverrides="and">
<if test="stuName!=null and stuName!=''">
and stu_name like #{pattern_stuName}
</if>
</trim>
</select>
相關文章
- mybatis動態SQLMyBatisSQL
- MyBatis 動態 SQLMyBatisSQL
- MyBatis系列(七):MyBatis動態Sql之choose,where,set標籤的用法MyBatisSQL
- 1 Mybatis動態SQLMyBatisSQL
- Mybatis--動態SQLMyBatisSQL
- Mybatis-06 動態SqlMyBatisSQL
- mybatis動態sql總結MyBatisSQL
- MyBatis從入門到精通(七):MyBatis動態Sql之choose,where,set標籤的用法MyBatisSQL
- mybatis動態sql與分頁MyBatisSQL
- Java-Mybatis動態SQL整理JavaMyBatisSQL
- Mybatis介紹之 動態SQLMyBatisSQL
- MyBatis對動態SQL的支援MyBatisSQL
- MyBatis框架之SQL對映和動態SQLMyBatis框架SQL
- Mybatis 動態執行SQL語句MyBatisSQL
- 【MyBatis學習總結 (五),動態SQL】MyBatisSQL
- Mybatis where 1=1 動態sql問題MyBatisSQL
- MyBatis 動態 SQL 最全教程,這樣寫 SQL 太優雅了!MyBatisSQL
- Mybatis(一)Porxy動態代理和sql解析替換MyBatisSQL
- MyBatis標籤實現的動態SQL語句MyBatisSQL
- MyBatis從入門到精通(六):MyBatis動態Sql之if標籤的用法MyBatisSQL
- MyBatis從入門到精通(八):MyBatis動態Sql之foreach標籤的用法MyBatisSQL
- MyBatis進階--介面代理方式實現Dao 和動態SQLMyBatisSQL
- 好程式設計師分享MyBatis之動態SQL語句程式設計師MyBatisSQL
- Mybatis 裡對映檔案的動態 SQL 語句,實現if,where,foreache的SQL語句動態拼接查詢MyBatisSQL
- mybatis條件判斷及動態sql的簡單擴充MyBatisSQL
- 好程式設計師Java分享Mybatis必會的動態SQL程式設計師JavaMyBatisSQL
- Mybatis連線池_動態sql語句_多表查詢實現MyBatisSQL
- 學習MyBatis必知必會(7)~註解開發、動態SQLMyBatisSQL
- Mybatis中Foreach動態SQL標籤(map和list兩種情況)MyBatisSQL
- MybBatis動態SQLBATSQL
- Mybatics動態sqlBATSQL
- Mybatis系列全解(八):Mybatis的9大動態SQL標籤你知道幾個?提前致女神!MyBatisSQL
- 自動生成Sql--基於Mybatis的單表SqlSQLMyBatis
- MyBatis動態代理執行原理MyBatis
- 靜態代理、動態代理與Mybatis的理解MyBatis
- MyBatis筆記(七)MyBatis筆記
- SQL 改寫系列七:謂詞移動SQL
- 淺析MyBatis的動態代理原理MyBatis