SSM-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中的关键字,理解就好

 

posted @ 2018-02-24 20:20  晨曦Dawn  阅读(785)  评论(0编辑  收藏  举报