MyBatis动态sql

一、parameterType 属性

1、作用

在XXXMapper.xml中<select><delete>等标签的 parameterType 可以控制参数类型

2、在mapper.xml中通过 #{} 获取参数
  1. 使用索引,从0开始,#{0}表示第一个参数。
  2. 使用#{param1}获取第一个参数。
  3. 如果只有一个参数 (基本类型或String类型),对#{}里面的内容没有要求,只要写内容即可。
  4. 如果参数是对象类型,写成#{属性名}。
  5. 如果参数是map类型,写成#{key}。
3、#{} 和 ${} 的区别
  1. #{}获取参数的内容支持,索引获取,param1 获取指定位置参数,并且 SQL 中使用 “?” 占位。
  2. ${}字符串拼接不使用 “?” 拼接,默认找${内容}的 get/set 方法,如果是数字,就是一个数字。
4、xml转义标签

如果在xml文件中出现 “<”、“>”、双引号等特殊字符时可以使用 xml 转义标签 <![CDTA[内容]]>

二、定义 sql 语句

1、select标签

属性介绍:

id:唯一的标识符。

parameterType:传给此语句的参数的全路径名或别名(例:com.pojo.User 或 user)。

resultType:语句返回值类型或别名。注意,如果是集合,那么这里填写的是集合的泛型,而不是集合本身(resultType 与 resultMap 不能并用)

示例 :

<resultMap type="com.pojo.User" id="mymap">
	<!-- 主键使用 id 标签配置映射关系 -->
  	<id column="id" property="id1" />
	<!-- 其他列使用 result 标签配置映射关系 -->
  	<result column="name" property="name1"/>
  	<result column="sex" property="sex1"/>
</resultMap>

<select id="selectByPrimaryKey" resultMap="myMap" parameterType="int">
    select * from user where id=#{0}
</select>
<!-- 
	resultMap:将查询结果与实体类映射 
	使用resultMap属性引用<resultMap>标签
-->
2、insert标签

属性介绍:

id:唯一的标识符。

parameterType:传给此语句的参数的全路径名或别名(例:com.pojo.User) 。

示例:

