天天看點

Mybatis學習-動态SQL

官網學習位址:https://mybatis.org/mybatis-3/zh/dynamic-sql.html

建立blog表(實測OK)

create table `blog`(
	`id` varchar(50) not null primary key 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
           

實體類:

public class Blog {
		private String id;
		private String title;
		private String author;
		private Date createTime;
		private int views;
	}
           

1、if标簽:

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

問題:

看到我們上面用了【where 1=1】,這是因為如果我們不寫 1=1,當title條件滿足時,就會出現【select * from blog where and title = #{title}】的語句,多了個and
我們使用where标簽就能解決這個問題。
           

小技巧:

我們開發中直接傳遞 parameterType=“map” 有個好處就是,我們可以在代碼中添加我們想要的字段,sql寫好就可以不用修改了。

比如我們搜尋頁面,可以通過多個關鍵字搜尋。我們一個接口就可以搞定了。前端傳多少個我們通過if都可以處理完。

2、where标簽:

where 元素隻會在子元素傳回任何内容的情況下才插入 “WHERE” 子句。而且,若子句的開頭為 “AND” 或 “OR”,where 元素也會将它們去除。
<select id="queryBlogIF" parameterType="map" resultType="com.example.Blog">
		select * from blog
		<where>
			<if test="title!=null">
				and title = #{title}
			</if>
			<if test="author!=null">
				and author = #{author}
			</if>
		</where>
	</select>
           
通過這個where标簽,當我們滿足一個條件時,前面的and會自動去掉。
           

總結:是以我們寫sql的時候,最好不要直接寫where關鍵字,使用标簽會更好。

3、choose、when、otherwise标簽:

有時候,我們不想使用所有的條件,而隻是想從多個條件中選擇一個使用。針對這種情況,MyBatis 提供了 choose 元素,它有點像 Java 中的 switch 語句。
<select id="queryBlogchoose" parameterType="map" resultType="com.example.Blog">
		select * from blog
		<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、set标簽:

set 元素會動态地在行首插入 SET 關鍵字,并會删掉額外的逗号(這些逗号是在使用條件語句給列指派時引入的)。
正常更新記錄的sql【update student set name = "update name" where id = 1;】

我們更新資料時,sql會使用到set關鍵字,例如
           
<update id="updateBlog" parameterType="map">
		update blog set 
		<if test="title != null">
			title = #{title},
		</if>
		<if test="author != null">
			author = #{author}
		</if>
		where id = #{id}
	</update>
           

問題:

可以看到會出現這樣問題,當第一個條件滿足時,就會多個逗号出來【update blog set title = #{title}, where id = #{id}】

<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>
           

總結:xml中不直接寫set關鍵字。

5、trim标簽

trim标簽使用:可以去掉if中的多餘的逗号
不加trim if中會存在逗号,例如:
           
<insert id="insertAccessRecord" parameterType="accessRecord">
    insert into t_tmss_access_record
	(
	    <if test="account != null">account,</if>
	)
	values(
		<if test="account != null">#{account},</if>
	)
  </insert>
           

使用trim标簽就可以自動去掉逗号

<insert id="insertAccessRecord" parameterType="accessRecord">
	insert into t_tmss_access_record
	<trim prefix="(" suffix=")" suffixOverrides=",">
		<if test="account != null">account,</if>
		<if test="interfaceName != null">interface_name,</if>
	</trim>
	<trim prefix="values (" suffix=")" suffixOverrides=",">
		<if test="account != null">#{account},</if>
		<if test="interfaceName != null">#{interfaceName},</if>
	</trim>
</insert>
           

suffixOverrides含義就是覆寫後面的逗号

6、sql片段:

把一些公共部分提取出來

a.使用sql便簽抽取公共部分
b.在使用的地方使用include标簽
           
<sql id="if-title-author">
		<if test="title != null">
			title = #{title}
		</if>
		<if test="author != null">
			and author = #{author}
		</if>
	</sql>

	<select id="queryBlogIF" parameterType="map" resultType="com.example.Blog">
		select * from blog
		<where>
			<include refid="if-title-author"></include>
		</where>
	</select>
           

注意:

最好基于單表

sql裡不要存在where标簽

繼續閱讀