MyBatis关联查询
2015-06-14 16:47
393 查看
在实际的应用开发中,对单表进行查询是很少的。即使是通过视图这种方式,也是对表进行关联而来的。在ORM中,通常需要在一个实体中关联另一个实体。在MyBatis中一对多关系使用collection标签,一对一关系使用association标签。在这里我们考虑这样一张场景,学生,班级。学生和班级是多对一的关系。现在我们要在查询班级的时候得到学生信息,在查学生时要得到班级信息。 准备工作。 建表:
表 student create table t_student ( id varchar2(10) primary key, name varchar2(20), sex varchar(2), class_id varchar2(10) )
表 class create table t_class ( id varchar2(10) primary key, class_name varchar(10), teacher varchar2(10) )
创建实体:
Student package sy.model; public class TStudent { private String id; private String name; private String sex; private String classId; private TClass tClass; public void setId(String id){ this.id = id; } public String getId(){ return this.id; } public void setName(String name){ this.name = name; } public String getName(){ return this.name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public void setClassId(String classId){ this.classId = classId; } public String getClassId(){ return this.classId; } public void setTClass(TClass tClass){ this.tClass= tClass; } public TClass getTClass (){ return this.tClass; } }
实体 TClass package sy.model; import java.util.List; public class TClass { private String id; private String className; private String teacher; private List<TStudent> studentList; public List<TStudent> getStudentList() { return studentList; } public void setStudentList(List<TStudent> studentList) { this.studentList = studentList; } public void setId(String id){ this.id = id; } public String getId(){ return this.id; } public void setClassName(String className){ this.className= className; } public String getClassName(){ return this.className; } public void setTeacher(String teacher){ this.teacher= teacher; } public String getTeacher(){ return this.teacher; } }
TStudent 映射 <?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="sy.dao.TStudentMapper" > <resultMap id="studentResultMap" type="sy.model.TStudent"> <id property="id" column="id" /> <result property="name" column="name" /> <result property="sex" column="sex" /> <result property="classId" column="class_id" /> </resultMap> <resultMap id="classResultMap" type="sy.model.TClass"> <id property="id" column="id" /> <result property="className" column="class_name" /> <result property="teacher" column="teacher" /> </resultMap> <resultMap id="studentWithClassMap" type="sy.model.TStudent" extends="studentResultMap"> <association property="tClass" javaType="sy.model.TClass" resultMap="classResultMap"/> </resultMap> <select id="selectStudentWithClass" resultMap="studentWithClassMap"> select s.id, s.name, s.sex, s.class_id, t.id, t.class_name, t.teacher from t_student s,t_class t where s.class_id = t.id </select> <resultMap id="studentWithClassMap2" type="sy.model.TStudent" extends="studentResultMap"> <!-- association 中的column表示的是关联的字段--> <association property="tClass" javaType="sy.model.TClass" select="selectClass" column="class_id"/> </resultMap> <select id="selectClass" parameterType="string" resultMap="classResultMap"> select * from t_clsss t where t.id=#{class_id} </select> <select id="selectStudentWithClass2" resultMap="studentWithClassMap"> select * from t_student s </select> </mapper>
TClass映射 <?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="sy.dao.TClassMapper" > <resultMap id="classResultMap" type="sy.model.TClass"> <id property="id" column="id" /> <result property="className" column="name" /> <result property="teacher" column="teacher" /> </resultMap> <resultMap id="classWithStudentMap" type="sy.model.TClass" extends="classResultMap"> <collection property="studentList" javaType="list" ofType="sy.model.TStudent"> <!-- property对应实体中的属性,column对应的是查询出的**字段或者是别名**。查询出来的字段中有重名,就必须取别名,否则会映射不到--> <id property="id" column="student_id" /> <result property="name" column="name" /> <result property="sex" column="sex" /> <result property="classId" column="class_id" /> </collection> </resultMap> <select id="selectClassWithStudent" resultMap="classWithStudentMap"> select t.id, t.class_name, t.teacher, s.id student_id, s.name, s.sex, s.class_id from t_student s,t_class t where s.class_id = t.id </select> </mapper>
上面id为selectStudentWithClass2会产生N+1次查询,效率较低。而使用嵌套查询可以避免这种情况。
相关文章推荐
- Shiro使用自己的SessionId管理和rememberMe配置
- vs2010 vc jsoncpp 编译要点
- Apache2.4配置(全)
- 今晚打一局cf
- 关于安装的顺序及问题
- Clean Code 读书笔记四
- 【PHP学习】PHP 字符串函数
- iOS天气预报Demo
- HDU-1062
- GPU Management Platform
- matlab中strcmp函数的使用
- FaceBook/infer-linter静态代码分析
- 屏蔽贴吧游戏。
- Windows用来定位DLL的搜索路径(静态加载和动态加载)
- mac装机
- 自己写着玩(三)
- Android中使用HttpURLConnection和HttpClient实现GET和POST请求访问网络
- 用一维数组模拟堆栈
- NOJ1121 Message Flood STL应用
- Array Pascal's Triangle II