3、XML映射文件Mapper
2016-03-11 22:27
295 查看
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" <!--STATEMENT,PREPARED 或 CALLABLE 的一个。默认值:PREPARED。--> statementType="PREPARED" <!--FORWARD_ONLY,SCROLL_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>
相关文章推荐
- 识别输入装置ID与InputDevice装置(Improved event management)
- Android中的PendingIntent 原理
- Swift 2.0中常见的bug (1)
- cocos2dx 物理按键
- cocos2dx 加速度传感器
- android源码设计模式解析与实战 读书笔记 2 单例模式(下)
- appendChild()和insertBefore()方法插入元素的区别
- swift:ViewController中自定义tableView简单入门
- iOS 开源项目介绍之动画篇
- Android中用到的几大设计模式
- [android] 常用数据适配器SimpleAdapter
- IOS 如何隐藏tabbar
- Qml文件的两种加载方式|启动Qt quick app的两种方法
- XZ_iOS中清除缓存的实现
- android 的jpush极光推送
- 对微信的评价
- 用JAVA进行图像识别的小程序
- [置顶] Android第三方库学习清单
- android launchmode
- 关于Android app的launcher图标更换后,仍然显示默认的ic_launcher图标的解决方法