mybatis在mapper映射中的标签

1.${}和#{}的区别

#{}会自动在你要插入字段两端 加上引号。例如:你写的是order by #{username},传的是 zhangsan,那么会解析成order by “zhangsan”。

${}是将传入的数据直接显示生成在sql中。 如:order by ${user_id},如果传入的值是111,那么解析成sql时的值为order by 111 如果传入的值是id,则解析成的sql为order by id.

#{}: 解析为一个 JDBC 预编译语句(prepared statement)的参数标记符,一个 #{ } 被解析为一个参数占位符 。$ {}: 仅仅为一个纯碎的 string 替换,在动态 SQL 解析阶段将会进行变量替换。在使用order by 时,就需要使用$;

属性作用
namespace对应接口的路径
id表示此段sql执行语句的唯一标识,也是接口的方法名称【必须一致才能找到方法】
parameterType表示该sql语句中需要传入的参数, 类型要与对应的接口方法的类型一致【可选】
resultMap定义出参,调用已定义的映射管理器的id值
resultType定义出参,匹配普通Java类型或自定义的pojo【出参类型若不指定,将为语句类型默认类型,如语句返回值为int】

2.常见标签

< sql >标签

该标签主要定义复用的sql语句片段,在执行的sql语句标签直接引用即可。可以提高编码效率、简化代码和提高可读性。

需要配置id熟悉,表示该sql片段的唯一标识。

引用:通过<include refid=" " / >标签引用,refid的值就是< sql>的id属性的值。

<sql id="Base_Column_List">
    id, question, answer 
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from java
    where id = #{id,jdbcType=BIGINT}
  </select> 

< where >和< if >标签

< where > : 主要用来替换sql语句中的where字段,他的作用主要是用来简化sql语句中where条件判断的书写的

< if >:条件判断标签,配置属性test=" 条件字符串 ",判断是否满足条件,满足则执行,不满足则跳过。

<select id="selectByParams" parameterType="map" resultType="user">
   select * from user
   <where>
     <if test="id != null ">id=#{id}</if>
     <if test="name != null and name.length()>0" >and name=#{name}</if>
     <if test="age != null and age.length()>0">and age = #{age}</if>
   </where>
</select>   

如果当id值为空时,此时打印的sql应是:select * from user where name=“xx” and age=“xx”

where 标记会自动将其后第一个条件的and或者是or给忽略掉

< set >标签

< set > : 主要用来替换sql语句中的set字段,一般在update中使用。

<update>
  update user 
   <set>
     <if test="name != null and name.length()>0">name = #{name},</if>
     <if test="age != null and age .length()>0">age = #{age },</if>
   </set>
   where id = #{id}
</update> 

在上述的代码片段当中,假如说现在三个字段都有值得话,那么上面打印的SQL语句如下:

update user set name=‘xxx’ , age=‘xx’ where id=‘x’

在上面age="xx"的后是没有逗号的,也就是说set标记已经自动帮助我们把最后一个逗号给去掉了

set 标记会自动将其后第一个条件后的逗号忽略掉

< trim>标签

< trim > : 是一个格式化的标记,可以完成set或者是where标记的功能。

示例1:

select * from user 
  <trim prefix="WHERE" prefixoverride="AND |OR">
    <if test="name != null and name.length()>0"> AND name=#{name}</if>
    <if test="age != null and age.length()>0"> AND age=#{age}</if>

  </trim>

假如说name和age的值都不为null的话打印的SQL为:select * from user where name = ‘xx’ and age = ‘xx’

在where的后面是不存在第一个and的,上面两个属性的意思如下:
  prefix:前缀      
  prefixoverride:去掉第一个and或者是or

示例2:

  <trim prefix="set" suffixoverride="," suffix=" where id = #{id} ">
    <if test="name != null and name.length()>0"> name=#{name} , </if>
    <if test="age!= null and age.length()>0"> age=#{age} ,  </if>
  </trim>

假如说name和age的值都不为null的话打印的SQL为:update user set name=‘xx’ , age=‘xx’ where id=‘x’

在age='xx’的后面不存在逗号,而且自动加了一个set前缀和where后缀,上面三个属性的意义如下,其中prefix意义如上:
   suffixoverride:去掉最后一个逗号(也可以是其他的标记,就像是上面前缀中的and一样)
   suffix:后缀

< choose >标签

< where > : choose标签是按顺序判断其内部when标签中的test条件出否成立,如果有一个成立,则 choose 结束。当 choose 中所有 when 的条件都不满则时,则执行 otherwise 中的sql。类似于Java 的 switch 语句,choose 为 switch,when 为 case,otherwise 则为 default。

<select id="selectByParams" parameterType="map" resultType="user">
    select * from user where 1 = 1
        <choose>  
	            <when test="id !=null ">  
	                AND id = #{id}
	            </when >  
	            <when test="username != null and username != '' ">  
	                AND username = #{username}  
	            </when >  
	            <when test="age != null and age !=''">  
	                AND age = #{age}  
	            </when >  
	            <otherwise>  
	            </otherwise>  
       		 </choose>
  </select> 

————————————————
原文链接:https://blog.csdn.net/weixin_43882997/article/details/85625805

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值