Class A {
public int ID;
public int Rating1;
public int Rating2;
public int Rating3;
public B mRating1;
public B mRating2;
public B mRating3;
}
Class B {
public int ID;
public String Code;
public String NameEn;
public String NameCn;
public int IsActive;
}
<resultMap id="BaseResultMap" type="com.example.demo1.demo.system.dao.entities.dbo.tbA">
<id column="ID" jdbcType="INT" property="Id" />
<result column="Rating1" jdbcType="INT" property="Rating1" />
<result column="Rating2" jdbcType="INT" property="Rating2" />
<result column="Rating3" jdbcType="INT" property="Rating3" />
<association property="mRating1" resultMap="com.example.demo1.demo.system.dao.mapper.tbBMapper.BaseResultMap"></association>
<association property="mRating2" resultMap="com.example.demo1.demo.system.dao.mapper.tbBMapper.BaseResultMap"></association>
<association property="mRating3" resultMap="com.example.demo1.demo.system.dao.mapper.tbBMapper.BaseResultMap"></association>
</resultMap>
SELECT A.*,B.*,C.*,D.*
FORM tb_A A
LEFT JOIN tb_B B ON A.Rating1=B.ID
LEFT JOIN tb_B C ON A.Rating1=C.ID
LEFT JOIN tb_B D ON A.Rating1=D.ID
如上所述,A的对象中3个Rating字段关联B对象的ID,分另可以取到每一个分值的 Code, 英文名,中文名,是否有效等。我在定义ResultMap时,将B对象相应的Map包含在A的ResultMap中了。使用上面的SQL语句查询,但查询出来,只有mRating1这个对象有值,另外两个分值对象为空,Mybatis是如何关联这个查询结果集的。是需要在 association 设置对应的关联参数吗?
用集合啊.collection
理论可以,但是太麻烦,而不推荐这样写
Class A {
public int ID;
public int Rating1;
public int Rating2;
public int Rating3;
public B mRating1;
public B mRating2;
public B mRating3;
}
你改成这样不行吗
Class A {
public int ID;
public int Rating1;
public int Rating2;
public int Rating3;
public List<B> list;
}
list里面每一个元素就是一个B对象,随便放
xml里面两类联立映射时,对于list属性可以使用collection关键词进行映射,典型的一对多嘛,一个A对应多个B,association是多对一,用在A实体类对应的xml里面代表多个A对应一个B,但是用在B里,代表多个B对应一个A
assocation和collection的用法可以参考这篇文章:https://199604.com/709
如有帮助,请采纳一下,谢谢O(∩_∩)O
你这个配置一对一有点问题,要配置一对多关联就可以了。
javabean里面设置List集合。