当前位置:Gxlcms > 数据库问题 > mybatis的动态sql

mybatis的动态sql

时间:2021-07-01 10:21:17 帮助过:5人阅读

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>

 

mybatis的动态sql

标签:student   lis   uname   app   set   动态sql   code   避免   style   

人气教程排行