Mybatis_动态sql

Mybatis提供动态sql【基于OGNL表达式】,简化拼装sql的操作。

  • sql include
  • where if
  • set if
  • trim
  • choose when otherwise
  • foreach

  • sql include 【抽取、引用重用sql】
<sql id="insertColumn">
	last_name,email,gender
</sql>

<!-- public void insertEmployee (Employee employee); -->
<insert>
	insert into tbl_employee(<include refid="insertColumn"></include>) 
	values(#{lastName},#{email},#{gender})
</insert>

  • where if 【封装查询条件,去除语句前面多余的and】
<!-- public List<Employee> getEmpsByConditionIf(Employee employee); -->
<select id="getEmpsByConditionIf" resultType="com.tt.mybatis.bean.Employee">
	select * from tbl_employee
	<where>
		<if test="id!=null">
			id=#{id}
		</if>
		<if test="lastName!=null and lastName!=''">
			and last_name like #{lastName}
		</if>
		<if test="email!=null &amp;&amp;email.trim()!=&quot;&quot;">
			and email=#{email}
		</if> 
		<if test="gender==0 or gender==1">
		 	and gender=#{gender}
		</if>
	</where>
</select>
 <!-- 
 	1.ognl特殊字符 &&"" 这些需要转义;ognl会进行字符串与数字的转换判断  "0"==0。
 -->
  • set if【封装修改条件,去除语句后面多余的逗号】
<!--public void updateEmp(Employee employee);  -->
<update id="updateEmp">
	update tbl_employee 
	<set>
		<if test="lastName!=null">
			last_name=#{lastName},
		</if>
		<if test="email!=null">
			email=#{email},
		</if>
		<if test="gender!=null">
			gender=#{gender}
		</if>
	</set>
	where id=#{id} 
</update>
  • trim【字符串拼接,可以用来替代where和set】
<!-- 	
		trim标签体中是整个字符串拼串后的结果。
	 	prefix="":	拼串后的整个字符串加一个前缀 
	 	prefixOverrides="":	去掉整个字符串前面多余的字符
	 	suffix="":	给拼串后的整个字符串加一个后缀 
	 	suffixOverrides=""	去掉整个字符串后面多余的字符
-->
<!--public List<Employee> getEmpsByConditionTrim(Employee employee);  -->
<select id="getEmpsByConditionTrim" resultType="com.tt.mybatis.bean.Employee">
 	select * from tbl_employee
 	<trim prefix="where" suffixOverrides="and">
 		<if test="id!=null">
	 		id=#{id} and
	 	</if>
	 	<if test="lastName!=null &amp;&amp; lastName!=&quot;&quot;">
	 		last_name like #{lastName} and
	 	</if>
	 	<if test="email!=null and email.trim()!=&quot;&quot;">
	 		email=#{email} and
	 	</if> 
	 	<if test="gender==0 or gender==1">
	 	 	gender=#{gender}
	 	</if>
	 </trim>
 </select>
 
<!--public void updateEmp(Employee employee);  -->
<update id="updateEmp">
update tbl_employee 
	<trim prefix="set" suffixOverrides=",">
		<if test="lastName!=null">
			last_name=#{lastName},
		</if>
		<if test="email!=null">
			email=#{email},
		</if>
		<if test="gender!=null">
			gender=#{gender}
		</if>
	</trim>
	where id=#{id}
</update>
  • choose when otherwise 【类似switch case break】
<!-- public List<Employee> getEmpsByConditionChoose(Employee employee); -->
<select id="getEmpsByConditionChoose" resultType="com.tt.mybatis.bean.Employee">
	select * from tbl_employee 
	<where>
		<!-- 如果带了id就用id查,如果带了lastName就用lastName查;只会进入其中一个 -->
		<choose>
			<when test="id!=null">
				id=#{id}
			</when>
			<when test="lastName!=null">
				last_name like #{lastName}
			</when>
			<when test="email!=null">
				email = #{email}
			</when>
			<otherwise>
				gender = 0
			</otherwise>
		</choose>
	</where>
</select>
  • foreach 【遍历集合/数组,批量操作】
<!--
	collection:指定要遍历的集合:
	item:将当前遍历出的元素赋值给指定的变量
	separator:每个元素之间的分隔符
	open:遍历出所有结果拼接一个开始的字符
	close:遍历出所有结果拼接一个结束的字符
	index:索引。遍历list的时候是index就是索引,item就是当前值
			   遍历map的时候index表示的就是map的key,item就是map的值
	#{变量名}就能取出变量的值也就是当前遍历出的元素
 -->
 
<!--public List<Employee> getEmpsByConditionForeach(List<Integer> ids);  -->
<select id="getEmpsByConditionForeach" resultType="com.tt.mybatis.bean.Employee">
	select * from tbl_employee
	<foreach collection="ids" item="item_id" separator="," 
			 open="where id in(" close=")">
		#{item_id}
	</foreach>
</select>

<!-- 批量保存 -->
<!--public void addEmps(@Param("emps")List<Employee> emps);  -->
<!--
	方式一:
	MySQL下批量保存:可以foreach遍历   mysql支持values(),(),()语法
-->
<insert id="addEmps">
	insert into tbl_employee(last_name,email,gender,d_id)	
	values
	<foreach collection="emps" item="emp" separator=",">
		(#{emp.lastName},#{emp.email},#{emp.gender},#{emp.dept.id})
	</foreach>
</insert>

<!-- 
	方式二:
	这种分号分隔多个sql,可以用于其他的批量操作(删除,修改) 
	这种方式需要修改数据库连接属性 
 	jdbc.url=jdbc:mysql://localhost:3306/mybatis?allowMultiQueries=true
-->
<insert id="addEmps">
	<foreach collection="emps" item="emp" separator=";">
		insert into tbl_employee(last_name,email,gender,d_id)
		values(#{emp.lastName},#{emp.email},#{emp.gender},#{emp.dept.id})
	</foreach>
</insert>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值