<insert id="insertUser" parameterType="com.pojo.MyUser">
    insert into user(name,sex) values(#{name},#{sex})
</insert>
3、delete标签

属性介绍:

id:唯一的标识符。

parameterType:传给此语句的参数的全路径名或别名(例:com.pojo.User) 。

示例:

<delete id="deleteUser" parameterType="Integer">
    delete from user where id=#{id}
</delete>
4、update标签

属性介绍:

id:唯一的标识符。

parameterType:传给此语句的参数的全路径名或别名(例:com.pojo.User) 。

示例:

<update id="updateUser" parameterType="com.pojo.MyUser">
    update user set uname = #{uname},usex = #{usex} where uid = #{uid}
</update>

三、resultMap标签

1、resultMap标签的使用

基本作用:

  1. 建立 SQL 查询结果字段与实体属性的映射关系。
  2. 查询结果集转换为 java 对象,方便进一步操作。
  3. 将结果集中的列与 java 对象中的属性对应起来并将值填充进去。

定义查询结果集对应关系:

<resultMap id="mymap" type="com.pojo.User">
	<!-- 主键使用 id 标签配置映射关系 -->
  	<id column="id" property="id1" />
	<!-- 其他列使用 result 标签配置映射关系 -->
  	<result column="name" property="name1"/>
  	<result column="sex" property="sex1"/>
</resultMap>

属性说明:

id:该<resultMap>标签的标志。

type:返回值的类名,此例中返回 User 类。

查询时引用<resultMap>标签:

<select id="selectByPrimaryKey" resultMap="myMap" parameterType="int">
    select * from user where id=#{0}
</select>

四、动态sql拼接

1、概述

    根据不同条件需要执行不同 sql 命令,称为动态 sql。

    MyBatis 中动态 SQL 在 mapper.xml 中添加逻辑判断等。

2、if 标签

    if 标签通常用于 WHERE 语句、UPDATE 语句、INSERT 语句中,通过判断参数值来决定是否使用某个查询条件、判断是否更新某一个字段、判断是否插入某个字段的值。

<select id="selByAccinAccout" resultType="log"> 
  	select * from log where 1=1
	<!-- OGNL 表达式,直接写 key 或对象的属性.不需要添加任何特字符号 -->
  	<if test="accin!=null and accin!=''">
      	and accin=#{accin}
  	</if>
  	<if test="accout!=null and accout!=''">
      	and accout=#{accout}
  	</if>
</select>
3、where 标签

    如果 where 标签中包含的标签中有返回值的话,它就自动插入一个 where 连接 sql 语句。如果标签返回的内容是 AND 或者 OR 开头的,则它就会被自动剔除掉。比直接使用 if 标签少写一个where 1 = 1。

<select id="selByAccinAccout" resultType="log"> 
    select * from log
    <where> 
          <if test="accin!=null and accin!=''"> 
              and accin=#{accin} 
          </if> 
          <if test="accout!=null and accout!=''">
              and accout=#{accout}
          </if>
    </where>
</select>
4、choose…when…otherwise 标签

    按顺序判断 when 中的条件出否成立,如果有一个成立,则 choose 结束。当 choose 中所有 when的条件都不满足时,则执行 otherwise 中的 sql。类似于 Java 的 switch 语句,choose 为 switch,when 为 case,otherwise 则为 default。

<select id="selByAccinAccout" resultType="log"> 
  	select * from log 
  	<where>
      	<choose>
          	<when test="accin!=null and accin!=''">
              	and accin=#{accin} 
          	</when>
          	<when test="accout!=null and accout!=''">
              	and accout=#{accout}
          	</when>
            <otherwise>
            	and id = 10
          	</otherwise>
      	</choose>
  	</where>
</select>
5、set标签

作用:

  1. 去掉最后一个逗号。
  2. 如果<set>标签里面有内容,就生成 set 关键字,没有就不生成 set 关键字。

示例:

<update id="upd" parameterType="log" >
  	update log
  	<set>
      	id=#{id},
        <if test="accIn!=null and accIn!=''">
            accin=#{accIn},
        </if>
        <if test="accOut!=null and accOut!=''">
            accout=#{accOut},
        </if>
  	</set>
  	where id=#{id}
</update>
6、trim 标签

属性介绍:

  1. prefix:在前面添加内容。
  2. prefixOverrides:去掉前面的内容。
  3. suffix:在后面添加内容。
  4. suffixOverrides:去掉后面的内容
  5. 执行顺序:去掉内容后添加内容

示例:

<update id="upd" parameterType="log">
  	update log
  	<trim prefix="set" suffixOverrides=",">
      	a=a,
  	</trim>
  	where id=100
</update>
7、bind 标签

作用:

给参数重新赋值

适用场景:

  1. 模糊查询
  2. 在原内容前后添加内容

示例:

<select id="selByLog" parameterType="log" resultType="log"> 
  	<bind name="user_name" value="'%'+accin+'%'"/>
  	select * from user where username like #{user_name}
</select>
8、foreach 标签

作用:

主要用于构建 in 条件,可以在 sql 中对集合进行迭代。也常用到批量删除、添加等操作中。

属性介绍:

  1. collection:collection 属性的值有三个分别是 list、array、map 三种,分别对应的参数类型为:List、数组、map 集合。
  2. item :表示在迭代过程中每一个元素的别名。通过 #{迭代变量名} 获取内容。
  3. index :表示在迭代过程中每次迭代到的位置(下标)。
  4. open :前缀,循环后左侧添加的内容。
  5. close :后缀,循环后右侧添加的内容。
  6. separator :分隔符,表示迭代时每个元素之间以什么分隔。

示例:

<select id="selectIn" resultMap="myMap">
    select * from user where id in
    <foreach item="item" index="index" collection="list" open="(" separator="," close=")">
        #{item}
    </foreach>
</select>
9、sql 标签

作用:

当多种类型的查询语句的查询字段或者查询条件相同时,可以将其定义为常量,方便调用。

示例:

<!-- 查询字段 -->
<sql id="user_field">
    id,name,age,sex
</sql>

<!-- 查询条件 -->
<sql id="user_field_where">
    where 1=1
    <trim suffixOverrides=",">
        <if test="id != null and id !=''">
            and id = #{id}
        </if>
        <if test="name != null and name != ''">
            and name = #{name}
        </if>
        <if test="age != null ">
            and age = #{age}
        </if>
        <if test="sex != null">
            and sex = #{sex}
        </if>
    </trim>
</sql>
10、include 标签

作用:

用于引用 sql 标签定义的常量

示例:

<!-- 按条件字段查询所有 -->
<select id="selectAll" resultMap="myMap">
    SELECT
    <include refid="user_field" />
    from user
    <include refid="user_field_where" />
</select>
11、concat函数

作用: 对于 mybatis 中的 like 关键字查询,使用like concat() 组合,可以防止SQL注入。

格式:

like concat('%/',#{dataPath,jdbcType=VARCHAR},'/%')
<!--
参数说明:
	第一个参数,就是 '%',其中 % 后边可以加上一些常量字符比如 ‘/’。
	第二个参数,传递过来的参数。
	第三个参数,是结尾的 '%',其中 % 前边可以加一些常量字符比如 ‘/’。
 	常量字符:0-9 a-z A-Z _ / ? * ,等
-->

示例:

select * from user where name like concat('%','#{username}','%')
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值