王小白 Mybatis(3)动态sql语句

到mybatis提供的动态sql演变过程
从sql语句开始到mybatis提供的自动化:

<if text="表达式">sql片段</if>

当符合表达式规定的要求时,讲sql片段拼接到sql语句当中去否则不拼接
据下列例子:

<select id="search" resultType="包名">
		SELECT * FROM 表名
		where 1 = 1
		<if test="属性名 != null">
			and 表中列名 = #{属性名}
		</if>
		<if test="属性 != null">
			and 表中列名 = #{属性名}
		</if>
</select>

其中大家可以看到中间有一句where 1=1这个语句,这句话的作用在于,如果后面的if条件的判断都不正确,这个where语句相当于没有,就搜索这个表中的所有的值。
2. <trim prefix="前缀" prefixOverrides="被覆盖的前缀" suffixOverrides="被覆盖的后缀"></trim>
prefix:如果trim标签中内容不为空串,则添加前缀,否则不添加
prefixOverrides:如果trim标签内容最前面是该属性的值,则被覆盖掉
suffixOverrides:如果trim标签内容最后面是该属性的值,则被覆盖掉
举例:

<select id="search" resultType="包名">
		SELECT * FROM 表名
		<trim prefix="where" prefixOverrides="and">
		<if test="属性名 != null">
			and 表中列名 = #{属性名}
		</if>
		<if test="属性 != null">
			and 表中列名 = #{属性名}
		</if>
		</trim>
</select>
	
<update id="update">
		update 表名
		<trim prefix="set" suffixOverrides=",">
		<if test="属性名 != null">
			and 表中列名 = #{属性名}
		</if>
		<if test="属性 != null">
			and 表中列名 = #{属性名}
		</if>
		</trim>
		where 表中列名 = #{属性名}
</update>

3.直接使用标签

**<where></where>**
代替
<trim prefix="where" prefixOverrides="and"></trim>
<select id="search" resultType="包名">
		SELECT * FROM 表名
		<where>
		<if test="属性名 != null">
			and 表中列名 = #{属性名}
		</if>
		<if test="属性 != null">
			and 表中列名 = #{属性名}
		</if>
		</where>
</select>
**<choose></choose> choose(when,otherwise)**
选择一个查询条件进行查询,类似于java中的switch语句
<select id="selectUserByChoose" resultType="包名" parameterType="参数名">
      select * from 表名
      <where>
          <choose>
              <when test="查询条件">
                  举例:例如 deptno=#{deptno}
              </when>
              <when test="查询条件">
                  ......
              </when>
              <otherwise>
                  ......
              </otherwise>
          </choose>
      </where>
  </select>
**<set></set>**
代替
<trim prefix="set" suffixOverrides=",">
<update id="update">
		update 表名
		<set>
		<if test="属性名 != null">
			and 表中列名 = #{属性名}
		</if>
		<if test="属性 != null">
			and 表中列名 = #{属性名}
		</if>
		</set>
		where 表中列名 = #{属性名}
</update>

4.处理集合和数组的便签
<foreach></foreach>
举例:批量删除

int deleteBatch(Integer[] 变量名);
<delete id="deleteBatch">
		DELETE FROM 表名 WHERE 列名 IN
		<foreach collection="array" item="循环遍历元素" open="(" close=")" separator=",">
			#{属性名}
		</foreach>
</delete>

解释以下里面的各种标签的含义
collection:list | collection | array 根据你自己的定义会自动生成。
item:引用集合或数组中当前遍历的元素
separator:分隔符
open:要添加的开始字符串
close:要添加的结束字符串

看过这个在看一个批量增加

<insert id="insertBatch">
		INSERT INTO dept VALUES
		<foreach collection="list" item="dept" separator=",">
			(#{dept.属性名},#{dept.属性名},#{dept.属性名})
		</foreach>
</insert>

5.sql片段
1.作用:用来定义可重复使用的sql代码片段,可以包含在其他sql语句当中
2.定义:举例

<sql id="select">
		select deptno,dname,loc from dept
</sql>

3.引用片段

<select id="getById" resultType="包名">
		<include refid="select"></include> WHERE deptno = #{deptno}
</select>

6.得到自动增长的值

<selectKey></selectKey>
<insert id="insert">
		<selectKey order="AFTER" keyProperty="id" resultType="int">
			select last_insert_id()
		</selectKey>
		insert into userinfo(username,password) values(#{username},#{password})
</insert>
  • order:决定了查询语句执行的先后
    • after:先执行插入语句,后执行查询主键的sql语句(mysql)
    • before:先执行查询,生成主键值,在把生成的主键值带到插入语句中(oracle,序列)
  • keyProperty:键属性名,查询得到的主键值放在形参的哪个属性中
  • resultType:查询的主键值的类型

暂时就先总结到这了,如果有补充则继续往上添加,希望大家批评指正。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值