MyBatis学习三:动态SQL

  1. 动态 SQL是MyBatis强大特性之一。极大的简化拼装SQL的操作
  2. 动态 SQL 元素和使用 JSTL 或其他类似基于 XML 的文本处理器相似
  3. MyBatis 采用功能强大的基于 OGNL 的表达式来简化操作

 

1.if

   <if test=" ">  </if>:通过test表达式,拼接SQL。

	<select id="getEmpListByMoreTJ1" resultType="Emp">
		select eid,ename,age,sex,did from emp 
            where 1=1
			<if test="eid != null">
				and eid = #{eid}
			</if>
			<if test="ename != null and ename != ''">
				and ename = #{ename}
			</if>
			<if test="age != null">
				and age = #{age}
			</if>
			<if test="sex == 1 or sex == 0">
				and sex = #{sex}
			</if>
		
	</select>


多条件查询,如果页面中没有设置此条件,SQL语句中一种不能有该条件。  

<if test="sex=='男'  or sex==‘女’">会报错,==的比较只能是数值,

2.where

  添加where关键字,并且去掉多余的and

<select id="getEmpListByMoreTJ1" resultType="Emp">
		select eid,ename,age,sex,did from emp 
		<where>
			<if test="eid != null">
				and eid = #{eid}
			</if>
			<if test="ename != null and ename != ''">
				and ename = #{ename}
			</if>
			<if test="age != null">
				and age = #{age}
			</if>
			<if test="sex == 1 or sex == 0">
				and sex = #{sex}
			</if>
		</where>
</select>
	

3.trim

    截取并拼接  <trim prefix="" suffix="" prefixOverrides="" suffixOverrides="">:
        prefix:在操作的SQL语句前加入某些内容
        suffix:在操作的SQL语句后加入某些内容
        prefixOverrides:把操作的SQL语句前的某些内容去掉
        suffixOverrides:把操作的SQL语句后的某些内容去掉

<sql id="empColumns">select eid,ename,age,sex,did from emp </sql>

<select id="getEmpListByMoreTJ" resultType="Emp">
		<include refid="empColumns"></include>
		<trim prefix="where" suffixOverrides="and|or">
			<if test="eid != null">
				eid = #{eid} and
			</if>
			<if test="ename != null and ename != ''">
				ename = #{ename} and 
			</if>
			<if test="age != null">
				age = #{age} or 
			</if>
			<if test="sex == 1 or sex == 0">
				sex = #{sex} 
			</if>
		</trim>
	</select>



<sql id=""></sql>:设置一段SQL片段,即公共SQL,可以被当前映射文件中所有的SQL语句所访问

4.set

   set 主要是用于解决修改操作中SQL语句中可能多出逗号的问题

<update id="updateEmpByConditionSet">
		update  tbl_employee  
		<set>
			<if test="lastName!=null &amp;&amp; lastName!=&quot;&quot;">
				 last_name = #{lastName},
			</if>
			<if test="email!=null and email.trim()!=''">
				 email = #{email} ,
			</if>
			<if test="&quot;m&quot;.equals(gender) or &quot;f&quot;.equals(gender)">
				gender = #{gender} 
			</if>
		</set>
		 where id =#{id}
</update>

5.choose

<choose>:选择某一个when或otherwise拼接SQL》》》》类似于switch-case。
            <when test=""></when>:通过test表达式拼接SQL
            .......
            <otherwise></otherwise>:当when都不符合条件,就会选择otherwise拼接SQL
</choose>

	<select id="getEmpListByChoose" resultType="Emp">
		select eid,ename,age,sex from emp 
		where 
		<choose>
			<when test="eid != null">
				eid = #{eid}
			</when>
			<when test="ename != null and ename != ''">
				ename = #{ename}
			</when>
			<when test="age != null">
				age = #{age}
			</when>
			<otherwise>
				sex = #{sex}
			</otherwise>
		</choose>
	</select>
	

多条件查询,不断往SQL里面拼接信息,choose-when只往里面添加一个条件查询。

6.foreach

<foreach collection="" item="" close="" open="" separator="" index=""></foreach>
        对一个数组或集合进行遍历
        collection:指定要遍历的集合或数组, list,或者array
        item:设置别名
        close:设置循环体的结束内容
        open:设置循环体的开始内容
        separator:设置每一次循环之间添加的分隔符
        index:若遍历的是list,index代表下标;若遍历的是map,index代表键

批量操作


		delete:
			delete from emp where eid in ();
			delete from emp where eid = 1 or eid = 2 or eid = 3 
		select:
			select * from emp where eid in ();
			select * from emp where eid = 1 or eid = 2 or eid = 3 
		update:
			把每条数据修改为相同内容
			update emp set ... where eid in ();
			update emp set ... where eid = 1 or eid = 2 or eid = 3
			***********把每条数据修改为对应内容,注意:必须在连接地址(url)后添加参数?allowMultiQueries=true;也就是传递多条SQL语句
			update emp set ... where eid = 1;
			update emp set ... where eid = 2;
			update emp set ... where eid = 3
		insert
			insert into emp values(),(),()
//*****批量增加员工 
//     1.Mapper接口中的方法:
void insertMoreByArray(Emp[] emps);

//     2.Mapper.xml配置文件中的SQL映射
<insert id="insertMoreByArray">
	 	insert into emp values
	 	<foreach collection="emps" item="emp" separator=",">
	 		(null,#{emp.ename},#{emp.age},#{emp.sex},1)
	 	</foreach>
</insert>



//*****批量修改员工信息
//     1.Mapper接口中的方法:
void updateMoreByArray(Emp[] emps)

//     2.Mapper.xml配置文件中的SQL映射
<update id="updateMoreByArray">
	 	<foreach collection="emps" item="emp">
	 		update emp set ename = #{emp.ename}, age = #{emp.age}, sex = #{emp.sex} where eid = #{emp.eid};
	 	</foreach>
</update>
	 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值