• mybatis中的智能标签之一


    智能标签if-where和智能标签choose(when , otherwise)

    接口:
    /**
    * 智能标签if
    * @param student
    * @return
    */
    public List<Student> findByIf(Student student);

    /**
    * 智能标签choose
    * @param student
    * @return
    */
    public List<Student> findByChoose(Student student);


    xml文件(小配置)
    <!--智能标签if-->
    <select id="findByIf" resultType="student">
    SELECT *from Student
    <where>
    <if test="name!=null">
    AND name LIKE '%' #{name } '%'
    </if>
    <if test="age!=null">
    AND age>#{age}
    </if>
    </where>
    </select>

    <!--智能标签choose-->
    <select id="findByChoose" resultType="student">
    SELECT *from Student
    <where>
    <choose>
    <when test="name!=null">
    AND name LIKE '%' #{name } '%'
    </when>
    <when test="age">
    AND age>#{age}
    </when>
    <otherwise>
    1=1
    </otherwise>
    </choose>
    </where>
    </select>


    测试类:
    /**
    * 智能标签if
    */
    @Test
    public void findByIf(){
    SqlSession session = MyBatisUtil.getSession();
    IStudentDAO mapper = session.getMapper(IStudentDAO.class);
    Student student=new Student();
    //student.setName("张");
    student.setAge(20);
    List<Student> list = mapper.findByIf(student);
    for (Student item:list){
    System.out.println(item.getName());
    }
    session.commit();
    session.close();
    }





    /**
    * 智能标签choose
    */
    @Test
    public void findByChoose(){
    SqlSession session = MyBatisUtil.getSession();
    IStudentDAO mapper = session.getMapper(IStudentDAO.class);
    Student student=new Student();
    //student.setName("张");
    //student.setAge(20);
    List<Student> list = mapper.findByChoose(student);
    for (Student item:list){
    System.out.println(item.getName());
    }
    session.commit();
    session.close();
    }
     
  • 相关阅读:
    【攻防世界】beginners-luck
    Virtual Judge 20210601 日常训练 AB题解
    20210419 Vj 组队练习赛 CDIHEB 题解
    20210415 日常组队练习赛 EGJACI题解
    20210412 组队赛 BCEFGH题解
    20210408 Codeforces Round #372 (Div. 2) ABC 题解
    Codeforces Round #280 (Div. 2) BC题解
    QLU 日常训练 20210311 (Codeforces )
    QLU ACM-ICPC 2020 Training Contest 12 @2014 ICPC Anshan [Cloned]
    Virtual Judge 20210219 日常赛
  • 原文地址:https://www.cnblogs.com/sujulin/p/7588741.html
Copyright © 2020-2023  润新知