好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

mybatis中mapper.xml文件的常用属性及标签讲解

${}和#{}的区别

#{}会自动在你要插入字段两端 加上引号。例如:你写的是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】

常见标签

< sql >标签

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

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

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

?

1

2

3

4

5

6

7

8

9

< 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=" 条件字符串 ",判断是否满足条件,满足则执行,不满足则跳过。

?

1

2

3

4

5

6

7

8

< 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中使用。

?

1

2

3

4

5

6

7

8

< 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语句如下:

?

1

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

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

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

< trim>标签

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

示例1:

?

1

2

3

4

5

 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:

?

1

2

3

4

5

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 >标签

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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

< 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 > 

mybatis 的xml文件中标签错误

mybatis 的xml文件中对应关系,如果包含一对一和一对多,那么一对一的标签association必须放在collection前面,resultMap内的标签的都是有顺序的。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

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

查看更多关于mybatis中mapper.xml文件的常用属性及标签讲解的详细内容...

  阅读:42次