iBATIS的多对多映射配置6,sqlmap配置文件
Teacher.xml
- ﹤?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﹥
- tudent.xml
- ﹤?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="student"﹥
- ﹤typeAlias alias="Student" type="com.lsm.domain.Student" /﹥
- ﹤typeAlias alias="Teacher" type="com.lsm.domain.Teacher" /﹥
- ﹤resultMap class="Student" id="studentBasicResultMap"﹥
- ﹤result property="id" column="id"/﹥
- ﹤result property="name" column="name"/﹥
- ﹤result property="birthday" column="birthday"/﹥
- ﹤/resultMap﹥
- ﹤resultMap class="Student" id="studentWithTeacherResultMap" extends="studentBasicResultMap"﹥
- ﹤result property="teachers" column="id" select="getTeachersByStudentId"/﹥
- ﹤/resultMap﹥
- ﹤select id="getStudents" resultMap="studentWithTeacherResultMap"﹥
- ﹤!--[CDATA[
- select * from student
- ]]﹥
- ﹤/select﹥
- ﹤select id="getTeachersByStudentId" resultClass="Teacher"﹥
- ﹤![CDATA[
- select t.* from teacher t,student_teacher st where t.id=st.teacherid and st.studentid=#value# ]]--﹥
- ﹤/select﹥
- ﹤/sqlMap﹥
评论