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

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/fengdong1996/article/details/86186871

一、转译符

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)

猜你喜欢

转载自blog.csdn.net/fengdong1996/article/details/86186871