一、环境准备
1、表结构
stu表
class表
2、表数据
stu表
class表
*3、对象类
Student*
package com.taozi.mybatis.bean;
public class Student {
private int id;
private String name;
private char Sex;
private Classes banji;;
public Student() {
// super();
// TODO Auto-generated constructor stub
}
public Student(int id, String name, char sex) {
super();
this.id = id;
this.name = name;
Sex = sex;
}
public char getSex() {
return Sex;
}
public void setSex(char gender) {
this.Sex = gender;
}
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 Classes getBanji() {
return banji;
}
public void setBanji(Classes banji) {
this.banji = banji;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", Sex=" + Sex + ", banji=" + banji + "]";
}
}
Classes类因最开始没注意到类名,用了Class,后来为了方便就直接改了个名字,此处只为演示下列的查询()
package com.taozi.mybatis.bean;
public class Classes {
private int id;
private String desc;
public Classes() {
// TODO Auto-generated constructor stub
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
@Override
public String toString() {
return "Classes [id=" + id + ", desc=" + desc + "]";
}
}
二、对象查询
查出某个学生的信息时,同时查询班级信息
1、简单级联查询
接口类中新增方法
public Student getStuClassById(int id);
sql映射
<resultMap type="com.taozi.mybatis.bean.Student" id="myStuMap_1">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="class_id" property="banji.id"/>
<result column="des" property="banji.desc"/>
</resultMap>
<select id="getStuClassById" resultMap="myStuMap_1">
select a.id id,a.name,a.class_id,b.id class,b.description des from stu a,class b
where a.class_id = b.id and a.id=#{id}
</select>
将resultMap中,学生信息中的班级id映射到班级信息的主键id字段,属性为学生类对象的班级属性的属性值。
测试方法
2、association关联查询
修改sql映射文件
<resultMap type="com.taozi.mybatis.bean.Student" id="myStuMap_2">
<id column="id" property="id"/>
<result column="name" property="name"/>
<association property="banji" javaType="com.taozi.mybatis.bean.Classes">
<id column="class_id" property="id"/>
<result column="des" property="desc"/>
</association>
</resultMap>
<select id="getStuClassById" resultMap="myStuMap_2">
select a.id id,a.name,a.class_id,b.id class,b.description des
from stu a,class b
where a.class_id = b.id and a.id=#{id}
</select>
将学生中的班级属性使用association封装,内部的映射和普通映射类似
3、association分布查询
修改sql映射文件
<!-- 分步查询
<association property:"banji"
select:班级查询语句 所在包。select_id
column:传递给select语句的参数值></association>
-->
<resultMap type="com.taozi.mybatis.bean.Student" id="myStuMap_3">
<id column="id" property="id"/>
<result column="name" property="name"/>
<!-- <result column="class_id" property="class_id"/>-->
<association property="banji"
select="com.taozi.mybatis.test.ClassMapper.getClasById" column="class_id">
</association>
</resultMap>
<select id="getStuByIdStep" resultMap="myStuMap_3" databaseId="mysql">
select * from stu where id =#{id}
</select>
在查询学生信息时,嵌套班级查询
接口类中新增分步查询方法
public Student getStuByIdStep(int id);
测试方法
Student stu = mapper.getStuByIdStep(3);
System.out.println(stu);
System.out.println(stu.getBanji());
结果如下:
因sql查询上未映射sex字段,故该字段为空
三、集合查询
查询班级信息时,将所对应的学生信息一并查出
1、级联查询
班级接口新增学生集合属性
private List<Student> student;
为班级新增查询方法
public List<Student> getClassStuById(int id);
sql映射
<!-- public List<Student> getClassStuById(int id); -->
<resultMap type="com.taozi.mybatis.bean.Classes" id="myGetClassStu">
<id column="id" property="id"/>
<result column="des" property="desc"/>
<collection property="student" ofType="com.taozi.mybatis.bean.Student">
<id column="stu_id" property="id"/>
<result column="name" property="name"/>
</collection>
</resultMap>
<select id="getClassStuById" resultMap="myGetClassStu">
select a.id id,a.description des,
b.id stu_id,b.name name
from class a left join stu b
on a.id = b.class_id
where a.id=#{id}
</select>
测试方法
@Test
public void test02() throws IOException
{
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory =
new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session = sqlSessionFactory.openSession();
ClassMapper mapper = session.getMapper(ClassMapper.class);
Classes banji = mapper.getClassStuById(1);
System.out.println(banji);
System.out.println(banji.getStudent());
}
测试结果
2、分步查询
Student新增查询方法(根据班级id查询)
public List<Student> getStuByClass(int id);
sql映射
<!-- public List<Student> getStuByClass(int id); -->
<select id="getStuByClass" resultType="com.taozi.mybatis.bean.Student">
select * from stu where class_id=#{id}
</select>
班级类新增查询方法
public Classes getClassStuByIdStep(int id);
sql映射
<!-- public Classes getClassStuByIdStep(int id); -->
<resultMap type="com.taozi.mybatis.bean.Classes" id="myGetClassStuStep">
<id column="id" property="id"/>
<result column="des" property="desc"/>
<collection property="student" select="com.taozi.mybatis.test.Mybatis_mapper_04.getStuByClass"
column="id">
</collection>
</resultMap>
<select id="getClassStuByIdStep" resultMap="myGetClassStuStep">
select id,description des from class where id=#{id}
</select>
测试方法
ClassMapper mapper = session.getMapper(ClassMapper.class);
Classes banji = mapper.getClassStuByIdStep(1);
System.out.println(banji);
测试结果
延迟加载
开启全局设置中的lazyLoadingEnabled属性及aggressiveLazyLoading属性