1.表结构
CREATE TABLE `a` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
CREATE TABLE `b` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`aid` int(11) DEFAULT NULL,
`c1` int(11) DEFAULT NULL,
`c2` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
CREATE TABLE `c` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
2.对象
package com.testmybatis.model;
import java.util.List;
public class A {
int id;
String name;
private List<B> blist;
public List<B> getBlist() {
return blist;
}
public void setBlist(List<B> blist) {
this.blist = blist;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.testmybatis.model;
public class B {
private int id;
private String name;
private int aid;
private int c1;
private int c2;
private C c1obj;
private C c2obj;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAid() {
return aid;
}
public void setAid(int aid) {
this.aid = aid;
}
public int getC1() {
return c1;
}
public void setC1(int c1) {
this.c1 = c1;
}
public int getC2() {
return c2;
}
public void setC2(int c2) {
this.c2 = c2;
}
public C getC1obj() {
return c1obj;
}
public void setC1obj(C c1obj) {
this.c1obj = c1obj;
}
public C getC2obj() {
return c2obj;
}
public void setC2obj(C c2obj) {
this.c2obj = c2obj;
}
}
3.collection
<select id="test" resultMap="aMap1">
select
a.id as id,
a.name as name,
b.id as bid,
b.name as bname,
b.aid as aid,
b.c1 as c1,
b.c2 as c2
from A a
left join B b on a.id=b.aid
</select>
<resultMap type="com.testmybatis.model.A" id="aMap1">
<id property="id" column="id"/>
<result property="name" column="name"/>
<collection property="blist" ofType="com.testmybatis.model.B">
<id property="id" column="bid"/>
<result property="name" column="bname"/>
<result property="aid" column="aid"/>
<result property="c1" column="c1"/>
<result property="c2" column="c2"/>
</collection>
</resultMap>
4.association
<select id="testb" resultMap="aMap2">
select
b.id as bid,
b.name as bname,
b.aid as aid,
b.c1 as c1,
b.c2 as c2,
c1.id as c1id,
c1.name as c1name,
c2.id as c2id,
c2.name as c2name
from B b
left join C c1 on b.c1=c1.id
left join C c2 on b.c2=c2.id
</select>
<resultMap type="com.testmybatis.model.B" id="aMap2">
<id property="id" column="bid"/>
<result property="name" column="bname"/>
<result property="aid" column="aid"/>
<result property="c1" column="c1"/>
<result property="c2" column="c2"/>
<association property="c1obj" javaType="com.testmybatis.model.C" >
<id property="id" column="c1id"/>
<result property="name" column="c1name"/>
</association>
<association property="c2obj" javaType="com.testmybatis.model.C" >
<id property="id" column="c2id"/>
<result property="name" column="c2name"/>
</association>
</resultMap>
5. collection和association嵌套
<select id="test" resultMap="aMap">
select
a.*,
b.id bid,b.name bname,
c1.id c1id,c1.name c1name,
c2.id c2id,c2.name c2name
from A a
left join B b on a.id=b.aid
left join C c1 on b.c1=c1.id
left join C c2 on b.c2=c2.id
</select>
<resultMap type="com.testmybatis.model.A" id="aMap">
<id property="id" column="id"/>
<result property="name" column="name"/>
<collection property="blist" ofType="com.testmybatis.model.B">
<id property="id" column="bid"/>
<result property="name" column="bname"/>
<association property="c1obj" javaType="com.testmybatis.model.C" >
<id property="id" column="c1id"/>
<result property="name" column="c1name"/>
</association>
<association property="c2obj" javaType="com.testmybatis.model.C" >
<id property="id" column="c2id"/>
<result property="name" column="c2name"/>
</association>
</collection>
</resultMap>

本文介绍了一个使用MyBatis进行复杂关联映射的例子,包括一对一、一对多及多对多关系的处理方式。具体涉及表结构设计、Java对象模型定义、SQL查询语句与映射文件配置等内容。
1万+

被折叠的 条评论
为什么被折叠?



