Mybatis动态sql标签说明

一、简述

1️⃣config文件常用标签
properties标签:引入外部properties文件资源。
settings标签:设置mybatis全局行为。
typeAlias标签:减少mapper文件配置,给模型类起别名。
transactionManager标签:配置mybatis的事务行为(JDBC|MANAGED)
dataSource标签:配置mybatis数据源(POOLED|UNPOOLED|JNDI)
mappers标签:引入Mapper文件或接口(url|resources|class|package)
2️⃣Mapper文件常用标签
a. resultMap标签:对查询的结果进行封装处理
简单pojo类型处理,只需处理表中字段和对像中属性映射处理。
包装pojo类型处理,使用Collection处理包装集合类型的对像,association处理包含单个pojo对像。
Collection处理的两种方式:
1.连接查询join
2.通过多条select查询的方式。
association处理两种方式:
1.连接查询join
2.通过多条select查询的方式。

discriminor鉴别器:根据查询结果中某个字段作为标识符,根据其查询的值,进行分类封装处理。

b.主键映射策略:如果主键由数据库生成,而不是手动指定,怎么获取该主键的问题。
一般使用selectKey标签处理。
3️⃣自定义封装结果集(了解):
按照自已定义方式把数据库返回结果封装成自己想要的类型。
一般自己建立结果的处理类,实现ResultHandler接口,重写里面的方法。
一般在select("statementId",params,ResultHandler实现类对像)
4️⃣解决什么样问题
用来解决sql语句where后条件的拼接问题,
使用流程控制标签来完成条件的拼接:if标签,where标签,foreach标签。。。。
5️⃣常用动态sql标签
1.if标签
2.choose when otherwise标签
3.trim标签
4.foreache标签
5.where标签
6.set标签

二、if标签:类似于Java中的if语句

<select id="selectByItem" parameterType="User" resultType="User">
    select * from USER
    where 1=1
    <!--mybatis如果是低版本时,如果是整形值,不要用‘’来判断;3.4.6版本没有问题-->
    <if test="id != null and id !=''">
        and id = #{id}
    </if>
    <if test="username !=null and username !=''">
        and username=#{username}
    </if>
    <if test="password !=null and password !=''">
        and password=#{password}
    </if>
    <if test="address !=null and address !=''">
        and address=#{address}
    </if>
</select>

三、where标签

根据查询条件是否存在,来决定是否生成where字符串。可以去除where后面紧跟的sql关键字, 如or或and。

<!-- where标签 -->
<select id="selectByItem2" parameterType="User" resultType="User">
    select * from USER
    <where>
        <if test="id !='' and id != null">
            and id = #{id}
        </if>
        <if test="username !=null and username !=''">
            and username=#{username}
        </if>
        <if test="password !=null and password !=''">
            and password=#{password}
        </if>
        <if test="address !=null and address !=''">
            and address=#{address}
        </if>
    </where>
</select>

四、 choose when otherwise

和Java中switch case作用类似。不管有多少条件满足,只拼接其中一个条件。

<!-- choose when otherwise -->
<select id="selectByItem3" parameterType="User" resultType="User">
    select * from USER
    <where>
        <choose>
            <when test="id !='' and id != null">
                and id = #{id}
            </when>
            <when test="username !=null and username !=''">
                and username=#{username}
            </when>
            <when test="password !=null and password !=''">
                and password=#{password}
            </when>
            <when test="address !=null and address !=''">
                and address=#{address}
            </when>
            <otherwise>
                and 1=1
            </otherwise>
        </choose>
    </where>
</select>

五、Set标签

set用法和上面where用法一致。可以生成update语句中的set关键字,也可去除sql关键字,如逗号(set标签中sql字符串最后的逗号)

<update id="update" parameterType="User">
    update USER
    <set>
        <if test="username !=null and username !=''">
            username = #{username},
        </if>
        <if test="password !=null and password !=''">
            password=#{password},
        </if>
        <if test="address !=null and address !=''">
            address=#{address},
        </if>
    </set>
    where id = #{id}
</update>

六、foreach标签

类似于Java中的foreach迭代。把传入sql语句中的数组类型或集合类型数据进行遍历操作。

<!-- 接口中参数为数组时,collection值必须为array -->
<select id="selectByIds" resultType="User">
    select * from USER
    <foreach collection="array" item="id" open="where id in(" separator="," close=")">
        #{id}
    </foreach>
</select>
​
<!-- 接口中参数为pojo,collection值必须为该数组类型属性名 -->
<select id="selectByIds2" resultType="User" parameterType="User">
    select * from USER
    <foreach collection="ids" item="id" open="where id in(" separator="," close=")">
        #{id}
    </foreach>
</select>

七、trim标签:可以删除sql语句指定的字符串。

<!--
 prefix="前缀":在后面sql字符串前面拼接指定的前缀。
 prefixOverrides="要去除的字符":把紧跟着前缀后面的字符去掉。
 suffix="后缀":在后面sql字符串后面拼接指定的后缀。
 suffixOverrides="要去除的字符":把紧跟着后缀前面的字符去掉。
-->
​<update id="update2" parameterType="User">
    update USER
    <trim prefix="set" prefixOverrides=",">
        <if test="username !=null and username !=''">
            ,username = #{username}
        </if>
        <if test="password !=null and password !=''">
            ,password=#{password}
        </if>
        <if test="address !=null and address !=''">
            ,address=#{address}
        </if>
    </trim>
    where id = #{id}
</update>
<update id="update3" parameterType="User">
    update USER
    <trim prefix="set" suffixOverrides=",">
        <if test="username !=null and username !=''">
            username = #{username},
        </if>
        <if test="password !=null and password !=''">
            password=#{password},
        </if>
        <if test="address !=null and address !=''">
            address=#{address},
        </if>
    </trim>
    where id = #{id}
</update>
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 151,829评论 1 331
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 64,603评论 1 273
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 101,846评论 0 226
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 42,600评论 0 191
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 50,780评论 3 272
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 39,695评论 1 192
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,136评论 2 293
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 29,862评论 0 182
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 33,453评论 0 229
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 29,942评论 2 233
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,347评论 1 242
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 27,790评论 2 236
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,293评论 3 221
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 25,839评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,448评论 0 181
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 34,564评论 2 249
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 34,623评论 2 249