*获取主键信息
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/> -->