zoukankan      html  css  js  c++  java
  • mybatis中的转义字符

    Mybatis转义字符表
    &lt; < 小于
    &gt; > 大于
    &amp; &
    &apos; ' 单引号
    &quot; " 双引号

    需要注意的是分号是必不可少的。 比如 a > b 我们就写成  a &gt; b

    二、常用的sql语句写法
    1、模糊查询

    user_name like CONCAT("%",#{userName},"%") and

    2、月份查询

    输入月份(2019-01),查找属于这个月份的记录

    DATE_FORMAT(start_time,'%Y-%m') <![CDATA[ <= ]]> DATE_FORMAT(#{theMonth},'%Y-%m')
    and
    DATE_FORMAT(end_time,'%Y-%m') <![CDATA[ >= ]]>DATE_FORMAT( #{theMonth},'%Y-%m')
    and

    提示:DATE_FORMAT正则表达式(%Y-%m-%d %H:%i:%S)

    3、时间区间查找

    DATE_FORMAT(create_time,'%Y-%m-%d')  <![CDATA[ >= ]]> DATE_FORMAT(#{startTime},'%Y-%m-%d') 
    and
    DATE_FORMAT(create_time,'%Y-%m-%d')  <![CDATA[ <= ]]> DATE_FORMAT(#{endTime},'%Y-%m-%d') 
    and    

    全部格式化成年月日格式进行时间对比。

    4、resultMap嵌套

    <resultMap id="UserInfoResultMap" type="com..entity.User">
            <id column="id" property="id" />
            <result column="user_name" property="userName" />
            <result column="password" property="password" />
            
            <collection ofType="com.entity.Image" property="imageList">
                <id column="image_id" property="id" />
                <result column="image_path" property="imagePath" />
                <result column="image_size" property="imageSize" />
            </collection>
        </resultMap>

    collection 嵌套:
    如user类中有一个image图片类的集合List imageList,要将每个人的图片都嵌套存放在各自的imageList属性中。

    ofType:嵌套类的类名。这里就写图片类名称Image。
    property:主类中属性名。这里就填user类中的imageList。
    5、查询

    <select id="selectUserList"
            parameterType="com.param.UserParam"
            resultMap="BaseResultMap">
            SELECT *
            FROM
            user
            <trim prefix="where"  suffixOverrides="and">
                <if test="userName != null">
                    user_name LIKE CONCAT("%",#{userName},"%") and
                </if>
                
                <if test="startTime != null">
                    DATE_FORMAT(create_time,'%Y-%m-%d')  <![CDATA[ >= ]]>  DATE_FORMAT(#{startTime},'%Y-%m-%d') 
                    and
                </if>
                <if test="endTime != null">
                    DATE_FORMAT(create_time,'%Y-%m-%d')  <![CDATA[ <= ]]> DATE_FORMAT(#{endTime},'%Y-%m-%d') 
                    and
                </if>
                
                <trim prefix="id in(" suffix=")and" suffixOverrides=",">
                    <foreach collection="idList" item="tiem">
                        #{tiem},
                    </foreach>
                </trim>
                
            </trim>
        </select>

    6、批量添加

    <insert id="insertBatch"
            parameterType="com.entity.User"
            useGeneratedKeys="true" keyProperty="id">
            insert into
            user
            <trim prefix="(" suffix=")" suffixOverrides=",">
            
                name,
                age
                
            </trim>
            values
            <foreach collection="list" item="item" index="index"
                separator=",">
                <trim prefix="(" suffix=")" suffixOverrides=",">
                
                    #{item.name},
                    #{item.age},
                    
                </trim>
            </foreach>
        </insert>

    相当于insert into user (name,age)values (张,20),(李,21),(王,22)·····

    7、批量更新

    <update id="updateBatch"
            parameterType="com.safety.exam.entity.StaffAccount"
            useGeneratedKeys="true" keyProperty="id">
            update user  set 
            name =
            <foreach collection="list" item="item" index="index"
                open="case id" close="end">
                
                when #{item.id} then #{item.name}
                
            </foreach>,
            
            age =
            <foreach collection="list" item="item" index="index"
                open="case id" close="end">
                
                when #{item.id} then #{item.age}
                
            </foreach>
            
            where id in
            <foreach collection="list" index="index" item="item"
                separator="," open="(" close=")">
                #{item.id}
            </foreach>
            
        </update>

    注意: set关键字只有一个;每个foreach之间有个逗号。
    最后sql是这样:

    UPDATE categories SET
            display_order = CASE id
                WHEN 1 THEN 3
                WHEN 2 THEN 4
                WHEN 3 THEN 5
            END,
            title = CASE id
                WHEN 1 THEN 'New Title 1'
                WHEN 2 THEN 'New Title 2'
                WHEN 3 THEN 'New Title 3'
            END
        WHERE id IN (1,2,3)
  • 相关阅读:
    http://www.17sucai.com/
    vs2010 开发过程中调试时 有错误不中断
    dev 激活没有权限问题
    华为设备板卡类别
    CentOS6.8安装python2.7以及XX-Net
    PHP之封装一些常用的工具类函数
    PHP环境配置
    Ajax接收不到PHP return后的结果的原因
    PHP实现删除数组中的特定元素
    PHP用单例模式实现一个数据库类
  • 原文地址:https://www.cnblogs.com/zoro-zero/p/12511605.html
Copyright © 2011-2022 走看看