MyBatis 动态SQL

动态SQL中的元素

动态SQL是 MyBatis的强大特性之一,MyBatis 3采用了功能强大的基于OGNL的表达过来完成动态SQL。MyBatis动态SQL中的主要元素如下所示。

  • :判断语句,用于单条件分支判断。
  • (、< otherwise>):用于多条件分支判断。
  • < where>、:辅助元素,用于处理一些SQL拼装、特殊字符问题。
  • :循环语句,常用于in语句等列举条件中。
  • :从OGNL表达式中创建一个变量,并将其绑定到上下文,常用于模糊查询的sql中。

元素

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
	"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ssm.mapper.UserMapper">
	<!--<if>元素使用 -->
	  <select id="findUserByNameAndJobs" parameterType="com.ssm.po.User"
      resultType="com.ssm.po.User">
			select * from t_user where 1=1
		<if test="username !=null and username !=''">
			and username like concat('%',#{username},'%')
		</if>
		<if test="jobs !=null and jobs !=''">
			and jobs=#{jobs}
		</if>
  </select>
</mapper>

元素

<!--<choose>(<when>、<otherwise>)元素使用 -->
	<select id="findUserByNameOrJobs" parameterType="com.ssm.po.User"
          resultType="com.ssm.po.User">
			select * from t_user where 1=1
		<choose>
			<when test="username !=null and username !=''">
				and username like concat('%',#{username},'%')
			</when>
			<when test="jobs !=null and jobs !=''">
				and jobs=#{jobs}
			</when>		
			<otherwise>
				and phone is not null
			</otherwise>
		</choose>
	</select>

元素

映射文件中编写的SQL后面都加入了“where1=1”的条件,是为了保证当条件不成立时,拼接起来的SQL语句在执行时不会报错,即使得SQL不出现语法错误。那么在 MyBatis中,有没有什么办法不用加入“1=1”这样的条件,也能使拼接后的SQL成立呢?针对这种情况, MyBatis提供了< where>元素来处理这样的问题。

<!--<if>、<where>元素使用 -->
	<select id="findUserByNameAndJobs" parameterType="com.ssm.po.User"
     resultType="com.ssm.po.User">
		select * from t_user 
	   <where>
		<if test="username !=null and username !=''">
			and username like concat('%',#{username},'%')
		</if>
		<if test="jobs !=null and jobs !=''">
			and jobs=#{jobs}
		</if>
	  </where>
	</select>

<!--<if>、<trim>元素使用 -->
	<select id="findUserByNameAndJobs" parameterType="com.ssm.po.User"
     resultType="com.ssm.po.User">
		select * from t_user 
	   <trim prefix="where" prefixOverrides="and">
		<if test="username !=null and username !=''">
			and username like concat('%',#{username},'%')
		</if>
		<if test="jobs !=null and jobs !=''">
			and jobs=#{jobs}
		</if>
	  </trim>
	</select>

元素

元素主要用于更新操作,其主要作用是在动态包含的SQL语句前输出一个SET关键字,并将SQL语句中最后一个多余的逗号去除

<!-- <set>元素 -->
	<update id="updateUser" parameterType="com.ssm.po.User">
		update t_user
		<set>
			<if test="username !=null and username !=''">
				username=#{username}
			</if>
			<if test="jobs !=null and jobs !=''">
				jobs=#{jobs}
			</if>
			<if test="phone !=null and phone !=''">
				phone=#{phone}
			</if>
		</set>
		where id=#{id}
	</update>

元素

数组和集合循环遍历的方式,那就是使用< foreach>元素

<!--<foreach>元素使用 -->
	<select id="findUserByIds" parameterType="List" resultType="com.ssm.po.User">
			select * from t_user where id in
		<foreach item="id" index="index" collection="list" open="(" separator="," close=")">
		   #{id}
		</foreach>
	</select>

元素

防止SQL注入问题

<!--<bind>元素的使用:根据用户姓名模糊查询用户信息 -->
	<select id="findUserByName2" parameterType="com.ssm.po.User"
        resultType="com.ssm.po.User">
		<!--_parameter.getUsername()也可以直接写成传入的字段属性名,即username  -->
		<bind name="p_username" value="'%'+_parameter.getUsername()+'%'"/>
		select * from t_user 
		where username like #{p_username}
	</select>
原文地址:https://www.cnblogs.com/zq98/p/13184480.html