Mybatis 中的转义字符及常用查询

一、转译符

1、特殊字符转译

&lt;<小于
&gt;>大于
&amp;&
&apos;单引号
&quot;"双引号

需要注意的是分号是必不可少的。 比如 a > b 我们就写成 a &gt; b
(分号需为英文状态下的,应为英文分号会将转译符直接显示为对应的符号,所以本文都是中文下的)

2、<![CDATA[ ]]>

在mybatis中这种符号将不会解析。如

<![CDATA[ min(starttime)<='12:00' and max(endtime)<='12:00' ]]> 

二、常用的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)
  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值