1. 程式人生 > >SM-MyBatis-14:Mybatis中智能標簽

SM-MyBatis-14:Mybatis中智能標簽

IT log 分享圖片 -- void 分享 sql item foreach

------------吾亦無他,唯手熟爾,謙卑若愚,好學若饑-------------

談論到智能,有什麽要想的沒有?

我下面放張圖

技術分享圖片

相信都見過這個吧,你在之前沒有學習過框架的時候怎麽寫的,動態sql?還是。。。

智能標簽可以解決類似問題

它可以在sql語句中隨傳入參數是否為null甚至其他來自行加where或者and,或者其他等等用法

他分為 where ,if ,choose ,foreach的array方式 ,foreach的list方式 ,foreach的list自定義類型方式

我一塊放在下面,對照著看吧

實體類

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; } }

接口中的方法

//智能標簽where if
    public List<Book> findtrueBookByIf(String bookName,Integer bookPrice);
    //智能標簽where choose
    public List<Book> findtrueBookByChoose(Integer bookPrice);
    //智能標簽where foreach array
    public List<Book> findtrueBookByForeachArray(int [] array);
    //智能標簽where foreach list
    public List<Book> findtrueBookByForeachList(List<Integer> list);
    //智能標簽where foreach list
    public List<Book> findtrueBookByForeachListBook(List<Book> list);

小配置中

<!--智能標簽,where if-->
    <select id="findtrueBookByIf" resultType="Book">
        select * from book
        <where>
            <if test="#{0}!=null">
                AND bookName LIKE % #{0} %
            </if>
            <if test="#{0}!=null">
                AND bookPrice>#{1}
            </if>
        </where>
    </select>
    <!--智能標簽,where choose-->
    <select id="findtrueBookByChoose" resultType="Book">
        select * from book
        <where>
            <choose>
                <when test="#{0}!=null">
                    AND bookPrice>#{0}
                </when>
                <otherwise>1=1</otherwise>
            </choose>
        </where>
    </select>
    <!--智能標簽,where foreach array-->
    <select id="findtrueBookByForeachArray" resultType="Book">
        select * from book
        <where>
            bookID IN 
            <foreach collection="array" open="(" close=")" separator="," item="myid">
                #{myid}
            </foreach>
        </where>
    </select>
    <!--智能標簽,where foreach list-->
    <select id="findtrueBookByForeachList" resultType="Book">
        select * from book
        <where>
            bookID IN
            <foreach collection="list" open="(" close=")" separator="," item="myid">
                #{myid}
            </foreach>
        </where>
    </select>
    <!--智能標簽,where foreach list book-->
    <select id="findtrueBookByForeachListBook" resultType="Book">
        select * from book
        <where>
            bookID IN
            <foreach collection="list" open="(" close=")" separator="," item="book">
                #{book.bookID}
            </foreach>
        </where>
    </select>

測試類中

///智能標簽where + foreach list Book自定義list 進行多條件查詢
    @Test
    public void t9selectZhiNengByForeachListBook(){
        SqlSession session= MyBatisUtils.getSession();

        IBookDAO mapper = session.getMapper(IBookDAO.class);
        List<Book> list=new ArrayList<Book>();
        Book b1=new Book();
        b1.setBookID(1);
        Book b2=new Book();
        b2.setBookID(2);
        list.add(b1);
        list.add(b2);
        List<Book> books = mapper.findtrueBookByForeachListBook(list);
        for (Book items:books) {
            System.out.println(items.getBookName());
        }

        session.close();

    }


    ///智能標簽where + foreach list 進行多條件查詢
    @Test
    public void t8selectZhiNengByForeachList(){
        SqlSession session= MyBatisUtils.getSession();

        IBookDAO mapper = session.getMapper(IBookDAO.class);
        List<Integer> list=new ArrayList<Integer>();
        list.add(1);
        list.add(3);
        List<Book> books = mapper.findtrueBookByForeachList(list);
        for (Book items:books) {
            System.out.println(items.getBookName());
        }

        session.close();

    }


    ///智能標簽where + foreach array 進行多條件查詢
    @Test
    public void t7selectZhiNengByForeachArray(){
        SqlSession session= MyBatisUtils.getSession();

        IBookDAO mapper = session.getMapper(IBookDAO.class);
        int[] array={1,3};
        List<Book> books = mapper.findtrueBookByForeachArray(array);
        for (Book items:books) {
            System.out.println(items.getBookName());
        }

        session.close();

    }



    ///智能標簽where + choose進行多條件查詢
    @Test
    public void t6selectZhiNengByChoose(){
        SqlSession session= MyBatisUtils.getSession();

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

        session.close();

    }


    ///智能標簽where + if 進行多條件查詢
    @Test
    public void t5selectZhiNengByIf(){
        SqlSession session= MyBatisUtils.getSession();

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

        session.close();

    }

打完收工

SM-MyBatis-14:Mybatis中智能標簽