zoukankan      html  css  js  c++  java
  • MyBatis动态Sql 的使用

    Mapper.xml提示:

     1:mapper包中新建一个文件:mybatis-3-mapper.dtd  

    2:在web app libraries/mybatis.jar/org.apache.ibatis.builder.xml/mybatis-3-mapper.dtd,打开,复制内容,贴到自己新建mybatis-3-mapper.dtd

     3:把mapper.xml头上的dtd的路径该为当前目录!

     4:关闭StuMapper.xml文件,重新打开,即有提示!

    #{}和${}区别:  ${} 不会数值,不考虑数据类型,直接拼入sql,如果是字符串,没有单引号,sql异常!Statment,可能会发生sql注入问题!    #{} 不会发生:自动考虑数据类型,字符串会自动拼接'',PreparedStatment

    mybatis:动态sql标签

     <if> <choose> <when> <otherwise>  <where>  <set>   <foreach>  必须能解释清楚  剩下:在逆向生成的mapper.xml中:自己看!扩展 

        <select  id=”findByLike”  resultType=”com.stu.bean.Stu”  parameterType=”com.stu.bean.Stu”>   

     selec * from stu where 1=1   

       <if  test=”uname!=null”>and  uname=#{uname}</if>      

       <if  test=”uage!=null”>and  uage=#{uage}</if>   

       <if  test=”usex!=null”>and  usex=#{usex}</if>  

     </select>

        <select  id=”findByLike”  resultType=”com.stu.bean.Stu”  parameterType=”com.stu.bean.Stu”>    

      selec * from stu where 1=1   

        <if  test=”uname!=null”>or  uname=#{uname}</if>      

        <if  test=”uage!=null”>or  uage=#{uage}</if>  

        <if  test=”usex!=null”>or  usex=#{usex}</if>  

     </select>

    问题:如果永远都是全表  

      <select  id=”findByLike”  resultType=”com.stu.bean.Stu”  parameterType=”com.stu.bean.Stu”>    

    selec * from stu where 1!=1  

           <if  test=”uname!=null”>or  uname=#{uname}</if>   

           <if  test=”uage!=null”>or  uage=#{uage}</if>  

           <if  test=”usex!=null”>or  usex=#{usex}</if>   </select>解决上面问题

      <select id="xxx3" parameterType="com.stu.bean.Stu" resultType="com.stu.bean.Stu">  

      select * from stu where 1!=1

       <choose>      <!-- if  elseif   elseif -->   

        <when test="sid!=null">  

          or sid = #{sid}   

       </when>  

       <when test="sname!=null">  

               or sname like #{sname}   

      </when>    

     <when test="sage!=null">  

               or sage = #{sage}   

      </when>    

     <otherwise>  

          or saddress like #{saddress}   

      </otherwise>   

     </choose>  条件只可能成立一个!

      </select>  

      <select id="xxx4" parameterType="com.stu.bean.Stu" resultType="com.stu.bean.Stu">  

        select * from stu   

      <where>  <!-- 根据条件自动拼接where和and -->   

        <if test="sname!=null">     

         and sname=#{sname}    

       </if>  

       <if test="saddress!=null">    

              and saddress=#{saddress}   

      </if>   

     </where>

      </select>  

      <update id="xxx5" parameterType="com.stu.bean.Stu">  

          update stu <!-- 根据条件自动拼接set和逗号 -->

       <set>   

      <if test="sname!=null">  

          sname=#{sname},   

      </if>   

      <if test="sage!=null">    

          sage=#{sage},   

      </if>    

     <if test="ssex!=null">    

          ssex=#{ssex},    

     </if>    

     <if test="saddress!=null">    

           saddress=#{saddress},   

      </if>   

     </set>  

      <where>

        <if test="sid!=null">     

           sid=#{sid}    

     </if>    

    </where>  

     </update>

       <Delete id=”aaa”  parameterType=”array”>        

    Delete from stu where sid in    

      <foreach collection=”array” index=”index”  item=”item” open=”(”   separator=”,”  close=”)”>     #{item}  

      </foreach>

      </Delete >

    =========================================================================================

      逆向生成:  

    1:工具需要插件:放入dropins,重启my10!

     2:考入jar包,并且把mysql。jar导入工作区  

    3: 考入generatorConfig.xml文件,修改路径  

    4: 右键 generatorConfig.xml,点击蝴蝶   XxxExample:方法条件样例

     使用:  

     SqlSession session= MybatisUtil.getSqlSessionFactory().openSession();   

    StuMapper sm = session.getMapper(StuMapper.class);   

      sm.selectByPrimaryKey(Integer sid);  //和原来自己写xml方法一样   sm.selectByExample(Stuexample se);    StuExample se = new StuExample();   Criteria c = se.createCriteria();  c.andxxxxx()   sm.selectByExample(null);  查询所有   sm.countByExample(Stuexample se); 统计Criteria条件的行数   sm.deleteByExample(Stuexample e);   sm.deleteByPrimaryKey(Integer sid);  //和原来自己写xml方法一样   sm.insert(Stu s);   //和原来自己写xml方法一样,注意主键!   sm.insertSelective(Stu s);   哪个属性不为null,插入哪个属性   sm.updateByExample(Stu s, Stuexample e);   sm.updateByExampleSelective(Stu s, Stuexample e);   sm.updateByPrimaryKey(Stu s);   //和原来自己写xml方法一样   sm.updateByPrimaryKeySelective(Stu s);  哪个属性不为null,修改哪个属性      条件或者:   se.or(criteria2);

      排序:    se.setOrderByClause("sid desc");

      分页:    oracle:     select * from (      select x1.*,rownum rr from (select * from stu order by sid desc) x1 where rr<=5*n     ) x2 where x2.rr>=5*n-4

       mysql:     select * from stu order by sid desc limit 5*n-5,5

       mybatis:不使用Mapper,使用session.selectList();     使用一个类:RowBounds     SqlSession session= MybatisUtil.getSqlSessionFactory().openSession();     RowBounds rb = new RowBounds(pageSize*pageNow-pageSize,pageSize);     List<Stu> list = session.selectList("com.aaa.mapper.StuMapper.selectByExample",null, rb);

      重点:理解selective:所有的属性添加了if判断      Example:  对象的模板      Criteria:sql条件模板    StuExample se = new StuExample();    Criteria c = se.createCriteria();          熟练使用:XXXExample类  

    ============================================================================

    作业:2表crud  servlet-》biz->mapper(分页  模糊查询)

     SqlSessionFactroy的作用: 

     SqlSession的作用:  

    StuMapper.xml中描述:必须清楚,动态sql标签,resultType  resultMap  对象间的关系  xml描述

     ${}和#{}的区别!  StuExample的作用:提供了条件模板,提供了排序,提供去冗余  Criteria的作用:  排序  分页

    =========================================================================

     group  by:分组:只要用到分组,只能必须一定查询组信息    组信息:select  组名,5个聚合函数 from  表名  group by  组名    having:给组加条件,只能出现组名,5个聚合函数

       符合排序:select * from stu order by sage desc,sid asc  符合分组:select  ssex,count(*) from stu group by ssex,saddress...     是把ssex和saddress完全相同分为一组!

  • 相关阅读:
    java 在线网络考试系统源码 springboot mybaits vue.js 前后分离跨域
    springboot 整合flowable 项目源码 mybiats vue.js 前后分离 跨域
    flowable Springboot vue.js 前后分离 跨域 有代码生成器 工作流
    Flowable 工作流 Springboot vue.js 前后分离 跨域 有代码生成器
    java 企业 网站源码 后台 springmvc SSM 前台 静态化 代码生成器
    java 进销存 商户管理 系统 管理 库存管理 销售报表springmvc SSM项目
    基于FPGA的电子计算器设计(中)
    基于FPGA的电子计算器设计(上)
    FPGA零基础学习:SPI 协议驱动设计
    Signal tap 逻辑分析仪使用教程
  • 原文地址:https://www.cnblogs.com/zhuyeshen/p/11465739.html
Copyright © 2011-2022 走看看