*获取主键信息
myBatis 可以直接获取主键id

 <!--为了获取到新增数据生成之后的主键-->
    <insert id="addOne"  keyColumn="teacher_id" keyProperty="teacherId" useGeneratedKeys="true">
        insert  into  tb_teacher values (null ,#{teacherName})
    </insert>

*mybatis 可以批量添加以及修改参数
*批量添加
collection :传的参数类型
item : 参数的别名
separator : 循环中每一条语句拼接结束后都加上的值

<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

查询

  <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 &lt;= #{maxPrice}
            </if>
            <if test="minSize!=null">
                and size>=#{minSize}
            </if>
            <if test="maxSize!=null">
                and size &lt;= #{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>

修改

  <!--修改时没有值就不更新,有值就更新   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>
  • cache 缓存

在spring-mapper.xml配置中 添加 开启二级缓存

  <property name="cacheEnabled" value="true"/>

此标签在第一次查询时增加缓存,在增删改时会清空缓存 (一般写在 所有sql的最上面)

 <cache/> 
-->
posted on 2019-09-10 17:34  脑抽不要停  阅读(129)  评论(0编辑  收藏  举报