数据库
tb_studentb
CREATE TABLE `tb_studentb` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(32) DEFAULT NULL,
`sex` char(1) DEFAULT NULL,
`banji_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `banji_id` (`banji_id`),
CONSTRAINT `banji_id` FOREIGN KEY (`banji_id`) REFERENCES `tb_banji` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of tb_studentb
-- ----------------------------
INSERT INTO `tb_studentb` VALUES ('1', '李四', 'm', '1');
INSERT INTO `tb_studentb` VALUES ('2', 'wangwu', 'm', '2');
INSERT INTO `tb_studentb` VALUES ('3', 'jack', 'f', '1');
INSERT INTO `tb_studentb` VALUES ('4', 'tom', 'f', '2');
tb_banji
CREATE TABLE `tb_banji` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(32) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of tb_banji
-- ----------------------------
INSERT INTO `tb_banji` VALUES ('1', '16软件技术1班');
INSERT INTO `tb_banji` VALUES ('2', '16软件技术2班');
POJO类
package com.ssm.po;
import java.util.List;
public class Banji {
private Integer id;
private String name;
private List<Studentb> studentList;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Studentb> getStudentList() {
return studentList;
}
public void setStudentList(List<Studentb> studentList) {
this.studentList = studentList;
}
@Override
public String toString() {
return "Banji [id=" + id + ", name=" + name + ", studentList=" + studentList + "]";
}
}
package com.ssm.po;
public class Studentb {
private Integer id;
private String name;
private String sex;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Student2 [id=" + id + ", name=" + name + ", sex=" + sex + "]";
}
}
映射文件
BanjiMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ssm.mapper.BanjiMapper">
<select id="findBanjiWithStudent" parameterType="Integer" resultMap="BanjiWithStudentResult">
select b.*,s.id as student_id,s.name as student_name,s.sex from tb_banji b,tb_studentb s where b.id = s.banji_id and b.id = #{id}
</select>
<resultMap type="com.ssm.po.Banji" id="BanjiWithStudentResult">
<id property="id" column="id"/>
<result property="name" column="name"/>
<collection property="studentList" ofType="com.ssm.po.Studentb" >
<id property="id" column="student_id"/>
<result property="name" column="student_name"/>
<result property="sex" column="sex"/>
</collection>
</resultMap>
</mapper>
<mapper resource="com/ssm/mapper/BanjiMapper.xml"/>
测试
@Test
public void findBanjiWithStudentTest() {
SqlSession sqlSession = MybatisUtil.getSession();
Banji banji = sqlSession.selectOne("com.ssm.mapper.BanjiMapper.findBanjiWithStudent", 1);
System.out.println(banji.toString());
sqlSession.close();
}
结果
Banji [id=1, name=16软件技术1班, studentList=[Studentb [id=1, name=李四, sex=m], Studentb [id=3, name=jack, sex=f]]]