官网文档: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)