在使用Mybatis進行crud時,會遇到判斷是否為空,從而導致有多余逗號導致sql語句出問題的情況,這里有幾種解決方式:
1.update語句更新時,解決多余逗號問題(通過<trim prefix="set" suffixOverrides=",">):
<update id="updateById" parameterType="com.wk.ManActivity"> update man_activity <trim prefix="set" suffixOverrides=","> <if test="activityType != null"> activity_type = #{activityType,jdbcType=TINYINT}, </if> <if test="client != null"> client = #{client,jdbcType=TINYINT}, </if> <if test="activityStatus != null"> activity_status = #{activityStatus,jdbcType=TINYINT}, </if> <if test="activityName != null"> activity_name = #{activityName,jdbcType=VARCHAR}, </if> <if test="startTime != null"> start_time = #{startTime,jdbcType=TIMESTAMP}, </if> <if test="endTime != null"> end_time = #{endTime,jdbcType=TIMESTAMP}, </if> <if test="updateTime != null"> update_time = #{updateTime,jdbcType=TIMESTAMP}, </if> <if test="updateUser != null"> update_user = #{updateUser,jdbcType=VARCHAR}, </if> </trim> where id = #{id,jdbcType=BIGINT} </update>
或者也可以使用<set>標簽
<update id="updateAuthorIfNecessary"> update Author <set> <if test="username != null">username=#{username}, </if> <if test="password != null">password=#{password}, </if> <if test="email != null">email=#{email}, </if> <if test="bio != null">bio=#{bio} </if> </set> where id=#{id} </update>
這里,set 元素會動態前置 SET 關鍵字,同時也會消除無關的逗號,因為用了條件語句之后很可能就會在生成的賦值語句的后面留下這些逗號。
<update id="updateAuthorIfNecessary"> update user <trim prefix="set" suffixoverride="," suffix=" where id = #{id} "> <if test="name != null and name.length()>0"> name=#{name} , </if> <if test="gender != null and gender.length()>0"> gender=#{gender} , </if> </trim> </update>
假如說name和gender的值都不為null的話打印的SQL為:update user set name='xx' , gender='xx' where id='x'
在紅色標記的地方不存在逗號,而且自動加了一個set前綴和where后綴,上面三個屬性的意義如下,其中prefix意義如上:
suffixoverride:去掉最后一個逗號(也可以是其他的標記,就像是上面前綴中的and一樣)
suffix:后綴
2.where語句使用時遇到的問題(and或者or多余的情況)
<select id="quaryQualityList" parameterType="com.ManQualityShop" resultMap="BaseResultMap"> select * from user <trim prefix="WHERE" prefixoverride="AND |OR"> <if test="name != null and name.length()>0"> AND name=#{name}</if> <if test="gender != null and gender.length()>0"> AND gender=#{gender}</if> </trim> </select>
假如說name和gender的值都不為null的話打印的SQL為:select * from user where name = 'xx' and gender = 'xx'
在紅色標記的地方是不存在第一個and的,上面兩個屬性的意思如下:
prefix:前綴
prefixoverride:去掉第一個and或者是or
也可以使用<where>標簽
<select id="findActiveBlogLike" resultType="Blog"> SELECT * FROM BLOG <where> <if test="state != null"> state = #{state} </if> <if test="title != null"> AND title like #{title} </if> <if test="author != null and author.name != null"> AND author_name like #{author.name} </if> </where> </select>
3.<include>標簽的作用
MyBatis中sql標簽定義SQL片段,include標簽引用,可以復用SQL片段。
sql標簽中id屬性對應include標簽中的refid屬性。通過include標簽將sql片段和原sql片段進行拼接成一個完整的sql語句進行執行。
<sql id="sqlid"> res_type_id,res_type </sql> <select id="selectbyId" resultType="com.property.vo.PubResTypeVO"> select <include refid="sqlid"/> from pub_res_type </select>
引用同一個xml中的sql片段
<include refid="sqlid"/>
引用公用的sql片段
<include refid="namespace.sqlid"/>
Mybatis插入數據的時候返回插入記錄的主鍵id
在進行輸入庫插入的時候,如果我們需要使用已經插入的記錄的主鍵,則需要返回剛才插入的數據的主鍵id。
通過設置 insert 標簽的 useGeneratedKeys 屬性為 true 可以返回插入的記錄的主鍵的id。
<insert id="User" useGeneratedKeys="true" keyProperty="id"> </insert>