前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Mybatis动态SQL

Mybatis动态SQL

作者头像
Java开发者之家
发布2021-06-17 16:54:47
5770
发布2021-06-17 16:54:47
举报
文章被收录于专栏:Java开发者之家Java开发者之家

# Mybatis动态SQL

# if判断条件

代码语言:javascript
复制
<!-- public List<Employee> getEmpsByConditionIf(Employee employee); -->
	 
	 <select id="getEmpsByConditionIf" resultType="com.finen.mybatis.bean.Employee">
	 	select * from tbl_employee
	 	<where>
	 		<!-- test:判断表达式; c:if test 
	 		从参数中取值进行判断
		 	-->
		 	<if test="id != null">
		 		id=#{id}
		 	</if>
		 	<if test="lastName != null and lastName != ''">
		 		and last_name like #{lastName}
		 	</if>
		 	<if test="email != null and email.trim()!=''">
		 		and email=#{email}
		 	</if>
		 	<if test="gender == 0 or gender == 1">
		 		and gender=#{gender}
		 	</if>
	 	</where>
	 </select>

1

# trim

代码语言:javascript
复制
<!-- public List<Employee> getEmpsByConditionTrim(Employee employee); -->
	 <select id="getEmpsByConditionTrim" resultType="com.finen.mybatis.bean.Employee">
	 	select * from tbl_employee
	 	<!-- 后面多出的and或者or where不能解决 -->
	 	<!-- 
	 		prefix="" :前缀,trim标签体中是整个字符拼串后的结果
	 				prefix给拼串后的整个字符串加一个前缀
	 		prefixOverrides="" 
	 				前缀覆盖:去掉整个字符串前面多余的字符
	 		suffix="" 后缀
	 				suffix给拼串后的整个字符串多余的字符
	 		suffixOverrides=""
	 				后缀覆盖:去掉整个字符串后面多余的字符
	 	 -->
	 	 <!-- 自定义字符串的截取规则 -->
	 	<trim prefix="where" suffixOverrides="and">
	 		<!-- test:判断表达式; c:if test 
	 		从参数中取值进行判断
		 	-->
		 	<if test="id != null">
		 		id=#{id} and
		 	</if>
		 	<if test="lastName != null and lastName != ''">
		 		last_name like #{lastName} and
		 	</if>
		 	<if test="email != null and email.trim()!=''">
		 		email=#{email} and
		 	</if>
		 	<if test="gender == 0 or gender == 1">
		 		gender=#{gender}
		 	</if>
	 	</trim>
	 </select>

# choose选择判断

代码语言:javascript
复制
<!-- public List<Employee> getEmpsByConditionChoose(Employee employee); -->
	 <select id="getEmpsByConditionChoose" resultType="com.finen.mybatis.bean.Employee">
	 	select * from tbl_employee
	 	<where>
	 		<!-- 如果带了id就用id查,如果带了lastName就用lastName查,只会进入其中一个 -->
	 		<choose>
	 			<when test="id != null">
	 				id = #{id}
	 			</when>
	 			<when test="email != null">
	 				last_name like #{lastName}
	 			</when>
	 			<when test="lastName != null">
	 				email = #{email}
	 			</when>
	 			<otherwise>
	 				gender = 0
	 			</otherwise>
	 		</choose>
	 	</where>
	 </select>

# set标签

代码语言:javascript
复制
<!-- public void updateEmp(Employee employee); -->
	 <update id="updateEmp" >
	 	update tbl_employee
	 	
	 	
		<!-- 
		Set
			<set>
				<if test="lastName != null">
					last_name = #{lastName},
				</if>
				<if test="email != null">
					email = #{email},
				</if>
				<if test="gender != null">
					gender = #{gender}
				</if>
			</set>
		-->
		<!-- Trim -->
		<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>

# Foreach

代码语言:javascript
复制
<!-- 动态SQL-Foreach -->
	 <!-- public List<Employee> getEmpsByConditionForeach(List<Integer> list); -->
	 <select id="getEmpsByConditionForeach" resultType="com.finen.mybatis.bean.Employee">
	 	select * from tbl_employee where id in
	 	<!-- 
	 		collection:指定要遍历的集合
	 			list类型的参数会特殊处理封装在map中,map的key叫list
	 		item:将遍历出来的元素赋值给指定的变量
	 		separator:每个元素之间的分隔符
	 		open:遍历出所有结果拼接拼接处一个开始字符
	 		close:遍历出所有结果拼接拼接处一个结束字符
	 		index:索引,遍历list的时候是index就是索引,item就是当前值
	 					遍历map的时候index表示map的key,item就是map的值
	 		
	 		#{变量名}就能取出变量的值也就是当前遍历出的元素
	 	 -->
	 	 <foreach collection="ids" item="item_id" separator=","
	 	 	open="("
	 	 	close=")">
	 	 	#{item_id}
	 	 
	 	 </foreach>
	 </select>

# foreach批量操作

代码语言:javascript
复制
<!-- 批量保存 -->
	 <!-- public void addEmps(@Param("emps")List<Employee> emps); -->
	 <!-- MySQL下批量保存,可以foreach遍历 -->
	 <!-- 首选方式 -->
	 <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>
	 
	 <!-- 这种方式需要数据库连接属性allowMultiQueries=true
	 		这种分号分隔多个sql可以用于其他的批量操作(删除,修改)
	  -->
	 <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>
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-02-26 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • # Mybatis动态SQL
    • # if判断条件
      • # trim
        • # choose选择判断
          • # set标签
            • # Foreach
              • # foreach批量操作
              相关产品与服务
              云数据库 MySQL
              腾讯云数据库 MySQL(TencentDB for MySQL)为用户提供安全可靠,性能卓越、易于维护的企业级云数据库服务。其具备6大企业级特性,包括企业级定制内核、企业级高可用、企业级高可靠、企业级安全、企业级扩展以及企业级智能运维。通过使用腾讯云数据库 MySQL,可实现分钟级别的数据库部署、弹性扩展以及全自动化的运维管理,不仅经济实惠,而且稳定可靠,易于运维。
              领券
              问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档