Mybatis
概述
mybatis是一款基于ORM轻量级框架,底层封装了JDBC。
ORM
o:应用对象,实体类
r:数据库表
m:建立o与r之间的映射关系
- 属性名称与数据库字段完全一致(resultMap)
- 属性名称与数据库字段符合驼峰命名(resultMap)
- 属性名称与数据库字段不符合1和2,则自定义封装(resultMap)
动态SQL
- if
<select id="queryByCount" parameterType="Map" resultType="Long">
select count(1) from rms_personnel
<where>
<!--名字-->
<if test="name!=null and name!='' ">
name like concat('%',#{name},'%')
</if>
</where>
</select>
- where
- choose
<select id="getSubsidyPersonnel" resultType="Map">
select id,name,unit,card,grade from rms_personnel
where
<choose>
<when test="type==1">
property_subsidies=1
</when>
<otherwise>
vehicles_subsidies=1
</otherwise>
</choose>
</select>
- foreach
collection:迭代元素的类型
item:循环中的对象
index:下标
open:循环开始前添加的元素
close:循环结束后添加的元素
separator:循环分隔符,每循环一次添加一次分隔符
<delete id="deletes" parameterType="int">
delete from rms_subsidy where id in
<foreach collection="array" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</delete>
[11,22,33,44]
<foreach collection="array" item="id" index="i">
#{id} 11,22,33,44
#{i} 0,1,2,3
</foreach>
{name:admin,age:22}
<foreach collection="map" item="value" index="key">
#{key} name,age
#{value} admin,22
</foreach>
5.set
<update id="update" parameterType="subsidy">
update rms_subsidy
set money=#{money}
where id = #{id}
</update>