首页 > 其他 > 详细

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

时间:2018-02-24 21:24:51      阅读:174      评论:0      收藏:0      [点我收藏+]

 

 

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

 

 

实体类

 

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中多条件查询

原文:https://www.cnblogs.com/DawnCHENXI/p/8467542.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!