zoukankan      html  css  js  c++  java
  • mybatis中的多条件查询

    使用Map集合和索引号

    接口:

    /**
    * 多条件查询Map集合
    * @param map
    * @return
    */
    public List<Student> findByManyCondition(Map<String,Object> map);

    /**
    * 多参数查询使用索引
    * @param name
    * @param age
    * @return
    */
    public List<Student> findStudentByCondition(String name,int age);


    xml文件(小配置)

    <!--多条件查询Map集合-->
    <select id="findByManyCondition" resultType="student">
    SELECT *from Student WHERE name LIKE '%' #{name } '%' AND age>#{age}
    </select>

    <!--多参数查询使用索引-->
    <select id="findStudentByCondition" resultType="student">
    SELECT *from Student WHERE name LIKE '%' #{0} '%' AND age>#{1}
    </select>


    测试类

    /**
    * 多条件查询Map集合
    */
    @Test
    public void findByManyCondition(){
    SqlSession session = MyBatisUtil.getSession();
    IStudentDAO mapper = session.getMapper(IStudentDAO.class);
    Map<String,Object> map=new HashMap<String,Object>();
    map.put("name","张");
    map.put("age",20);
    List<Student> list = mapper.findByManyCondition(map);
    for (Student item:list){
    System.out.println(item.getName());
    }
    session.commit();
    session.close();
    }



    /**
    * 多条件查询使用索引号
    */
    @Test
    public void findStudentByCondition(){
    SqlSession session = MyBatisUtil.getSession();
    IStudentDAO mapper = session.getMapper(IStudentDAO.class);
    List<Student> list = mapper.findStudentByCondition("张", 20);
    for (Student item:list){
    System.out.println(item.getName());
    }
    session.commit();
    session.close();
    }



  • 相关阅读:
    POJ2528——Mayor's posters (线段树区间更新查询+离散化)
    C++STL——unique函数总结
    HDU 5618 Jam's problem again(CDQ分治+树状数组(三维模板题))
    c++解决爆栈,手动加栈!
    POJ1741——Tree (树分治之点分治)
    树分治之点分治模板总结
    CodeForces
    字典树
    卡特兰数高精度算法
    基数排序
  • 原文地址:https://www.cnblogs.com/sujulin/p/7588799.html
Copyright © 2011-2022 走看看