MyBatis 增改查

*获取主键信息
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>
  • cache 缓存

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

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

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

 <cache/> 
-->

转载于:https://www.cnblogs.com/lxx-1843693653/p/11498725.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值