zoukankan      html  css  js  c++  java
  • Mybatis(动态sql标签)

      动态 SQL 元素和 JSTL 或基于类似 XML 的文本处理器相似。在 MyBatis 之前的版本中,有很多元素需要花时间了解。
    MyBatis 3 大大精简了元素种类,现在只需学习原来一半的元素便可。MyBatis 采用功能强大的基于 OGNL 的表达式来淘汰其它大部分元素。

      SQL标签(if,choose,where,trim,foreach)

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.zhiyou100.hhz.dao.UsersDao">
        <!-- 动态sql片段 -->
        <sql id="userscolumn">id,name,sex,age,created</sql>
        <select id="selectByWhere" parameterType="int" resultType="com.zhiyou100.hhz.bean.Users">
            select <include refid="userscolumn"/> from users
            <!-- where 可以在第一个判断成功的语句前加where 且去除and -->
            <where>
                <if test="name!=null and name!=''">
                    and name=#{name}
                </if>
                <if test="sex!=null and sex!=''">
                    and sex=#{sex}
                </if>
                <if test="age>0">
                    and age=#{age}
                </if>
            </where>
        </select>
    
        <update id="updateByWhere">
            update users
            <!-- set可以在第一个判断成功的语句前加set 且去除, -->
            <set>
                <if test="name!=null and name!=''">
                    name=#{name},
                </if>
                <if test="sex!=null and sex!=''">
                    sex=#{sex},
                </if>
                <if test="age>0">
                    age=#{age},
                </if>
                <if test="created!=null and created!=''">
                    created=#{created}
                </if>
            </set>
            where id=#{id}
        </update>
    
        <select id="selectByWhere2" resultType="com.zhiyou100.hhz.bean.Users">
            select <include refid="userscolumn"/> from users 
            <!-- trim可以代替where和set 做到类似的效果
                prefix:添加前缀
                prefixOverrides:去除前缀
                suffix:添加后缀
                suffixOverrides:去除后缀
             -->
            <trim prefix="where" prefixOverrides="and">
                <if test="name!=null and name!=''">
                    and name=#{name}
                </if>
                <if test="sex!=null and sex!=''">
                    and sex=#{sex}
                </if>
                <if test="age>0">
                    and age=#{age}
                </if>
            </trim>
        </select>
    
        <!-- 循环遍历数组
            collection:集合
            open:前缀
            close:后缀
            separator:每次遍历的间隔
            item:集合命名
         -->
        <delete id="deleteById">
            delete from users where id in
            <foreach collection="ids" open="(" close=")" separator="," item="id">
                #{id}
            </foreach>
        </delete>
    
        <select id="selectById" resultType="com.zhiyou100.hhz.bean.Users">
            select <include refid="userscolumn"/>from users 
            <where>
            <!-- choose+when+otherwise类似java中的switch+case+default -->
                <choose>
                    <when test="name!=null and name!=''">
                        name=#{name}
                    </when>
                    <when test="sex!=null and sex!=''">
                        sex=#{sex}
                    </when>
                    <when test="age>0">
                        age=#{age}
                    </when>
                    <otherwise>
                        created=#{created}
                    </otherwise>
                </choose>
            </where>
        </select>
    </mapper>
  • 相关阅读:
    98.公共汽车
    100.选菜(动态规划)01背包
    102.愤怒的LJF
    96.老鼠的旅行(动态规划)
    95.(01背包)之小吃
    94.Txx考试
    93.数字三角形W(深搜)
    POJ 3352 Road Construction (边双连通分量)
    POJ 3114 Countries in War(强联通分量+Tarjan)
    POJ 3592 Instantaneous Transference(强联通分量 Tarjan)
  • 原文地址:https://www.cnblogs.com/zfyyfw/p/11438452.html
Copyright © 2011-2022 走看看