mybatis的5.1.10分頁外掛的使用
Mybatis 動態SQL
動態SQL就是根據不同的條件生成不同的SQL語句
1.環境搭建:
先建立一個部落格表:
CREATE TABLE `blog`( `id` VARCHAR(50) NOT NULL COMMENT '部落格id', `title` VARCHAR(100) NOT NULL COMMENT '部落格標題', `author` VARCHAR(30) NOT NULL COMMENT '部落格作者', `create_time` DATETIME NOT NULL COMMENT '建立時間', `views` INT(30) NOT NULL COMMENT '瀏覽量' )ENGINE=INNODB DEFAULT CHARSET=utf8
建立實體類:
程式碼:
package com.xiaofu.pojo; import java.util.Date; public class Blog { private int id; private String title; private String author; private Date createTime; private int views; public Blog() { } public Blog(int id, String title, String author, Date createTime, int views) { this.id = id; this.title = title; this.author = author; this.createTime = createTime; this.views = views; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } public int getViews() { return views; } public void setViews(int views) { this.views = views; } @Override public String toString() { return "Blog{" + "id=" + id + ", title=" + title + ", author='" + author + '\'' + ", createTime=" + createTime + ", views=" + views + '}'; } }
建立介面和對應xml
在核心配置檔案中註冊 建立的BlogMapper.xml
2.if語句:
編寫一個介面:
package com.xiaofu.dao; import com.xiaofu.pojo.Blog; import java.util.List; import java.util.Map; public interface BlogMapper { //查詢部落格 List<Blog> queryBlogIf(Map map); }
用 if 實現介面:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.xiaofu.dao.BlogMapper"> <select id="queryBlogIf" parameterType="map" resultType="com.xiaofu.pojo.Blog"> select * from blog where 1=1 <if test="title != null"> and title = #{title} </if> <if test="author != null"> and author = #{author} </if> </select> <!-- <if test="title != null"> 如果title不等於空 就在sql後面加上 and title = #{title} --> </mapper>
編寫測試類:
@Test public void getlist(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); BlogMapper mapper = sqlSession.getMapper(BlogMapper.class); HashMap map = new HashMap(); map.put("title","資料庫"); //查詢title為資料庫的所有資訊 // map.put("author","老王") //查詢author為老王的所有資訊; List<Blog> blogs = mapper.queryBlogIf(map); for (Blog blog : blogs) { System.out.println(blog); } sqlSession.close(); }
現在資料庫有這些資料:
執行測試類:
查詢出來了 title 等於 資料庫的 資料
3.其他動態SQL常用標籤:
choose、when、otherwise
有時候,我們不想使用所有的條件,而只是想從多個條件中選擇一個使用。針對這種情況,MyBatis 提供了 choose 元素,它有點像 Java 中的 switch 語句。
還是上面的例子,但是策略變為:傳入了 “title” 就按 “title” 查詢,傳入了 “author” 就按 “author” 查詢的情形。若兩者都沒有傳入,就返回標記為 featured 的 BLOG(這可能是管理員認為,與其返回大量的無意義隨機 Blog,還不如返回一些由管理員精選的 Blog)。
<select id="findActiveBlogLike" resultType="Blog"> SELECT * FROM BLOG WHERE state = ‘ACTIVE’ <choose> <when test="title != null"> AND title like #{title} </when> <when test="author != null and author.name != null"> AND author_name like #{author.name} </when> <otherwise> AND featured = 1 </otherwise> </choose> </select>
trim、where、set
set
set標籤 - 解決更新資料表時字串拼接逗號”,”問題;
沒有使用 if 標籤時,如果有一個引數為 null,都會導致錯誤。
當在 update 語句中使用 if 標籤時,如果最後的 if 沒有執行,則或導致逗號多餘錯誤。
使用 set 標籤可以將動態的配置 set 關鍵字,和剔除追加到條件末尾的任何不相關的逗號。
#如果最後的if沒有執行,會導致逗號多餘錯誤。 <update id="updateUser"> update User SET <if test="name != null"> name =#{name}, </if> <if test="age != null"> age=#{age}, </if> <if test="phone != null"> phone =#{phone}, </if> <if test="class != null"> class=#{class} </if> where id=#{id} </update>
<!-- set標籤除掉條件末尾的逗號--> <update id="updateUser"> update User <set> <if test="name != null"> name=#{name}, </if> <if test="age != null"> age=#{age}, </if> <if test="phone != null"> phone=#{phone}, </if> <if test="class != null"> adress=#{adress} </if> where class=#{class} </set> </update>
where標籤 - 解決if標籤拼接字串AND符號問題
where 標籤只會在子元素返回任何內容的情況下才插入 “WHERE” 子句。而且,若子句的開頭為 “AND” 或 “OR”,
where 元素也會將它們去除。這樣我們就不用在where後面加一個1=1了
當 if 標籤較多時,這樣的組合可能會導致錯誤。 如下:
<select id="getRoleListWhere" parameterType="Role" resultMap="roleResultMap"> SELECT * FROM role WHERE <if test="name!=null and name!=' ' "> name = #{name} </if> <if test="roletype!=null and roletype!=' ' "> AND roletype = #{roletype} </if> <if test="userid!=null"> AND userid = #{userid} </if> </select>
當 name 值為 null 時,查詢語句會出現 “WHERE AND” 的情況,解決該情況除了將"WHERE"改為“WHERE 1=1”之外,還可以利用 where標籤。
這個“where”標籤會知道如果它包含的標籤中有返回值的話,它就插入一個‘where’。此外,如果標籤返回的內容是以 AND 或 OR 開頭的,則它會剔除掉。
<select id="selectRoleListByRole" parameterType="Role" resultMap="roleResultMap"> <include refid="myselect"></include> <where> <if test="name!=null and name!=''"> name = #{name} </if> <if test="roletype!=null and roletype!=''"> AND roletype = #{roletype} </if> <if test="userid!=null"> AND userid = #{userid} </if> </where> </select>
trim
set 和 where 其實都是 trim 標籤的一種型別, 該兩種功能都可以使用 trim 標籤進行實現。如果你不滿足於where和set實現的功能你就可以使用trim來自定義實現。其中trim有四個主要的屬性,分別是:
- prefix:在sql語句加上字首
- suffix:在sql語句加上字尾
- prefixOverrides:去除指定的字首內容,比如:prefixOverrides=“AND | OR”,去除sql語句多餘的字首"AND"或者"OR"。
- suffixOverrides:去除指定的字尾內容,比如:suffixOverrides=",",去除sql語句多餘的逗號。
自定義實現3.1的set,去除update語句字尾多餘的逗號
<update id="updateUser" resultType="User"> update user set <trim suffixOverrides=","> <if test="class != null"> class = #{class}, </if> <if test="phone != null and adress != null"> age > #{age}, </if> </trim> where class=#{class} </update>
自定義實現3.2的where,去除where子句字首多餘的AND
<select id="findUser" resultType="User"> select * from user <trim prefix="WHERE" prefixOverrides="AND | OR"> <if test="class != null"> AND class = #{class} </if> <if test="phone != null and adress != null"> AND age > #{age} </if> </trim> </select>
sql標籤 - 可以提取重複sql語句片段
當多種型別的查詢語句的查詢欄位或者查詢條件相同時,可以將其定義為常量,方便呼叫。為求 <select> 結構清晰也可將 sql 語句分解。
<sql id="myselect"> SELECT * FROM role </sql>
include - 用於引用定義的常量
<resultMap type="Role" id="roleRM"> <id property="id" column="idrole" /> </resultMap> <select id="selectAllRole" resultMap="roleRM"> <include refid="myselect"></include> </select> <select id="selectRoleListByids" resultMap="roleRM"> <include refid="myselect"></include></select>
<-- <include refid="myselect"></include> 引用上面寫好的sql片段-->
foreach
主要用於構建 in 條件,可在 sql 中對集合進行迭代。也常用到批量刪除、新增等操作中。
- collection:collection 屬性的值有三個分別是 list、array、map 三種,分別對應的引數型別為:List、陣列、map 集合。
- item:表示在迭代過程中每一個元素的別名
- index:表示在迭代過程中每次迭代到的位置(下標)
- open:字首
- close:字尾
- separator:分隔符,表示迭代時每個元素之間以什麼分隔
<select id="findUser" resultType="User"> SELECT * FROM user where class in <foreach item="item" index="index" collection="classList" open="(" separator="," close=")"> #{item} </foreach> </select>