av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術(shù)文章
文章詳情頁

MyBatis動態(tài)SQL實(shí)現(xiàn)配置過程解析

瀏覽:4日期:2023-10-24 10:51:59

動態(tài)SQL

什么是動態(tài)SQL:

​ 動態(tài)SQL就是根據(jù)不同的條件生成不同的SQL語句

if choose(when,otherwise) trim(where,set) foreach

1、搭建環(huán)境

建表

CREATE TABLE `bolg`( `id` VARCHAR(50) NOT NULL COMMENT ’博客id’, `title` VARCHAR(100) not null comment ’博客標(biāo)題’, `author` VARCHAR(30) not null comment ’博客作者’, `creat_time` datetime not null comment ’創(chuàng)建時(shí)間’, `views` int(30) not null comment ’瀏覽量’)ENGINE=InnoDB DEFAULT CHARSET=utf8

創(chuàng)建一個(gè)基礎(chǔ)工程

導(dǎo)包

編寫配置文件

編寫實(shí)體類

@Datapublic class Blog { private int id; private String title; private String author; private Date creatTime; private int views;}

編寫實(shí)體類對應(yīng)的Mapper接口和Mapper.xm

2、IF

<select parameterType='map' resultType='com.rui.pojo.Blog'> select * from mybatis.bolg where 1=1 <if test='title != null'> and title = #{title} </if> <if test='author != null'> and author = #{author} </if></select>

@Testpublic void queryBlogIF(){ SqlSession sqlSession = MyBatisUtils.getSqlSession(); BlogMapper mapper = sqlSession.getMapper(BlogMapper.class); HashMap map = new HashMap(); map.put('author','尹銳'); List<Blog> blogs = mapper.queryBlogIF(map); for (Blog blog : blogs) { System.out.println(blog); } sqlSession.close();}

3、choose(when,otherwise)

<select parameterType='map' resultType='com.rui.pojo.Blog'> select * from mybatis.bolg <where> <choose> <when test='title != null'>title=#{title} </when> <when test='author!=null'>and author = #{author} </when> <otherwise>and views = #{views} </otherwise> </choose> </where></select>

4、trim(where,set)

select * from mybatis.bolg<where><if test='title != null'> title = #{title}</if><if test='author != null'> and author = #{author}</if></where>

<update parameterType='map'> update mybatis.bolg <set> <if test='title != null'> title = #{title}, </if> <if test='author != null'> author = #{author}, </if> </set> where id = #{id}</update>

所謂的動態(tài)SQL,本質(zhì)還是SQL語句,只是我們可以在SQL層面,去執(zhí)行一些邏輯代碼

5、Foreach

select * from user where 1=1 and <foreach item='id' index='index' collection='ids' open='(' separator='or' close=')'> #{id} </foreach>(id=1 or id=2 or id=3)

MyBatis動態(tài)SQL實(shí)現(xiàn)配置過程解析

<!--select * from mybatis.bolg where 1=1 and (id=1 or id=2 or id=3)我們現(xiàn)在傳遞一個(gè)萬能的map,這個(gè)map中可以存在一個(gè)map--><select parameterType='map' resultType='com.rui.pojo.Blog'> select * from mybatis.bolg <where> <foreach collection='ids' item='id' open='(' close=')' separator='or'> id = #{id} </foreach> </where></select>

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

建議:

先在MySQL中寫出完整的SQL,再對應(yīng)的去修改成為我們的動態(tài)SQL

SQL片段

有的時(shí)候,我們可能會將一些公共的部分抽取處理,方便復(fù)用

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

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

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

<select parameterType='map' resultType='com.rui.pojo.Blog'> select * from mybatis.bolg <where> <include refid='if-title-author'></include> </where></select>

注意事項(xiàng):

最好基于單表來定義SQL片段

不要存在where或者set標(biāo)簽,片段里盡量只有if就好了

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Mybatis 數(shù)據(jù)庫
相關(guān)文章:
主站蜘蛛池模板: 精品在线免费视频 | 亚洲国产黄色 | av不卡一区| 亚洲成人中文字幕 | 久久a级片 | 日韩av中文字幕在线播放 | 国产三级在线观看视频 | 国产欧美激情 | 免费毛片视频 | 欧美日韩在线看 | 久久精品国产精品 | 天堂色av| 福利视频一区二区 | 亚洲天堂网在线观看 | 精品久久久久久久 | 久久久精品一区 | 欧美激情国产精品 | 国产精品久久久久久久久久久久久久 | www.麻豆av| 女人av在线| 免费久久久 | 无遮挡毛片 | 少妇一级淫免费观看 | 一级做a爰片久久毛片潮喷 亚洲黄色天堂 | 国产日韩一区二区 | 天天草天天射 | www.成人网| 日韩在线视频播放 | 欧美一区二区在线播放 | 国产一级二级 | 久草网站 | 日韩av毛片 | 午夜在线观看视频 | 日韩欧美国产一区二区三区 | 依人久久 | 国产人人干 | 偷拍一区二区三区 | 三a毛片 | 天堂资源av | 无套内谢的新婚少妇国语播放 | 久久久夜色精品 |