映射文件的顶级元素
- select:映射查询语句
- insert:映射插入语句
- update:映射更新语句
- delete:映射删除语句
- sql:可以重用的sql代码块
- resultMap:最复杂,最有力量的元素,用来描述如何从数据库结果集中加载你的对象
- cache:配置给定命名空间的缓存
- cache-ref:从其他命名空间引用缓存配置
select标签
<select
<!--
1. id(必须配置)
id是命名空间中的唯一标识符,可被用来代表这条语句
一个命名空间(namespace)对应一个dao接口
这个id也应该对应dao里面的某个方法(sql相当于方法的实现),因此id应该与方法名一致
-->
id="selectUser"
<!--
2. parameterType(可选配置,默认由mybatis自动选择处理)
将要传入语句的参数的完全限定名或别名,如果不配置,mybatis会通过ParameterHandler根据参数类型默认选择合适的typeHandler进行处理
parameterType 主要指定参数类型,可以是int, short, long, string等类型,也可以是复杂类型(如对象)
-->
parameterType="int"
<!--
3. resultType(resultType 与 resultMap 二选一配置)
用来指定返回类型,指定的类型可以是基本类型,也可以是java容器,也可以是javabean
-->
resultType="hashmap"
<!--
4. resultMap(resultType 与 resultMap 二选一配置)
用于引用我们通过 resultMap 标签定义的映射类型,这也是mybatis组件高级复杂映射的关键
-->
resultMap="USER_RESULT_MAP"
<!--
5. flushCache(可选配置)
将其设置为true,任何时候语句被调用,都会导致本地缓存和二级缓存被清空,默认值:false
-->
flushCache="false"
<!--
6. useCache(可选配置)
将其设置为true,会导致本条语句的结果被二级缓存,默认值:对select元素为true
-->
useCache="true"
<!--
7. timeout(可选配置)
这个设置是在抛出异常之前,驱动程序等待数据库返回请求结果的秒数,默认值为:unset(依赖驱动)
-->
timeout="10000"
<!--
8. fetchSize(可选配置)
这是尝试影响驱动程序每次批量返回的结果行数和这个设置值相等。默认值为:unset(依赖驱动)
-->
fetchSize="256"
<!--
9. statementType(可选配置)
STATEMENT, PREPARED或CALLABLE的一种,这会让MyBatis使用选择Statement, PrearedStatement或CallableStatement,默认值:PREPARED
-->
statementType="PREPARED"
<!--
10. resultSetType(可选配置)
FORWARD_ONLY,SCROLL_SENSITIVE 或 SCROLL_INSENSITIVE 中的一个,默认值为:unset(依赖驱动)
-->
resultSetType="FORWORD_ONLY"
></select>
insert、update、delete标签与之相似。
resultMap标签
<!--
1. type 对应的返回类型,可以是javabean, 也可以是其它
2. id 必须唯一, 用于标示这个resultMap的唯一性,在使用resultMap的时候,就是通过id引用
3. extends 继承其他resultMap标签
-->
<resultMap type="" id="" extends="">
<!--
1. id 唯一性,注意啦,这个id用于标示这个javabean对象的唯一性, 不一定会是数据库的主键(不要把它理解为数据库对应表的主键)
2. property 属性对应javabean的属性名
3. column 对应数据库表的列名
(这样,当javabean的属性与数据库对应表的列名不一致的时候,就能通过指定这个保持正常映射了)
-->
<id property="" column=""/>
<!--
result 与id相比,对应普通属性
-->
<result property="" column=""/>
<!--
constructor 对应javabean中的构造方法
-->
<constructor>
<!-- idArg 对应构造方法中的id参数 -->
<idArg column=""/>
<!-- arg 对应构造方法中的普通参数 -->
<arg column=""/>
</constructor>
<!--
collection 为关联关系,是实现一对多的关键
1. property 为javabean中容器对应字段名
2. ofType 指定集合中元素的对象类型
3. select 使用另一个查询封装的结果
4. column 为数据库中的列名,与select配合使用
-->
<collection property="" column="" ofType="" select="">
<!--
当使用select属性时,无需下面的配置
-->
<id property="" column=""/>
<result property="" column=""/>
</collection>
<!--
association 为关联关系,是实现一对一的关键
1. property 为javabean中容器对应字段名
2. javaType 指定关联的类型,当使用select属性时,无需指定关联的类型
3. select 使用另一个select查询封装的结果
4. column 为数据库中的列名,与select配合使用
-->
<association property="" column="" javaType="" select="">
<!--
使用select属性时,无需下面的配置
-->
<id property="" column=""/>
<result property="" column=""/>
</association>
</resultMap>
sql标签
<sql id="userColumns">id,username,password</sql>
<!-- 重用上方定义sql -->
<select id="selectProjectList" paramertType="int" resultType="hashmap">
SELECT
<include refid="userColumns"/>
FROM
t_project_002_project_info
</select>
foreach标签
<!-- foreach -->
<delete id="delMulti" parameterType="java.util.List">
delete from user where id in
<!--collection:输入参数为List集合时,必须写list,
item:为集合里的每一项起名,可以任意定义
separator:每一项中间的分割符
open:在执行循环体之前拼接的内容;
close:在执行循环体之后拼接的内容;-->
<foreach collection="list" item="uid" separator="," open="(" close=")">
#{uid}
</foreach>
</delete>
where标签
<!--where:解析成where关键字,会自动去掉第一个符合条件的限定条件中的and -->
<select id="getByNameSex" parameterType="map" resultType="user">
select * from user
<where>
<if test="uname!=null and uname!=''">
and username like "%"#{uname}"%"
</if>
<if test="usex!=null and usex!=''">
and sex=#{usex}
</if>
</where>
choose when otherwise标签
<!--choose when otherwise:某个判断满足条件后,其他条件就不会再执行 -->
<select id="getByNameSex1" parameterType="map" resultType="user">
select * from user where
<choose>
<when test="uname!=null and uname!=''">
username like "%"#{uname}"%"
</when>
<when test="usex!=null and usex!=''">
sex=#{usex}
</when>
<otherwise>
1=1
</otherwise>
</choose>
</select>
set标签
<!-- set:解析为set关键字,可以自动去掉最后一个更新的字段后面的逗号 -->
<update id="updUser" parameterType="user">
update user
<set>
<if test="username!=null and username!=''">
username=#{username},
</if>
<if test="sex!=null and sex!=''">
sex=#{sex}
</if>
</set>
where id=#{id}
</update>
trim标签
<update id="updUser1" parameterType="user">
<!--prefix:前缀,在trim中内容执行之前拼接
suffix:后缀:在trim中内容执行之后拼接
suffixOverrides:忽略后缀
prefixOverrides:忽略前缀-->
<trim prefix="update user set" suffix="where id=#{id}" suffixOverrides=",">
<if test="username!=null and username!=''">
username=#{username},
</if>
<if test="sex!=null and sex!=''">
sex=#{sex}
</if>
</trim>
</update>