SSM-MyBatis-14:Mybatis中智能标签

简介:   ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------     谈论到智能,有什么要想的没有? 我下面放张图   相信都见过这个吧,你在之前没有学习过框架的时候怎么写的,动态sql?还是。

 

 

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

 

 

谈论到智能,有什么要想的没有?

我下面放张图

 

相信都见过这个吧,你在之前没有学习过框架的时候怎么写的,动态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();

    }

 

打完收工

 

目录
相关文章
|
1月前
ssm(Spring+Spring mvc+mybatis)——updateDept.jsp
ssm(Spring+Spring mvc+mybatis)——updateDept.jsp
10 0
|
1月前
ssm(Spring+Spring mvc+mybatis)——showDept.jsp
ssm(Spring+Spring mvc+mybatis)——showDept.jsp
9 0
|
3月前
|
Java 关系型数据库 MySQL
基于ssm实现图书商城(spring+springmvc+mybatis)
基于ssm实现图书商城(spring+springmvc+mybatis)
|
3月前
|
Java 数据库连接 Maven
SSM框架整合:掌握Spring+Spring MVC+MyBatis的完美结合!
SSM框架整合:掌握Spring+Spring MVC+MyBatis的完美结合!
|
3月前
|
Java 数据库 数据安全/隐私保护
基于SSM框架实现管科类考研自我管理系统(分前后台spring+springmvc+mybatis+maven+jsp+jquery)
基于SSM框架实现管科类考研自我管理系统(分前后台spring+springmvc+mybatis+maven+jsp+jquery)
|
3月前
|
Java BI 数据库
基于SSM框架实现面向小微企业的简历管理系统企业简历管理系统(分前后台spring+springmvc+mybatis+maven+jsp+css+echarts)
基于SSM框架实现面向小微企业的简历管理系统企业简历管理系统(分前后台spring+springmvc+mybatis+maven+jsp+css+echarts)
|
3月前
|
前端开发 Java 关系型数据库
基于ssm框架旅游网旅游社交平台前后台管理系统(spring+springmvc+mybatis+maven+tomcat+html)
基于ssm框架旅游网旅游社交平台前后台管理系统(spring+springmvc+mybatis+maven+tomcat+html)
|
1月前
ssm(Spring+Spring mvc+mybatis)Dao层实现类——DeptDaoImpl
ssm(Spring+Spring mvc+mybatis)Dao层实现类——DeptDaoImpl
12 0
|
1月前
ssm(Spring+Spring mvc+mybatis)Dao接口——IDeptDao
ssm(Spring+Spring mvc+mybatis)Dao接口——IDeptDao
8 0
|
1月前
ssm(Spring+Spring mvc+mybatis)实体类——Dept
ssm(Spring+Spring mvc+mybatis)实体类——Dept
11 0