官网文档:mybatis 官网
mapper 中的几个顶级元素:
- cache
- cache-ref
- resultMap
parameterMap- sql
- insert
- delete
- update
- select
SELECT
<select
id="selectPerson"
parameterType="int"
parameterMap="deprecated"
resultType="hashmap"
resultMap="personResultMap"
flushCache="false"
useCache="true"
timeout="10"
fetchSize="256"
statementType="PREPARED"
resultSetType="FORWARD_ONLY">
注:resultType 与 resultMap 中只能二选一。
resultType 能够是任意包装类型 / 实体类;而 resultMap 更加弱小。
能够先看一篇文章先理解一下:[ResultType 与 ResultMap 区别]
(https://blog.csdn.net/weixin_…
flushCache:调用 sql 后,清空本地缓存和二级缓存,默认值为 false。
useCache:调用后二级缓存存下来,对 select 默认为 true。
statementType: PREPARED|STATEMENT|CALLABLE, 别离对应相应的 statement 类型。
INSERT/DELETE/UPDATE
<insert
id="insertAuthor"
parameterType="domain.blog.Author"
flushCache="true"
statementType="PREPARED"
keyColumn=""useGeneratedKeys=""
keyProperty=""timeout="20">
<update
id="updateAuthor"
parameterType="domain.blog.Author"
flushCache="true"
statementType="PREPARED"
timeout="20">
<delete
id="deleteAuthor"
parameterType="domain.blog.Author"
flushCache="true"
statementType="PREPARED"
timeout="20">
次级元素 (顶级标签下的标签)
- foreach
<foreach item="item" collection="list" separator=",">
(#{item.username}, #{item.password}, #{item.email}, #{item.bio})
</foreach>
- selectKey
<selectKey keyProperty="id" resultType="int" order="BEFORE">
select CAST(RANDOM()*1000000 as INTEGER) a from SYSIBM.SYSDUMMY1
</selectKey>
- sql 定义可重用的代码片段
定义
<sql id="userColumns"> ${alias}.id,${alias}.username,${alias}.password </sql>
应用
<select id="selectUsers" resultType="map">
select
<include refid="userColumns"><property name="alias" value="t1"/></include>,
<include refid="userColumns"><property name="alias" value="t2"/></include>
from some_table t1
cross join some_table t2
</select>
- 动静 sql(if,choose/when/otherwise,trim,where,set,bind)