zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    ceph 手工部署
    zstack 搭建部署
    ceph crush
    mini2440动态加载hello.ko模块
    j-flash配置用于烧录mini 2440 nor flash
    (转载)PPP协议规范
    at91sam9263: 定时器
    cyg_io_read返回值是0,因为读到的字节长度在参数中
    read函数
    Linux编译错误:‘cout’在此作用域中尚未声明
  • 原文地址:https://www.cnblogs.com/liudingwei/p/12764081.html
Copyright © 2011-2022 走看看