<sql>、<resultMap>、<where>、<foreach>、<trim>、<set>等标签的作用和用法
目录一. sql 代码片段标签二. resultMap 映射结果集标签三. where 条件标签四. set 修改标签五. trim 标签六. foreach 循环标签一. sql 代码片段标签sql 标签是 mybatis 框架中一个非常常用的标签页特别是当一张表很有多个字段多或者要多表查询很多字段时都会用到。拓本之际是用来定义一个SQL代码片段然后可以在其它SQL语句中引入此片段。举例如下图m_user 表中有多个字段。定义方法将SQL代码片段写在标签内并定义唯一id如下sql idUser_Column id,username,salt,phone,avatar,email,status,created,last_login /sql引用方式include refid被引用的代码片段IDselect include refidUser_Column / from m_user好处在 mapper.xml 文件中通常会定义大量的增删改查语句当我们定义一个SQL片段时就可以在整个 mapper.xml 文件中进行引用简化了后续的开发省去了很多不必要的写字段的时间和精力。此外当我们后续要对表添加修改字段时基本上只需要对SQL片段内部的字段做增减然后所有引用此SQL片段的SQL语句都会跟着改变非常方便。二. resultMap 映射结果集标签数据库字段与Java代码之前的映射关系是我们经常需要注意的一个问题例如数据库字段名称为 create_time但是在Java代码中通常采用驼峰的方式定义为 createTime这就会导致字段映射失败而 resultMap 就是来解决这个问题的。使用方法以上述 m_user 表为例last_login 字段在Java代码中通常定义为 lastLogin定义方式 mapper.xml 文件中在 resultMap 标签中这样写定义为一 id type 就是映射的 Java 实体类即 User 类。!-- 通用查询映射结果 -- resultMap idUserResultMap typecom.markerhub.entity.User id columnid propertyid / result columnusername propertyusername / result columnavatar propertyavatar / result columnemail propertyemail / result columnpassword propertypassword / result columnstatus propertystatus / result columncreated propertycreated / result columnlast_login propertylastLogin / /resultMap使用方式以 select 查询语句为例当查询返回值 resultMap 中全部都有时就可以通过 resultMapid值 来进行引用当查询数据库时mybatis 会自动把数据库字段的值映射到我们定义的Java属性值上。select idselectByUserName resultMapUserResultMap select include refidUser_Column/ from m_user where username #{username} /select三. where 条件标签条件 where 语句用于动态 SQL 时使用在查询数据库时我们不确定用户是否传入值如果有值就将条件加入到 where 如果没有传值则不加。使用方法将 where条件SQL定义在where标签内部当 where 标签内部不为空时where 标签就会生效。如果where 标签内部为空则不会将where条件语句拼接到SQL语句内。if标签也是同样的道理test id ! null 表示如果 id 值不为空则将 and id #{id}SQL片段拼接到SQL语句中。select idselectByIdAndUserName resultMapUserResultMap select include refidUser_Column/ from m_user w where if testid ! null and id #{id} /if if testusername ! null and username #{username} /if /where /select四. set 修改标签条件 set 语句和 where 标签是一个道理。示例代码如下set 标签 搭配 if 标签就可以达到当传入的值不为空时就更新值的效果。update idupdateUserById parameterTypecom.markerhub.entity.User update m_user set if testusername ! null username #{username}, /if if testavatar ! null avatar #{avatar}, /if if testemail ! null email #{email}, /if if testpassword ! null password #{password}, /if if teststatus ! null status #{status}, /if if testcreated ! null created #{created}, /if if testlastLogin ! null last_login #{lastLogin}, /if /set where id #{id} /update五. trim 标签trim 标签是一个单独的标签一共提供了四个属性如下图所示。prtefix/suffix在trim标签中内容前面/后面添加指定内容prefixOverrides/suffixOverrides在trim标签中内容前面/后面去掉指定内容完整的SQL语句这样写表示当 if 标签内容不为空时添加 where 过滤条件并且将 if 标签内不多余的后缀关键词 and 去掉防止语法错误。select idselectUserByIdAndUserName parameterTypecom.markerhub.entity.User select include refidUser_Column/ from m_user trim prefixwhere suffixOverridesand if testid ! null and id ! id #{id} and /if if testusername ! null and username ! username #{username} and /if /trim /select六. foreach 循环标签foreach 就是用来循环的语句通常出现在按照列表数据批量添加或批量删除数据时使用它提供了五个属性cllection代表要遍历的数组Java接口在定义是最好使用Parm进行标注item表示数组中的每个元素open表示该标签体以什么开头close 表示该标签体以什么结尾separator表示数据之间的分隔符是什么示例代码这段代码的意思就是参数传递一个ID集合集合不为空时where 过滤条件按照 id 值循环删除所有符合条件的数据。delete iddeleteUserByIds parameterTypearraylist delete from m_user where id in foreach collectionarray itemid open( separator, close) #{id} /foreach /where /delete