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)

原文地址:https://www.cnblogs.com/zoro-zero/p/12511605.html

时间: 2024-08-30 01:42:29

mybatis中的转义字符的相关文章

Mybatis 中的转义字符

记录以下mybatis中的转义字符,方便以后自己看一下 Mybatis转义字符表 < < 小于 > > 大于 & & 与 &apos; ' 单引号 " " 双引号 需要注意的是分号是必不可少的. 比如 a > b 我们就写成  a > b 当然啦, 我们也可以用另外一种,就是<![CDATA[ ]]>符号. 在mybatis中这种符号将不会解析. 比如 <![CDATA[ when min(starttime

mybatis中写sql语句时需要转义的字符

mybatis配置文件,sql语句中含有转义字符: 错误语句: select * from table_base where flag_topic  & #{topic_num} 错误信息: Caused by: org.xml.sax.SAXParseException; lineNumber: 8; columnNumber: 54; The entity name must immediately follow the '&' in the entity reference. 正确语

2017年9月3日 Spring及Mybatis中连接数据库的不同方式

连接数据库用spring和mybatis中使用的方法可以不同,mybaits可以不用写数据库的配置文件 Spring的连接方法 <!-- 读取属性文件(.properties)的内容 --> <!-- location:指定要读取的属性文件的位置及文件名. 注: classpath:表示依据类路径去查找 容器依据路径读取属性文件的内容, 并且将这些内容存放到Properties对象上 --> //数据库的登入数据文件 //文件名db.properties #db connectio

mybatis中&quot;#&quot;和&quot;$&quot;的区别

mybatis中"#"和"$"的区别 动态 sql 是 mybatis 的主要特性之一,在 mapper 中定义的参数传到 xml 中之后,在查询之前 mybatis 会对其进行动态解析.mybatis 为我们提供了两种支持动态 sql 的语法:#{} 以及 ${}. 在下面的语句中,如果 username 的值为 zhangsan,则两种方式无任何区别: select * from user where name = #{name}; select * from

mybatis中的mapper接口文件以及example类的实例函数以及详解

##Example example = new ##Example(); example.setOrderByClause("字段名 ASC"); //升序排列,desc为降序排列. example.setDistinct(false)//去除重复,boolean型,true为选择不重复的记录. Criteria criteria = new Example().createCriteria(); is null;is not null; equal to(value);not equ

MyBatis中如何通过继承SqlSessionDaoSupport来编写DAO(一)

在MyBatis中,当我们编写好访问数据库的映射器接口后,MapperScannerConfigurer就能自动成批地帮助我们根据这些接口生成DAO对象(),然后我们再使用Spring把这些DAO对象注入到业务逻辑层的对象(Service类的对象).因此,在这种情况下的DAO层,我们几乎不用编写代码,而且也没有地方编写,因为只有接口.这固然方便,不过如果我们需要在DAO层写一些代码的话,这种方式就无能为力了.此时,MyBatis-Spring提供给我们的SqlSessionDaoSupport类

MyBatis中的OGNL教程

MyBatis中的OGNL教程 有些人可能不知道MyBatis中使用了OGNL,有些人知道用到了OGNL却不知道在MyBatis中如何使用,本文就是讲如何在MyBatis中使用OGNL. 如果我们搜索OGNL相关的内容,通常的结果都是和Struts有关的,你肯定搜不到和MyBatis有关的,虽然和Struts中的用法类似但是换种方式理解起来就有难度. MyBatis常用OGNL表达式 e1 or e2 e1 and e2 e1 == e2,e1 eq e2 e1 != e2,e1 neq e2

myBatis中的注解@Param、返回值为Map、JAVA读取Excel并解析文本、Class.getResource()和ClassLoader.getResource()

myBatis中的注解@Param:http://blog.csdn.net/gao36951/article/details/44258217:  http://www.cnblogs.com/thomas12112406/p/6217211.html. myBatis返回值为Map:http://blog.csdn.net/werewr342352321df/article/details/11892755. ====================== JAVA读取Excel并解析文本:h

mybatis中xml的sql之test中文报错

在mybatis中sql,test中文报错( java.lang.NumberFormatException 这句话明确告诉了我们是数字格式异常).需加.tostring(). <if test="bookName == '毛选集'.tostring() "> and b.book_Name like #{bookName} </if>