• Java--MyBatis动态SQL;<if>,<where>,<foreach>标签


    Mybatis主要使用标签

    1、对应注解@lSelect

    2、对应注解@Update

    3、对应注解@Insert

    4、对应注解@Delete

    5、在某些条件根据入参有无决定是可使用以避免1=1这种写法,也会根据是否为where条件后第一个条件参数自动去除and

    6、:类似于java中的条件判断if,没有标签

    7、标签

    1. <choose>
    2. <when></when>
    3. <otherwise></otherwise>
    4. </choose>

    8、:可以对数组、Map或实现了Iterable接口(如List、Set)的对象遍历。可实现in、批量更新、批量插入等。

    9、:映射结果集

    10、:映射结果类型,可是java实体类或Map、List等类型

    动态sql:

    sql内容是变化的,可以根据条件获取到不同的sql语句,主要是where部分发生变化

    动态sql的实现,使用的是mybatis提供的标签:

    一、标签

    标签的执行,当 test 的值为 true 时,会将其包含的 SQL 片断拼接到其所在的 SQL 语句

    语法:<if test="判断java对象的属性值">

    如下:

    1. <select id="selectUsersIf" resultType="com.mycompany.domain.User">
    2. select user_id,user_name,email,age
    3. from user
    4. where
    5. <if test="userName != null and userName != ''">
    6. user_name = #{userName}
    7. if>
    8. <if test="age > 0">
    9. or age > #{age}
    10. if>
    11. select>

    这样会存在一个问题:如果 age 属性值为null,则会造成SQL语句错误,多带一个 "or" 字段,因此我们在where 条件语句后面加一个 恒成立等式,如 "1 = 1","user_id > 0",如下格式:

    1. <select id="selectUsersIf" resultType="com.mycompany.domain.User">
    2. select user_id,user_name,email,age
    3. from user
    4. where user_id > 0
    5. <if test="userName != null and userName != ''">
    6. and user_name = #{userName}
    7. if>
    8. <if test="age > 0">
    9. or age > #{age}
    10. if>
    11. select>

    标签的中存在一个问题:需要在 where 后手工添加 1=1 恒成立的子句;因为若 where 后 的所有条件均为 false,而 where 后若又没有 1=1 子句,则 SQL 中就会只剩下一个空的 where,回导致SQL 出错

    二、标签

    标签用来包含 多个的, 当多个if有一个成立的, 会自动增加一个where关键字,并去掉 if中多余的 and ,or等

    使用标签,在有查询条件时,可以自动添加上 where 子句;没有查询条件时,不会添加 where 子句

    注:

    第一个标签中的 SQL 片断,可以不包含 and。但写上 and 也不错, 系统会将多出的 and 去掉

    其它中 SQL 片断的 and 或者 or,必须写上;否则 SQL 语句将拼接出错

    语法:<where> 其他动态 sql </where>

     如下:

    1. <select id="selectUsersWhere" resultType="com.mycompany.domain.User">
    2. <include refid="selectUser" />
    3. <where>
    4. <if test="userName != null and userName != ''">
    5. user_name = #{userName}
    6. if>
    7. <if test="age > 0">
    8. or age > #{age}
    9. if>
    10. where>
    11. select>

    三、标签

    循环java中的数组,list集合的;主要用在sql的in语句中

    1. 语法:
    2. collection:表示接口中的方法参数的类型, 如果是数组使用array , 如果是list集合使用list
    3. item:自定义的,表示数组和集合成员的变量
    4. open:循环开始时的字符
    5. close:循环结束时的字符
    6. separator:集合成员之间的分隔符
    7. <foreach collection="集合类型" item="自定义字段" open="开始字符" close="结束字符" separator="分隔字符">
    8. #{item的值}
    9. </foreach>

    collection:表示接口中的方法参数的类型, 如果是数组使用array , 如果是list集合使用list
    item:自定义的,表示数组和集合成员的变量
    open:循环开始时的字符
    close:循环结束时的字符
    separator:集合成员之间的分隔符

    1、遍历List<简单类型>

    1. <select id="selectForeachOne" resultType="com.mycompany.domain.User">
    2. select user_id,user_name,email,age
    3. from user
    4. <if test="list !=null and list.size > 0 ">
    5. where user_id in
    6. <foreach collection="list" item="myId" open="(" close=")" separator=",">
    7. #{myId}
    8. foreach>
    9. if>
    10. select>

    2、遍历List<对象类型>

    1. <select id="selectForeachTwo" resultType="com.mycompany.domain.User">
    2. select user_id,user_name,email,age
    3. from user
    4. <if test="list !=null and list.size > 0 ">
    5. where user_id in
    6. <foreach collection="list" item="user" open="(" close=")" separator=",">
    7. #{user.userId}
    8. foreach>
    9. if>
    10. select>

    我们也可将上述写法手动拼接,如:

    1. <select id="selectForeachTwo2" resultType="com.mycompany.domain.User">
    2. select user_id,user_name,email,age
    3. from user
    4. <if test="list !=null and list.size > 0 ">
    5. where user_id in (
    6. <foreach collection="list" item="user">
    7. #{user.userId},
    8. foreach>
    9. -1 )
    10. if>
    11. select>

    四、SQL代码片段

    标签用于定义 SQL 片断,以便其它 SQL 标签复用;其它标签使用该 SQL 片断,使用 子标签

    1. 1、定义 <sql id="自定义名称唯一"> sql语句, 表名,字段等 sql>
    2. 2、使用, <include refid="id的值" />

    定义SQL片段如下:

    1. <sql id="selectUser">
    2. select user_id,user_name,email,age
    3. from user
    4. sql>
    5. <sql id="selectUserOne">
    6. user_id,user_name,email,age
    7. sql>

    使用SQL片段如下:

    1. <select id="selectUsersWhere" resultType="com.mycompany.domain.User">
    2. <include refid="selectUser" />
    3. <where>
    4. <if test="userName != null and userName != ''">
    5. user_name = #{userName}
    6. if>
    7. <if test="age > 0">
    8. or age > #{age}
    9. if>
    10. where>
    11. select>

    五、mybatis判断list长度与数组长度

    1、如果使用数组作为参数,那么使用length字段

     <if test="dataArr!=null and dataArr.length >0">

    2、如果使用List作为参数,那么使用size()方法

     <if test="dataList!=null and dataList.size() >0">
  • 相关阅读:
    [附源码]JAVA毕业设计高校在线教师教学学术能力评价系统(系统+LW)
    grafana使用小结
    Photoshop_00000
    【电源设计】11变压器在开关电源中的应用
    数字信号处理——CFAR检测器设计(3)
    使用 PMML 实现模型融合及优化技巧
    多线程并发或线程安全问题如何解决
    LINUX漏洞复现篇之ShellShock漏洞
    通过RedisTemplate简单实现延时队列
    服务端技术方案模板参考
  • 原文地址:https://blog.csdn.net/MinggeQingchun/article/details/122809764