zoukankan      html  css  js  c++  java
  • Mybatis框架五:动态SQL

    1.if   where

    实现一个简单的需求:

    根据性别和名字查询用户:

    正常来写:

        <select id="selectUserBySexAndUsername" parameterType="pojo.User"
            resultType="pojo.User">
            select * from user where sex = #{sex} and username = #{username}
        </select>

    弊端:传入参数user必须要有性别和姓名,缺一不可

    于是我稍做修改:

        <select id="selectUserBySexAndUsername" parameterType="pojo.User"
            resultType="pojo.User">
            select * from user
            where
            <if test="sex != null and sex != ''">
                sex = #{sex}
            </if>
            <if test="username != null and username != ''">
                and username = #{username}
            </if>
        </select>

    看似可以了,但是,如果sex不存在而username存在,SQL语句错误

    这时候,只需要加入一个Where标签即可:

        <select id="selectUserBySexAndUsername" parameterType="pojo.User"
            resultType="pojo.User">
            select * from user
            <where>
                <if test="sex != null and sex != ''">
                    sex = #{sex}
                </if>
                <if test="username != null and username != ''">
                    and username = #{username}
                </if>
            </where>
        </select>

    现在就可以满足需求了,只要至少存在一个条件即可查询成功

    2.SQL片段:

    很多的SQL标签内SQL语句重复,那么我们可以提出来共用吗?

    可以:

        <sql id="selector">
            select * from user
        </sql>
    
        <select id="selectUser">
            <include refid="selector" />
            <where>
                <if test="sex != null and sex != ''">
                    sex = #{sex}
                </if>
                <if test="username != null and username != ''">
                    and username = #{username}
                </if>
            </where>
        </select>

    这种设计思想值得学习,不过实际开发种并不会使用

    3.foreach

    情景:根据多个ID查询用户:

    先写个包装类,里面有一个存用户ID的List

    (先展示包装类的方法是因为直接用数组或者List会有坑,下边会解释)

    package pojo;
    
    import java.io.Serializable;
    import java.util.List;
    
    public class QueryVo implements Serializable {
    
        private static final long serialVersionUID = 1L;private List<Integer> idsList;public List<Integer> getIdsList() {
            return idsList;
        }
    
        public void setIdsList(List<Integer> idsList) {
            this.idsList = idsList;
        }
        
    }

    查询:

        <select id="selectUserByIds" parameterType="pojo.QueryVo"
            resultType="pojo.User">
            select * from user
            <where>
                <foreach collection="idsList" item="id" separator="," open="id in ("
                    close=")">
                    #{id}
                </foreach>
            </where>
        </select>

    不过这里要特别注意:如果foreach循环的不是List集合,而是简单的数组:

    collection不能写成属性名(如这里的idsList),而是要写成array

    因此可见,如果我们foreach循环的不是包装类属性,而直接是List集合,

    Collection也应该写成list

  • 相关阅读:
    什么是HTTP
    通过递归法解决阶梯问题(n个台阶,上楼可以一步上1阶,也可以一步上2阶,一共有多少种上楼的方法)
    在Intelli Idea中使用plantuml(plantuml时序图的使用)
    Java中if(boolean)与if(boolean=true)的区别
    实现一个Servlet程序
    退出mysql的编辑模式
    mysql数据库基本操作命令行
    通过mysql命令查看mysql服务实例支持的搜索引擎
    Mac环境下使用终端启动Mysql,并进行mysql数据库的连接
    路飞学城Python-Day4
  • 原文地址:https://www.cnblogs.com/xuyiqing/p/9409499.html
Copyright © 2011-2022 走看看