久久福利_99r_国产日韩在线视频_直接看av的网站_中文欧美日韩_久久一

您的位置:首頁技術文章
文章詳情頁

MyBatis動態SQL實現配置過程解析

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

動態SQL

什么是動態SQL:

​ 動態SQL就是根據不同的條件生成不同的SQL語句

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

1、搭建環境

建表

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

創建一個基礎工程

導包

編寫配置文件

編寫實體類

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

編寫實體類對應的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>

所謂的動態SQL,本質還是SQL語句,只是我們可以在SQL層面,去執行一些邏輯代碼

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動態SQL實現配置過程解析

<!--select * from mybatis.bolg where 1=1 and (id=1 or id=2 or id=3)我們現在傳遞一個萬能的map,這個map中可以存在一個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>

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

建議:

先在MySQL中寫出完整的SQL,再對應的去修改成為我們的動態SQL

SQL片段

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

使用SQL標簽抽取公共的部分

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

在需要使用的地方使用Include標簽引用即可

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

注意事項:

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

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Mybatis 數據庫
相關文章:
主站蜘蛛池模板: 明里在线观看 | 国产精品一区av | h片免费观看 | 日韩欧美理论片 | 国产不卡精品视频 | 久久国产婷婷国产香蕉 | 四虎成人在线播放 | 中文字幕在线免费看 | 香蕉视频一级片 | 国内精品视频一区 | 亚洲精品国产偷自在线观看 | 蜜桃视频在线观看www社区 | 久久精品成人 | 成人在线免费视频观看 | 91在线精品一区二区三区 | 国产高清久久久 | 黄色影视网址 | 欧美午夜电影 | aaaaaa黄色片 | 一区二区三区日韩 | 精品成人| 欧美极品在线 | 亚洲成人精品 | 亚洲经典视频在线观看 | 亚洲国产二区 | 99久久电影 | 91精品国产综合久久婷婷香蕉 | 欧美日韩久久精品 | 日本欧美在线观看 | 国产精品三级久久久久久电影 | 一区二区三区四区精品 | 欧美日韩亚 | 精品国产一区二区三区在线观看 | 婷婷综合网| 天天色天天色 | 日韩视频中文字幕在线观看 | 91亚洲国产 | 日韩av免费在线观看 | 亚洲欧洲日韩在线 | 国产超碰在线观看 | 天天摸天天干 |