Mapper.xml解析

来源:互联网 发布:淘宝怎么拒收退款流程 编辑:程序博客网 时间:2024/05/22 01:40
  • cache – 给定命名空间的缓存配置。
  • cache-ref – 其他命名空间缓存配置的引用。
  • resultMap – 是最复杂也是最强大的元素,用来描述如何从数据库结果集中来加载对象。
  • parameterMap – 已废弃!老式风格的参数映射。内联参数是首选,这个元素可能在将来被移除,这里不会记录。
  • sql – 可被其他语句引用的可重用语句块。
  • insert – 映射插入语句
  • update – 映射更新语句
  • delete – 映射删除语句
  • select – 映射查询语句 

(1)select
<select
<!--在命名空间中唯一的标识符,可以被用来引用这条语句。-->
id="selectPerson"
<!--将会传入这条语句的参数类的完全限定名或别名。-->
parameterType="int"
<!--已弃用-->
parameterMap="deprecated"
<!--从这条语句中返回的期望类型的类的完全限定名或别名。-->
resultType="hashmap"
<!--外部 resultMap 的命名引用。-->
resultMap="personResultMap"
<!--将其设置为 true,任何时候只要语句被调用,都会导致本地缓存和二级缓存都会被清空,默认值:false-->
flushCache="false"
<!--将其设置为 true,将会导致本条语句的结果被二级缓存,默认值:对 select 元素为 true-->
useCache="true"
<!--这个设置是在抛出异常之前,驱动程序等待数据库返回请求结果的秒数。默认值为 unset(依赖驱动)。-->
timeout="10000"
<!--这是尝试影响驱动程序每次批量返回的结果行数和这个设置值相等。默认值为 unset(依赖驱动)-->
fetchSize="256"
<!--STATEMENTPREPARED CALLABLE 的一个。默认值:PREPARED-->
statementType="PREPARED"
<!--FORWARD_ONLYSCROLL_SENSITIVE SCROLL_INSENSITIVE 中的一个,默认值为 unset (依赖驱动)。-->
resultSetType="FORWARD_ONLY">
SELECT * FROM PERSON WHERE ID = #{id}
</select>
statementType:
STATEMENT,PREPARED 或 CALLABLE 的一个。
这会让 MyBatis 分别使用 Statement,PreparedStatement 或 CallableStatement,默认值:PREPARED

(2)insert、update、delete
<insert
id="insertAuthor"
parameterType="domain.blog.Author"
flushCache="true"
statementType="PREPARED"
<!--(仅对 insert update 有用)唯一标记一个属性,MyBatis 会通过 getGeneratedKeys 的返回值或者通过 insert 语句的 selectKey 子元素设置它的键值-->
keyProperty=""
<!--(仅对 insert update 有用)通过生成的键值设置表中的列名-->
keyColumn=""
<!--(仅对 insert update 有用)这会令 MyBatis 使用 JDBC getGeneratedKeys 方法来取出由数据库内部生成的主键-->
useGeneratedKeys=""
timeout="20">
</insert>

<update
id="updateAuthor"
parameterType="domain.blog.Author"
flushCache="true"
statementType="PREPARED"
timeout="20">
</update>
<delete
id="deleteAuthor"
parameterType="domain.blog.Author"
flushCache="true"
statementType="PREPARED"
timeout="20">
</delete>

(3)sql
这个元素可以被用来定义可重用的 SQL 代码段,可以包含在其他语句中。
<!--这个元素可以被用来定义可重用的 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>

(4)resultMap
<!--id 一个 ID 结果;标记结果作为 ID 可以帮助提高整体效能-->
<resultMap id="detailedBlogResultMap" type="Blog">
<!--constructor - 类在实例化时,用来注入结果到构造方法中-->
<constructor>
<!--idArg - ID 参数;标记结果作为 ID 可以帮助提高整体效能-->
<!--arg - 注入到构造方法的一个普通结果-->
<idArg column="blog_id" javaType="int"/>
</constructor>

<!--result – 注入到字段或 JavaBean 属性的普通结果-->
<result property="title" column="blog_title"/>

<!--association – 一个复杂的类型关联;许多结果将包成这种类型-->
<!--嵌入结果映射结果映射自身的关联,或者参考一个-->
<association property="author" javaType="Author">
<id property="id" column="author_id"/>
<result property="username" column="author_username"/>
<result property="password" column="author_password"/>
<result property="email" column="author_email"/>
<result property="bio" column="author_bio"/>
<result property="favouriteSection" column="author_favourite_section"/>
</association>

<!--collection – 复杂类型的集-->
<!--嵌入结果映射结果映射自身的集,或者参考一个-->
<collection property="posts" ofType="Post">
<id property="id" column="post_id"/>
<result property="subject" column="post_subject"/>
<association property="author" javaType="Author"/>
<collection property="comments" ofType="Comment">
<id property="id" column="comment_id"/>
</collection>
<collection property="tags" ofType="Tag" >
<id property="id" column="tag_id"/>
</collection>

<!--discriminator – 使用结果值来决定使用哪个结果映射-->
<!--case – 基于某些值的结果映射-->
<!--嵌入结果映射这种情形结果也映射它本身,因此可以包含很多相同的元素,或者它可以参照一个外部的结果映射。-->
<discriminator javaType="int" column="draft">
<case value="1" resultType="DraftPost"/>
</discriminator>
</collection>

</resultMap>
0 0
原创粉丝点击