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基礎:MyBatis動態SQL(3)MyBatisSQL
- mybatis動態sql總結MyBatisSQL
- Mybatis-06 動態SqlMyBatisSQL
- Mybatis 動態 SQL 詳解MyBatisSQL
- MyBatis4:動態SQLMyBatisSQL
- MyBatis從入門到精通(七):MyBatis動態Sql之choose,where,set標籤的用法MyBatisSQL
- Mybatis介紹之 動態SQLMyBatisSQL
- Java-Mybatis動態SQL整理JavaMyBatisSQL
- MyBatis對動態SQL的支援MyBatisSQL
- mybatis動態sql與分頁MyBatisSQL
- MyBatis框架之SQL對映和動態SQLMyBatis框架SQL
- Mybatis 動態執行SQL語句MyBatisSQL
- Mybatis深入瞭解(五)----動態SQLMyBatisSQL
- 【MyBatis學習總結 (五),動態SQL】MyBatisSQL
- Mybatis where 1=1 動態sql問題MyBatisSQL
- Mybatis學習筆記(6)-動態SQLMyBatis筆記SQL
- mybatis入門基礎(五)----動態SQLMyBatisSQL
- MyBatis學習 之 三、動態SQL語句MyBatisSQL
- Mybatis(一)Porxy動態代理和sql解析替換MyBatisSQL
- MyBatis標籤實現的動態SQL語句MyBatisSQL
- MyBatis 動態 SQL 最全教程,這樣寫 SQL 太優雅了!MyBatisSQL
- MyBatis從入門到精通(六):MyBatis動態Sql之if標籤的用法MyBatisSQL
- MyBatis引數傳入集合之foreach動態sqlMyBatisSQL
- 好程式設計師分享MyBatis之動態SQL語句程式設計師MyBatisSQL
- MyBatis進階--介面代理方式實現Dao 和動態SQLMyBatisSQL
- MyBatis從入門到精通(八):MyBatis動態Sql之foreach標籤的用法MyBatisSQL
- 好程式設計師Java分享Mybatis必會的動態SQL程式設計師JavaMyBatisSQL
- Mybatis連線池_動態sql語句_多表查詢實現MyBatisSQL
- mybatis條件判斷及動態sql的簡單擴充MyBatisSQL
- 動態SQLSQL
- 動態SQL intoSQL
- Mybatis 裡對映檔案的動態 SQL 語句,實現if,where,foreache的SQL語句動態拼接查詢MyBatisSQL
- 學習MyBatis必知必會(7)~註解開發、動態SQLMyBatisSQL