Mybaits的association关联只能返回一条记录问题

场景:一个用户可以写多篇文章,先查用户再查文章,属于一对多关联。

问题:使用association便签在user下面关联article,多篇文章只能返回一个。

User实体

public class User {
	private int id;
	private String name;
	private int age;
	private Article article;
    //....get/set
}

Article实体

public class Article {
	private int id;
	private int userid;
	private String title;
	private String content;
//get/set
}

UserMapper.xml

<resultMap type="com.ij34.model.User" id="resultAticleList">
		<id property="id" column="uid" />
		<result property="name" column="name" />
		<result property="age" column="age" />
		<!--一对一 -->
		<association property="article" javaType="com.ij34.model.Article">
			<id property="id" column="aid" />
			<result property="userid" column="userid" />
			<result property="title" column="title" />
			<result property="content" column="content" />
		</association>
	</resultMap>


	<select id="selectarticle" parameterType="int" resultMap="resultAticleList">
		select u.id uid ,u.name,u.age,a.id aid,a.userid ,a.title,a.content from users u,article a
		where
		u.id=a.userid
		and
		u.id=#{id}
	</select>

查询结果:

DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@1a07250a]
DEBUG [main] - ==>  Preparing: select u.id uid ,u.name,u.age,a.id aid,a.userid ,a.title,a.content from users u,article a where u.id=a.userid and u.id=? 
DEBUG [main] - ==> Parameters: 1(Integer)
DEBUG [main] - <==      Total: 3
User [id=1, name=罗大佑, age=22, article=Article [id=6, userid=1, title=english, content=英语]]
DEBUG [main] - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@1a07250a]
DEBUG [main] - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@1a07250a]
DEBUG [main] - Returned connection 436675850 to pool.

查询到了三条记录,但是映射却是映射了一条

原因分析:

 

网上说的原因基本就那几个,两张表主键字段相同需要设置别名,这个我也设置了,然后有人说association有问题巴拉巴拉。

楼主看了下官方文档:

关联

<association property="author" column="blog_author_id" javaType="Author">
  <id property="id" column="author_id"/>
  <result property="username" column="author_username"/>
</association>

关联元素处理“有一个”类型的关系。比如,在我们的示例中,一个博客有一个用户。 关联映射就工作于这种结果之上。你指定了目标属性,来获取值的列,属性的 java 类型(很 多情况下 MyBatis 可以自己算出来) ,如果需要的话还有 jdbc 类型,如果你想覆盖或获取的 结果值还需要类型控制器。

关联中不同的是你需要告诉 MyBatis 如何加载关联。MyBatis 在这方面会有两种不同的 方式:

  • 嵌套查询:通过执行另外一个 SQL 映射语句来返回预期的复杂类型。
  • 嵌套结果:使用嵌套结果映射来处理重复的联合结果的子集。首先,然让我们来查看这个元素的属性。所有的你都会看到,它和普通的只由 select 和

resultMap 属性的结果映射不同。

英文版: 

association

<association property="author" javaType="Author">
  <id property="id" column="author_id"/>
  <result property="username" column="author_username"/>
</association>

The association element deals with a "has-one" type relationship. For example, in our example, a Blog has one Author. An association mapping works mostly like any other result. You specify the target property, the javaType of the property (which MyBatis can figure out most of the time), the jdbcType if necessary and a typeHandler if you want to override the retrieval of the result values.

Where the association differs is that you need to tell MyBatis how to load the association. MyBatis can do so in two different ways:

  • Nested Select: By executing another mapped SQL statement that returns the complex type desired.
  • Nested Results: By using nested result mappings to deal with repeating subsets of joined results.

First, let's examine the properties of the element. As you'll see, it differs from a normal result mapping only by the select and resultMap attributes.

总结: 根本原因就是association根本就不是用来处理一对多的,只能用来处理多对一或者一对一,一对多的可以使用collection来处理。

修改:

 

修改user实体article属性为list类型

private List<Article> article;

修改resultmap中association为collection类型即可:

<collection property="article" ofType="com.ij34.model.Article">
			<id property="id" column="aid" />
			<result property="userid" column="uid" />
			<result property="title" column="title" />
			<result property="content" column="content" />
		</collection>

附录:官网文档地址 

http://www.mybatis.org/mybatis-3/sqlmap-xml.html

http://www.mybatis.org/mybatis-3/zh/sqlmap-xml.html

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值