*獲取主鍵信息
myBatis 能夠直接獲取主鍵idspring
<!--爲了獲取到新增數據生成以後的主鍵--> <insert id="addOne" keyColumn="teacher_id" keyProperty="teacherId" useGeneratedKeys="true"> insert into tb_teacher values (null ,#{teacherName}) </insert>
mybatis 能夠批量添加以及修改參數
批量添加
collection :傳的參數類型
item : 參數的別名
separator : 循環中每一條語句拼接結束後都加上的值sql
<insert id="insert"> insert into orderdetail(num,money,goodsuuid,goodsname,price,state,ordersuuid) values <foreach collection="list" item="orderDetailList" separator=","> (#{orderDetailList.num},#{orderDetailList.money}, #{orderDetailList.goodsuuid},#{orderDetailList.goodsname}, #{orderDetailList.price},#{orderDetailList.state}, #{orderDetailList.ordersuuid}) </foreach> </insert>
*動態sql緩存
查詢mybatis
<select id="findByParams" resultType="diskBean"> select * from tb_disk <!-- 若是where沒有條件那麼最終的SQL中就沒有where子句 做用 去除第一個 and--> <where> <if test="minPrice!=null"> and price>=#{minPrice} </if> <if test="maxPrice!=null"> and price <= #{maxPrice} </if> <if test="minSize!=null"> and size>=#{minSize} </if> <if test="maxSize!=null"> and size <= #{maxSize} </if> <if test="company != null and company !=''"> <!-- like模糊查詢 --> <!-- 使用拼接方式查詢 concat()--> and company like concat('%',#{company},'%') </if> <if test="catgory !=null and catgory!= ''"> and catgory like #{catgory} </if> </where> </select>
修改app
<!--修改時沒有值就不更新,有值就更新 set 做用去除最後一個逗號--> <update id="update" flushCache="true"> update tb_disk <set> <if test="price!=null"> price = #{price}, </if> <if test="size!=null"> size = #{size}, </if> <if test="company!=null and company.trim.length>0"> company = #{company}, </if> <if test="catgory!=null and catgory!=''"> catgory = #{catgory}, </if> <!--between 在什麼~ 什麼之間--> <if test="createin != null and createto !=null"> createtime between #{createin} and #{createto}, </if> <if test="checkin != null and checkto !=null"> checktime between #{checkin} and #{checkto}, </if> </set> where id =#{id} </update>
<update id="updateDiary"> update diary <trim prefix="set" suffixOverrides=","> <if test="diary.studentId!=null">student_id=#{diary.studentId},</if> <if test="diary.taskId!=null">task_id=#{diary.taskId},</if> <if test="diary.img!=null">img=#{diary.img},</if> <if test="diary.content!=null">content=#{diary.content},</if> <if test="diary.createTime!=null">create_time=#{diary.createTime},</if> <if test="diary.updateTime!=null">update_time=#{diary.updateTime},</if> <if test="diary.status!=null">`status`=#{diary.status},</if> </trim> where id=#{id} </update>
在spring-mapper.xml配置中 添加 開啓二級緩存ide
<property name="cacheEnabled" value="true"/>
此標籤在第一次查詢時增長緩存,在增刪改時會清空緩存 (通常寫在 全部sql的最上面)ui
<cache/> -->