Mybatis练习---mybatis 代理形式

  1. SQL语句中特殊字段处理(转义字符 同xml)

  2. 多条件查询当功能有多个参数时,使用@Param("参数名称") 标记每一个参数,在映射配置文件中就需要使用 #{参数名称} 进行占位,也可以将多个参数封装成对象(里面的内容必须和实体类属性名保持一致 )或者map集合的形式(里面的内容必须和map集合中键的名称一致 

    List<Brand> selectByCondition(@Param("status") int status, @Param("companyName") String companyName,@Param("brandName") String brandName);
    <select id="selectByCondition" resultType="brand">
        select *
        from tb_brand
        where status = #{status}
        and companyName like #{companyName}
        and brandName like #{brandName}
    </select>
  3. 动态SQL---(了解mybatis高级特性!!!有难度)
      #  <where>标签会自动去掉第一个条件的 and
    select * from tb_brand
      <where>
         <if test="status!=null">   #if(status!=null)//有status条件
            status=?
         </if>
         <if test="brandName!=null and brandName!=''" >   #品牌名称写了必须不是空字符串
            and brandName=?
         </if>
         <if test="companyName!=null and companyName!=''" >   #品牌名称写了必须不是空字符串
           and companyName=?
         </if>
      </where>
    
       <if>标签作用: 表示条件判断 test属性里面写判断语句
              符合条件就拼接 不符合不拼接 --形成动态条件形式了--动态sql
       <where> 标签作用 替代了where 后面有条件有where 没条件没where
                后面条件的第一个and会自动去除
  4. 添加-主键返回 (这种思想要理解)

       <!--    /*
          完成 添加操作
         */
        void add(Brand brand);
    
        需要做主键回显
            就需要两个属性 写出来
              useGeneratedKeys  是否要获取自增主键的值。
              keyProperty  把这个主键的值 给谁?给哪个属性信息
    
        -->
        <insert id="add" useGeneratedKeys="true" keyProperty="id">
            insert into tb_brand
             values(null,#{brandName},#{companyName},#{ordered},#{description},#{status})
        </insert>

  5. 修改
        <update id="update">
            update tb_brand   set 
                       brandName = #{brandName},           
                      companyName = #{companyName},            
                      ordered = #{ordered},
                      description = #{description},
                      status = #{status}
                      where id =#{id}
        </update>

  6. 批量删除(扩展讲解)
     

      /*
             设计  根据多个id删除的方法
             参数  int[]  数组里面放置多个id
             返回值 void
          */
         void deleteByIds(int[] ids);
     <!--
         void deleteByIds(int[] ids);
           根据多个id删除多条数据
           假如 int[] ids = {1,2,4}  {3,5,6,9}
          delete from tb_brand  where id=ids;
          delete from tb_brand where id in(1,2,4);
      delete from tb_brand where id in(#{ids[0]},#{ids[1],#{ids[2]});
            问题是
              (1,2,4)怎么给搞出来
              (3,5,6,9)       3,5,6,9
             <foreach> 完成数组或集合的遍历
                 属性 collection 被遍历容器是谁
                           数组 写 array  集合 list
                      open   遍历之前拼接点啥
                      close  遍历之后拼接点啥
                      separator  元素之间的分隔符
                      item  定义被遍历出来的元素的接收变量名
                      比如 item="id"  id就是被遍历出来的数字
                      
                         #{id}  把每个元素拿出来进行拼接
       -->
        <delete id="deleteByIds">
           delete from tb_brand where id in
           <foreach collection="array" open="(" close=")" separator="," item="id">
               #{id}
           </foreach>
        </delete>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值