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

  • 相关阅读:
    NSURLConnection与NSURLSession的区别
    微信支付流程
    支付宝支付流程
    配置openfire问题(Oracle)
    id和instancetype
    数组和集合
    静态变量,静态常量以及全局变量
    内存的那些事
    Block循环引用解析
    线程的几个小问题
  • 原文地址:https://www.cnblogs.com/DawnCHENXI/p/8467542.html
Copyright © 2020-2023  润新知