一、常错点
-
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>