mybatis中association与collection
2013-09-04 16:41
423 查看
介绍association用法
一篇博客中存在多个评论
在博客实体类中增加评论属性
在评论的映射文件中加入
这里你应该注意很多东西,但大部分代码和上面的关联元素是非常相似的。首先,你应
该注意我们使用的是集合元素。然后要注意那个新的“of Type”属性。这个属性用来区分
JavaBean(或字段)属性类型和集合包含的类型来说是很重要的。所以你可以读出下面这个
映射:
<collection property="posts" javaType=”ArrayList” column="blog_id"
ofType="Post" select=”selectPostsForBlog”/>
读作:“在Post类型的ArrayList中的posts的集合。”
javaType属性是不需要的,因为MyBatis在很多情况下会为你算出来。所以你可以缩短
写法:
<collection property="posts" column="blog_id" ofType="Post" select=”selectPostsForBlog”/>
集合的嵌套结果
一篇博客中存在多个评论
在博客实体类中增加评论属性
private List<FaqComment> faqcomlist;在mybatis中增加
<association property="faqcomlist" column="id" select="com.dts.mapper.faq.FaqCommentMapper.queryList" />id为博客id
在评论的映射文件中加入
<select id="queryList" parameterType="java.lang.Integer" resultMap="BaseResultMap2"> SELECT co.*,cu.username AS assign_user FROM dts_faq_comment co LEFT JOIN dts_user cu ON cu.id=co.create_user_id WHERE faq_id=#{value} ORDER BY id DESC </select>
这里你应该注意很多东西,但大部分代码和上面的关联元素是非常相似的。首先,你应
该注意我们使用的是集合元素。然后要注意那个新的“of Type”属性。这个属性用来区分
JavaBean(或字段)属性类型和集合包含的类型来说是很重要的。所以你可以读出下面这个
映射:
<collection property="posts" javaType=”ArrayList” column="blog_id"
ofType="Post" select=”selectPostsForBlog”/>
读作:“在Post类型的ArrayList中的posts的集合。”
javaType属性是不需要的,因为MyBatis在很多情况下会为你算出来。所以你可以缩短
写法:
<collection property="posts" column="blog_id" ofType="Post" select=”selectPostsForBlog”/>
集合的嵌套结果
相关文章推荐
- Mybatis之association和collection
- mybatis association和collection标签怎么用
- Mybatis 高级结果映射 ResultMap Association Collection
- Mybatis 高级结果映射 ResultMap Association Collection
- 2014年2月27日 mybatis 的 association 和 collection 的用法小结
- mybatis 一对一与一对多collection和association的使用
- mybatis 学习记录(4.2)—— 级联查询(有 association 和 collection)
- mybatis 一对一与一对多collection和association的使用
- Mybatis 高级结果映射 ResultMap Association Collection
- MyBatis对象关联关系---- association与collection
- mybatis 一对一与一对多collection和association的使用
- Mybatis 高级结果映射 ResultMap Association Collection
- Mybatis 高级结果映射 ResultMap Association Collection
- Mybatis - 一对一(association)和一对多(collection)
- MyBatis对一(collection)和对多(association)
- mybatis中association和collection的column传入多个参数值
- Mybatis 高级结果映射 ResultMap Association Collection
- Mybatis 高级结果映射 ResultMap Association Collection
- Mybatis 高级结果映射 ResultMap Association Collection
- Mybatis中的collection、association来处理结果映射