SM-MyBatis-13:Mybatis中多条件查询

Posted 晨曦Dawn

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SM-MyBatis-13:Mybatis中多条件查询相关的知识,希望对你有一定的参考价值。

 

 

------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------

 

 

实体类

 

public class Book {
    private Integer bookID;
    private String bookName;
    private String bookAuthor;
    private Integer bookPrice;

    public Book() {
    }

    public Integer getBookID() {
        return this.bookID;
    }

    public void setBookID(Integer bookID) {
        this.bookID = bookID;
    }

    public String getBookName() {
        return this.bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public String getBookAuthor() {
        return this.bookAuthor;
    }

    public void setBookAuthor(String bookAuthor) {
        this.bookAuthor = bookAuthor;
    }

    public Integer getBookPrice() {
        return this.bookPrice;
    }

    public void setBookPrice(Integer bookPrice) {
        this.bookPrice = bookPrice;
    }
}

 

接口中的方法

俩种形式,一种采用map,一种采用直接参数Index(索引的方式)来实现的多条件查询

 

//根据多条件查询map版
    public List<Book> findtrueBookMap(Map<String,Object> map);
    //根据多条件查询index版
    public List<Book> findtrueBookIndex(String bookName,Integer bookPrice);

 

小配置中

 

<!--多条件查询map版-->
    <select id="findtrueBookMap" resultType="Book">
        select * from book WHERE bookName LIKE % #{bookName} % AND bookPrice>#{bookPrice}
    </select>
    <!--多条件查询Index版-->
    <select id="findtrueBookIndex" resultType="Book">
        select * from book WHERE bookName LIKE % #{0} % AND bookPrice>#{1}
    </select>

 

测试类中

 

    ///多条件查询Index版
    @Test
    public void t4selectmoreIndex(){
        SqlSession session= MyBatisUtils.getSession();

        IBookDAO mapper = session.getMapper(IBookDAO.class);
        List<Book> books = mapper.findtrueBookIndex("",40);
        for (Book items:books) {
            System.out.println(items.getBookName());
        }

        session.close();

    }

    ///多条件查询map版
    @Test
    public void t3selectmoreMap(){
        SqlSession session= MyBatisUtils.getSession();

        IBookDAO mapper = session.getMapper(IBookDAO.class);
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("bookName","");
        map.put("bookPrice",40);
        List<Book> books = mapper.findtrueBookMap(map);
        for (Book items:books) {
            System.out.println(items.getBookName());
        }

        session.close();

    }

 

这块要解释的真的没有些什么,先照猫画虎,会用,知道每出该填什么,入们后再去想其他,有些从字面意思就可以理解,有些则是就应该这么写,mybatis中独特的写法,就像java中的关键字,理解就好

 

以上是关于SM-MyBatis-13:Mybatis中多条件查询的主要内容,如果未能解决你的问题,请参考以下文章

mybatis查询中多个条件where后面怎么写

Mybatis XML动态SQL

mybatis中多表联合查询与DTO的字段对应问题

mybatis中多参数传递方式有几种

MyBatis映射文件中多参数传递的问题

MyBatis中多对多关系的映射和查询