• mybatis的动态sql


    1.if 标签

      注意:where 1=1是为了避免当uId不传值时,会导致生成bad sql

    <select id="getStuByIf" resultType="com.itheima.pojo.TbStudent" parameterType="com.itheima.pojo.TbStudent">
        select * from tb_student where 1=1
        <if test="uId!=null and uId!='' ">
         and   u_id=#{uId}
        </if>
    
        <if test="uName!=null and uName!=''">
            and    u_name=#{uName}
        </if>
    
        <if test="sex!=null and sex!=''">
            and  sex=#{sex}
        </if>
    
        <if test="tId!=null and tId!=''">
            and    t_id=#{tId}
        </if>
    </select>
      TbStudent student = new TbStudent();
         //   student.setuId(2);
            student.setuName("lisi");
            student.setSex("男");
            List<TbStudent> stu = tbStudentMapper.getStuByIf(student);
            System.out.println(stu);



     Preparing: select * from tb_student where 1=1 and u_name=? and sex=?

    2.where 标签

      改进if标签

    <select id="getStuByIf" resultType="com.itheima.pojo.TbStudent" parameterType="com.itheima.pojo.TbStudent">
        select * from tb_student
        <where>
        <if test="uId!=null and uId!='' ">
           and  u_id=#{uId}
        </if>
    
        <if test="uName!=null and uName!=''">
            and    u_name=#{uName}
        </if>
    
        <if test="sex!=null and sex!=''">
            and  sex=#{sex}
        </if>
    
        <if test="tId!=null and tId!=''">
            and    t_id=#{tId}
        </if>
        </where>
    </select>
  • 相关阅读:
    airodump-ng的使用及显示
    bash shell 遍历一个数组
    cisco 交换机 IOS命令
    apt-key Debian packages密钥管理命令
    find命令的用法
    bash的快捷键
    groff编写man页
    tcpdump软件使用
    vue 项目常见功能(搜索 时间戳转换 过滤器)
    vue2.0 常见功能 (v-for 配置路由 组件渲染)
  • 原文地址:https://www.cnblogs.com/liudingwei/p/12764081.html
Copyright © 2020-2023  润新知