最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Mybatis動(dòng)態(tài)SQL的示例代碼

 更新時(shí)間:2021年10月28日 08:37:12   作者:shuati2000  
本文主要介紹了Mybatis動(dòng)態(tài)SQL的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

什么是動(dòng)態(tài)SQL:動(dòng)態(tài)SQL就是根據(jù)不同的條件生成不同的SQL語(yǔ)句

基本流程

1,數(shù)據(jù)庫(kù)準(zhǔn)備一張表
2,導(dǎo)包
3,編寫(xiě)核心配置文件
4,編寫(xiě)實(shí)體類(lèi)
5,編寫(xiě)實(shí)體類(lèi)對(duì)應(yīng)的Mapper和Mapper.xml文件
6,在核心配置文件中注冊(cè)Mapper.xml
7,測(cè)試

開(kāi)啟自動(dòng)駝峰命名規(guī)則映射

    <!--開(kāi)啟駝峰命名映射-->
    <setting name="mapUnderscoreToCamelCase" value="true"/>

即在數(shù)據(jù)庫(kù)中為create_time對(duì)應(yīng)Java實(shí)體類(lèi)屬性createTime

IF,Where

    <select id="queryListIf" parameterType="map" resultType="Blog">
        select * from blog 
        <where>
            <if test="title != null">
             title = #{title}
         </if>
         <if test="author != null">
             and author = #{author}
         </if>
        </where>
      </select>

Where的作用:當(dāng)至少有一個(gè)滿足條件時(shí)添加Where,且會(huì)判斷后面加的第一條語(yǔ)句,若是and開(kāi)頭,則會(huì)自動(dòng)將這個(gè)and刪除
本質(zhì)上還是在拼接SQL,上述當(dāng)沒(méi)有滿足條件時(shí)查詢blog表中的所有,當(dāng)滿足條件時(shí),則拼接SQL

Set

 <update id="updateBlog" parameterType="map">
        update blog
        <set>
            <if test="title != null">
                title = #{title},
            </if>
            <if test="author != null">
                author = #{author}
            </if>
        </set>
        where id = #{id}
    </update>

Set的作用:至少有一個(gè)滿足條件時(shí)添加Set,且會(huì)判斷后面加的最后的語(yǔ)句,若是",“結(jié)尾,則會(huì)自動(dòng)將這個(gè)”,"刪除

Choose(when,otherwise)

    <select id="queryNoLimit" parameterType="map" resultType="Blog">
        select * from blog
        <where>
            <choose>
                <when test="title != null">
                    title = #{title}
                </when>
                <when test="author != null">
                    and author = #{author}
                </when>
                <otherwise>
                    and `view` = #{view}
                </otherwise>
            </choose>
        </where>
    </select>

choose(when,otherwise)類(lèi)似與Java中的switch(case,default),choose進(jìn)入選擇,when當(dāng)什么什么時(shí),進(jìn)行條件判斷,若滿足條件,則執(zhí)行條件中的內(nèi)容,后面的when,otherwise將不再執(zhí)行,otherwise當(dāng)所有when都不滿足條件時(shí)執(zhí)行

ForEach

    <select id="queryBlogById" parameterType="map" resultType="blog">
        select * from blog
        <where>
            <foreach collection="ids" item="id" open="(" close=")" separator="or">
                id = #{id}
            </foreach>
        </where>
    </select>

上述為,一個(gè)集合ids存儲(chǔ)id的內(nèi)容,根據(jù)這個(gè)集合查詢所包含的id,open為開(kāi)始,close為結(jié)束,separator為分隔符
才用map.put(“ids”,list)的方式導(dǎo)入集合

建議:現(xiàn)在Mysql中寫(xiě)出完整的sql,再對(duì)應(yīng)的去修改即可

SQL片段

將一些功能的部分抽取出來(lái)方便復(fù)用

使用SQL標(biāo)簽抽取公共的部分

    <sql id="titleAuthor">
        <if test="title != null">
            title = #{title}
        </if>
        <if test="author != null">
            and author = #{author}
        </if>
    </sql>

在需要的地方使用include標(biāo)簽引用即可

    <select id="queryListIf" parameterType="map" resultType="Blog">
        select * from blog
        <where>
            <include refid="titleAuthor"></include>
        </where>
    </select>

注意事項(xiàng):
1.最好基于單表來(lái)定義SQL片段
2.不要存在where標(biāo)簽

總結(jié)

所謂的動(dòng)態(tài)SQL就是在拼接SQL語(yǔ)句,我們只要保證SQL的正確性,按照SQL的格式去排列組合就可以了

到此這篇關(guān)于Mybatis動(dòng)態(tài)SQL的示例代碼的文章就介紹到這了,更多相關(guān)Mybatis動(dòng)態(tài)SQL內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

西宁市| 将乐县| 麦盖提县| 监利县| 太和县| 阳东县| 双桥区| 德昌县| 安塞县| 彭阳县| 礼泉县| 红原县| 霍山县| 视频| 宜君县| 晋城| 平凉市| 常宁市| 密山市| 黔西县| 昆山市| 中西区| 安庆市| 新源县| 邵东县| 宜君县| 灯塔市| 鹤壁市| 卓尼县| 蒙城县| 茌平县| 芒康县| 和静县| 汾西县| 嘉兴市| 会东县| 银川市| 临潭县| 通渭县| 清水县| 榆林市|