mapper中的常用标签和属性
#{}和${}在sql中的作用
#{}会自动在你要插入字段两端 加上引号。例如:你写的是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】 |
<resultMap id="BaseResultMap" type="com.example.knf4j.common.domain.DO.HumanInfo">
<result column="pic_id" jdbcType="BIGINT" property="picId" />
<result column="jacket_color" jdbcType="VARCHAR" property="jacketColor" />
</resultMap>
<select id="getHumanById" resultMap="BaseResultMap" resultType="com.example.knf4j.common.domain.DO.HumanInfo" parameterType="java.util.Map">
SELECT pic_id, jacket_color
FROM tb_human_pic as a inner join tb_shijiedevice as b
<where>
a.device_ipv6=b.ip
<if test="picId != null and pidId != '' ">
and pic_id = #{picId}
</if>
</where>
</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标记的功能。
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
update user
<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 >
< choose> : 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>
and gender= #{gender}
</otherwise>
</choose>
</select>
易错点(了解)
mybatis 的xml文件中对应关系,如果包含一对一和一对多,那么一对一的标签association必须放在collection前面,resultMap内的标签的都是有顺序的。
- 关联 association 一对一
- 集合 collection 一对多