在使用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>