复习:parameterType和resultType的区别 映射标签总结 动态sql sql模糊查询

一、常错点

  • parameterType
    我的理解:【传入某个类型的参数,制定参数类型

  • resultType
    我的理解:【参数成为某个对象实例
    我的错点:resultType应理解为返回参数类型
    在这里插入图片描述

二、CURD标签

只有查询的时候出现了resultType和parameterType
增删改只有parameterType

1、查询

<select id=”xxx” resultType=”xxxx” parameterType=”xxxxx”></select>

使用select标签代表这是一个查询的SQL语句
a)id:作为该SQL的唯一标志
b)parameterType:代表要执行这个select语句需要传入一个类型为long的参数,即User对象的id
【传入某个类型的参数】
c)resultType:非常重要的东西,即完成ORM的映射关系所在。这里指定的cd.xxx.mybatis.domain.User代表把结果集转换成一个User对象实例。
【参数成为某个对象的实例】
d)注意:在这里有一个要求就是,属性名称和列的名称必须一致,才能完成转型。否则就需要自定义属性到字段的映射规则。

2、新增

<insert id=”” parameterType=””></insert>

Id:作为这条SQL的唯一标志
ParameterType:指定参数类型,通常制定一个对象类型

3、修改

<update id=”” parameterType=””></update>

通过update标签标示一个update操作
id:为当前操作定义了一个名字,用于调用
parameterType:指定参数类型,通常制定一个对象类型

4、删除

<delete id=”” parameterType=””></delete>

id:为当前操作定义了一个名字,用于调用
parameterType:指定参数类型,通常制定一个对象类型

三、动态SQL

1、<sql> + <include>
Sql中可将重复的sql提取出来,使用时用include引用即可,最终达到sql重用的目的

共有部分:

<sql id="colums">
	id,name,age,sex,address
<sql/>

使用共有部分

<select id="findUserInfo" resultTpye="User">
	select
		<include refid="colums"/>
		from user where id=#{id}
<select/>

2、<where>

<select id="findUserInfo" resultTpye="User">
	select
		<include refid="colums"/>
		from user 
		<where>
			id=#{id}
		<where/>
<select/>

3、<if>

从if标签中取参数,dao层的参数必须加@param参数

<select id="ifsql" resultType="User">
		select 
		<include refid="columns"/> 
		from user
		<where>
			<if test="id!=null">
				id=#{id}
			</if>
			<if test="name!=null">
				and name=#{name}
			</if>
			<if test="addr!=null">
				and addr=#{addr}
			</if>
		</where>

4、<set>

<update id="upDateAge">
	update user 
	<set>
		age=20 where id=12
	</set>
	</update>

5、<foreach>

用于批量操作,批量进行CRUD,集合参数有三种用法:array,list,map

  • array
<select id="foreacharray" 
			resultType="User">
		select * from user
		where id in(
			<!-- foreach用来遍历数组
				collection:指定数据的类型
				separator:拼接SQL时用的分隔符
				item:遍历时数据的别名
			 -->
			<foreach collection="array"
					separator=","
					item="o">
				#{o}
			</foreach>
		)
	</select>
  • list
<!-- foreach标签 list-->
	<delete id="foreachlist">
		delete from user where id in 
		<!-- open:指SQL拼接时的左括号
			close:指SQL拼接时的右括号
		 -->
		<foreach collection="list" 
				separator=","
				item="o"
				open="("
				close=")">
			#{o}
		</foreach>
	
	</delete>
  • map
<!-- item表示集合中每一个元素进行迭代时的别名,
			index:指 定一个名字,用于表示在迭代过程中,每次迭代到的位置,
			open:表示该语句以什么开始,
			separator:表示在每次进行迭代之间以什么符号作为分隔 符,
			close:表示以什么结束。
			key : 是传入的参数Map的key
 		-->
<update id="foreachmap">
		update user set age=18 where id in(
			<foreach collection="key" separator="," item="a">
				#{a}
			</foreach>
		)
	</update>	

四、模糊查询防sql注入

1、参数中直接加入%%
如果要发起一条SQL,where name like '%#{name}%',name手动传入%大%这里就容易发生SQL注入问题
在这里插入图片描述
2、Sql中用concat函数防止sql注入

<select id="findone2" resultType="User" parameterType="map">
		select <include refid="cols"></include> from user
			<where>
		 		id=#{id}  and addr=#{addr} and name like  concat(concat('%',#{name}),'%')
			</where>
</select>

实际应用:

<select id="getXXXXXXXXX" resultType="java.util.Map" >
	     select 
	      (select count(*) from C) as AAA,
	      (select count(*) from xxx where  C= 'xxxxxxx') as BBB,
	      (select count(*) from xxx where  C like  CONCAT('%','停止','%')) as CCC,
	      (select count(*) from xxx where  C like  CONCAT('%','失败','%')) as DDD from xxx 
	      GROUP BY C LIMIT 0,1
	</select>
  • 11
    点赞
  • 48
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值