| < | < | 小於 |
| > | > | 大於 |
| & | & | 與 |
| ' | ' | 單引號 |
| " | " | 雙引號 |
需要注意的是分號是必不可少的。 比如 a > b 我們就寫成 a > b
二、常用的sql語句寫法
1、模糊查詢
user_name like CONCAT("%",#{userName},"%") and
2、月份查詢
輸入月份(2019-01),查找屬於這個月份的記錄
DATE_FORMAT(start_time,'%Y-%m') <![CDATA[ <= ]]> DATE_FORMAT(#{theMonth},'%Y-%m') and DATE_FORMAT(end_time,'%Y-%m') <![CDATA[ >= ]]>DATE_FORMAT( #{theMonth},'%Y-%m') and
提示:DATE_FORMAT正則表達式(%Y-%m-%d %H:%i:%S)
3、時間區間查找
DATE_FORMAT(create_time,'%Y-%m-%d') <![CDATA[ >= ]]> DATE_FORMAT(#{startTime},'%Y-%m-%d') and DATE_FORMAT(create_time,'%Y-%m-%d') <![CDATA[ <= ]]> DATE_FORMAT(#{endTime},'%Y-%m-%d') and
全部格式化成年月日格式進行時間對比。
4、resultMap嵌套
<resultMap id="UserInfoResultMap" type="com..entity.User"> <id column="id" property="id" /> <result column="user_name" property="userName" /> <result column="password" property="password" /> <collection ofType="com.entity.Image" property="imageList"> <id column="image_id" property="id" /> <result column="image_path" property="imagePath" /> <result column="image_size" property="imageSize" /> </collection> </resultMap>
collection 嵌套:
如user類中有一個image圖片類的集合List imageList,要將每個人的圖片都嵌套存放在各自的imageList屬性中。
ofType:嵌套類的類名。這里就寫圖片類名稱Image。
property:主類中屬性名。這里就填user類中的imageList。
5、查詢
<select id="selectUserList" parameterType="com.param.UserParam" resultMap="BaseResultMap"> SELECT * FROM user <trim prefix="where" suffixOverrides="and"> <if test="userName != null"> user_name LIKE CONCAT("%",#{userName},"%") and </if> <if test="startTime != null"> DATE_FORMAT(create_time,'%Y-%m-%d') <![CDATA[ >= ]]> DATE_FORMAT(#{startTime},'%Y-%m-%d') and </if> <if test="endTime != null"> DATE_FORMAT(create_time,'%Y-%m-%d') <![CDATA[ <= ]]> DATE_FORMAT(#{endTime},'%Y-%m-%d') and </if> <trim prefix="id in(" suffix=")and" suffixOverrides=","> <foreach collection="idList" item="tiem"> #{tiem}, </foreach> </trim> </trim> </select>
6、批量添加
<insert id="insertBatch" parameterType="com.entity.User" useGeneratedKeys="true" keyProperty="id"> insert into user <trim prefix="(" suffix=")" suffixOverrides=","> name, age </trim> values <foreach collection="list" item="item" index="index" separator=","> <trim prefix="(" suffix=")" suffixOverrides=","> #{item.name}, #{item.age}, </trim> </foreach> </insert>
相當於insert into user (name,age)values (張,20),(李,21),(王,22)·····
7、批量更新
<update id="updateBatch" parameterType="com.safety.exam.entity.StaffAccount" useGeneratedKeys="true" keyProperty="id"> update user set name = <foreach collection="list" item="item" index="index" open="case id" close="end"> when #{item.id} then #{item.name} </foreach>, age = <foreach collection="list" item="item" index="index" open="case id" close="end"> when #{item.id} then #{item.age} </foreach> where id in <foreach collection="list" index="index" item="item" separator="," open="(" close=")"> #{item.id} </foreach> </update>
注意: set關鍵字只有一個;每個foreach之間有個逗號。
最后sql是這樣:
UPDATE categories SET display_order = CASE id WHEN 1 THEN 3 WHEN 2 THEN 4 WHEN 3 THEN 5 END, title = CASE id WHEN 1 THEN 'New Title 1' WHEN 2 THEN 'New Title 2' WHEN 3 THEN 'New Title 3' END WHERE id IN (1,2,3)
