動(dòng)態(tài)SQL
動(dòng)態(tài)SQL語句是動(dòng)態(tài)的拼接Mybatis中SQL語句的情況,可以動(dòng)態(tài)的在Mybatis中使用SQL
if語句
if語句的xml文件:
<!-- List<Emp> getEmpByCondition(Emp emp);-->
<select id="getEmpByCondition" resultType="Emp">
select * from t_emp where 1 = 1
<if test="empName != null and empName != ''">
emp_name = #{empName}
</if>
<if test="sex != null and sex != ''">
and sex = #{sex}
</if>
<if test="age != null and age != ''">
and age = #{age}
</if>
<if test="email != null and email != ''">
and email = #{email}
</if>
</select>
傳入對(duì)象來進(jìn)行調(diào)用:
@Test
public void test() {
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
DynamicSQLMapper mapper = sqlSession.getMapper(DynamicSQLMapper.class);
List<Emp> empList = mapper.getEmpByCondition(new Emp(null, "", 23, "男", "123@qq.com", null));
System.out.println(empList);
}
WHERE語句
where標(biāo)簽中的and會(huì)被自動(dòng)去掉,并且若沒有合適的內(nèi)容,則不會(huì)添加where關(guān)鍵字
注意:where標(biāo)簽只能去掉條件前的and、五福去掉條件后的and
<select id="getEmpByCondition" resultType="Emp">
select * from t_emp
<where>
<if test="empName != null and empName != ''">
emp_name = #{empName}
</if>
<if test="sex != null and sex != ''">
and sex = #{sex}
</if>
<if test="age != null and age != ''">
and age = #{age}
</if>
<if test="email != null and email != ''">
and email = #{email}
</if>
</where>
</select>
trim標(biāo)簽
trim標(biāo)簽會(huì)在其內(nèi)容中沒有合適的內(nèi)容時(shí)不添加其內(nèi)容,若trim標(biāo)簽內(nèi)有內(nèi)容時(shí),則會(huì)添加這個(gè)內(nèi)容
<!-- 如果trim中的if有一個(gè)成立,就會(huì)把where添加進(jìn)去,如果if語句后沒有語句,則會(huì)將末尾的and或or去掉-->
<select id="getEmpByCondition" resultType="Emp">
select * from t_emp
<trim prefix="where" suffixOverrides="and|or">
<if test="empName != null and empName != ''">
emp_name = #{empName} and
</if>
<if test="sex != null and sex != ''">
sex = #{sex} or
</if>
<if test="age != null and age != ''">
age = #{age} and
</if>
<if test="email != null and email != ''">
email = #{email}
</if>
</trim>
</select>
choose、when、otherwise
choose是when和otherwise的父標(biāo)簽,when和otherwise要寫在choose中,相當(dāng)于編程語言中的if…else…if…else…
<!-- List<Emp> getEmpByChoose(Emp emp);-->
<select id="getEmpByChoose" resultType="Emp">
select * from t_emp
<where>
<choose>
<when test="empName != null and empName != ''">
emp_name = #{empName}
</when>
<when test="sex != null and sex != ''">
sex = #{sex}
</when>
<otherwise>
did = 1
</otherwise>
</choose>
</where>
</select>
注意這里when至少有一個(gè)、otherwise至多有一個(gè),也可以沒有
foreach
通過數(shù)組實(shí)現(xiàn)批量刪除
mapper接口:
/**
* 通過數(shù)組實(shí)現(xiàn)批量刪除(形參傳入數(shù)組)
*/
int deleteMoreByArray(@Param("eids") Integer[] eids);
xml:
<!-- int deleteMoreByArray(Integer[] eids);-->
<!-- foreach標(biāo)簽中,collection代表要遍歷的數(shù)組名,item代表每次遍歷得到的數(shù)據(jù),separator代表每個(gè)數(shù)據(jù)之間的分隔,open
代表遍歷前添加,close代表在遍歷之后添加-->
<delete id="deleteMoreByArray">
DELETE from t_emp where eid in
<foreach collection="eids" item="eid" separator="," open="(" close=")">
#{eid}
</foreach>
</delete>
xml的第二種寫法:
<delete id="deleteMoreByArray">
delete from t_emp where
<foreach collection="eids" item="eid" separator="or">
eid = #{eid}
</foreach>
</delete>
通過集合實(shí)現(xiàn)批量添加
建立接口:
/**
* 通過list實(shí)現(xiàn)批量添加
*/
int insertMoreByList(@Param("emps") List<Emp> list);
建立xml:
<!-- int insertMoreByList(List<Emp> list);-->
<insert id="insertMoreByList">
insert into t_emp values
<foreach collection="emps" item="emp" separator=",">
(null, #{emp.empName}, #{emp.age}, #{emp.sex}, #{emp.email}, null)
</foreach>
</insert>
sql標(biāo)簽
sql標(biāo)簽用來將常用的一些字段記錄,下次在有這些字段出現(xiàn)時(shí)可以簡易的替換這些字段:文章來源:http://www.zghlxwxcb.cn/news/detail-707025.html
使用include標(biāo)簽來引用定義的sql片段文章來源地址http://www.zghlxwxcb.cn/news/detail-707025.html
<sql id="empColumn">eid, emp_name, age, sex, email</sql>
<select id="getEmpByConditionOne" resultType="Emp">
select <include refid="empColumn"></include> from t_emp
</select>
到了這里,關(guān)于【MyBatis】四、MyBatis中的動(dòng)態(tài)SQL標(biāo)簽的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!