<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap namespace="teacher">
<typeAlias alias="Teacher" type="com.lsm.domain.Teacher" />
<typeAlias alias="Student" type="com.lsm.domain.Student" />
<resultMap class="Teacher" id="teacherBasicResultMap">
<result property="id" column="id"/>
<result property="name" column="name"/>
<result property="subject" column="subject"/>
</resultMap>
<!-- 下面这个resultMap中有个students属性,这个结果映射继承自上面的结果映射由于有了继承,结果映射可以任意扩展-->
<resultMap class="Teacher" id="teacherWithTeacherResultMap" extends="teacherBasicResultMap">
<result property="students" column="id" select="getStudentsByTeacherId"/>
</resultMap>
<!-- 这个查询中使用到了上面定义的结果映射,从而决定了查询出来的Teacher中关联出相关的students,在student.xml中配置相似,不再注释。-->
<select id="getTeachers" resultMap="teacherWithTeacherResultMap">
<!--[CDATA[ select * from teacher ]]-->
</select>
<select id="getStudentsByTeacherId" resultClass="Student">
<!--[CDATA[ select s.* from student s,student_teacher st where s.id=st.studentid and st.teacherid=#value# ]]-->
</select>
</sqlMap>
package com.lsm.domain;
import java.util.List;
public class Teacher {
private int id;
private String name;
private String subject;
private List students; //注意这里有个List类型的students,表示一个老师对应多个学生
public List getStudents() {
return students;
}
public void setStudents(List students) {
this.students = students;
}
//省略掉其他的getter and setter
}
这里将查询teacher和student的<select>混合在一个文件中了,不如分开清晰。《iBatis2学习笔记:多对多映射(双向) 》中的例子比较好。
分享到:
相关推荐
ibatis ORM框架 多对多,很好的例子
一份非常详细的讲解文档,从建表到Ibatis配置文件,再到dao代码全部都有
ibatis 一对多关系映射 ibatis 一对多关系映射ibatis 一对多关系映射
ibatis配置多表关联(一对一、一对多、多对多
NULL 博文链接:https://self4j.iteye.com/blog/906319
Ibatis多表查询
NULL 博文链接:https://keer2345.iteye.com/blog/673391
Ibatis多表查询,一个小小的多表查询实例教你如何用ibatis进行多表查询
利用ibatis实现一对一、多对一的示例代码。每种关系用两种方式实现,具体的讲解可看我的博客http://blog.csdn.net/duwenchao1986/article/details/8565386
ibatis学习 ibatis总结 ibatis ibatis ibatis
ibatis 多对一 个人平时学习中的总结 跟大家分享下
iBATIS 级联iBATIS 级联iBATIS 级联
Ibatis一对一映射提示,需要学习的同学请关注,谢谢。
ibatis资料ibatis资料ibatis资料ibatis资料ibatis资料ibatis资料ibatis资料ibatis资料ibatis资料
ibatis多表查询开发过程(java)
ibatis的的增删改查和一对一、一对多查询 ibatis的的增删改查和一对一、一对多查询 ibatis的的增删改查和一对一、一对多查询 完成的项目
ibatis动态多条件组合查询 实例 说明
ibatis demo,ibatis例子,ibatis示例
ibatis实战之一对多关联源代码 文章地址:http://blog.csdn.net/itmyhome1990/article/details/30050183