MyBatis一对多双向关联——MyBatis学习笔记之七
来源:互联网 发布:论文英语翻译软件 编辑:程序博客网 时间:2024/05/22 17:45
1
private
List<Student> supStudents;
//指导学生
package com.abc.mapper;import com.abc.domain.Teacher;public interface TeacherMapper {public Teacher getById(int id);}
<?xml version="1.0" encoding="utf8"?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><!--与以前一样,namespace的值是对应的映射器接口的完整名称--><mapper namespace="com.abc.mapper.TeacherMapper"><!--TeacherMapper接口中getById方法对应的SQL语句。查询教师及其指导的学生的信息。由于教师、学生都有id、name、gender等属性,因此给教师的字段都起了别名--><select id="getById" parameterType="int" resultMap="supervisorResultMap"><span style="white-space:pre"></span>select t.id t_id, t.name t_name, t.gender t_gender,<span style="white-space:pre"></span>t.research_area t_research_area, t.title t_title,<span style="white-space:pre"></span>s.id,s.name, s.gender,s.major,s.grade<span style="white-space:pre"></span>from teacher t,student s where t.id=#{id}<span style="white-space:pre"></span>and s.supervisor_id = t.id</select><!--教师实体映射--><resultMap id="supervisorResultMap" type="Teacher"><span style="white-space:pre"></span><id property="id" column="t_id"/><span style="white-space:pre"></span><result property="name" column="t_name"/><span style="white-space:pre"></span><result property="gender" column="t_gender"/><span style="white-space:pre"></span><result property="researchArea" column="t_research_area"/><span style="white-space:pre"></span><result property="title" column="t_title"/><!--collection元素映射教师的指导学生集合的属性。resultMap以命名空间名.resultMap的id的形式,引用studentResultMap。需要注意的是,上面的select语句中学生的字段名/别名应与studentResultMap中的column属性一致--><span style="white-space:pre"></span><collection property="supStudents"<span style="white-space:pre"></span>resultMap="com.abc.mapper.StudentMapper.studentResultMap"/></resultMap></mapper>
<?xml version="1.0" encoding="utf8"?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.abc.mapper.StudentMapper"><resultMap id="studentResultMap" type="Student"><span style="white-space:pre"></span><id property="id" column="id"/><span style="white-space:pre"></span><result property="name" column="name"/><span style="white-space:pre"></span><result property="gender" column="gender"/><span style="white-space:pre"></span><result property="major" column="major"/><span style="white-space:pre"></span><result property="grade" column="grade"/><!--相应地,在此引用supervisorResultMap,亦采用命名空间名.resultMap的id的形式。--><span style="white-space:pre"></span><association property="supervisor"<span style="white-space:pre"></span>resultMap="com.abc.mapper.TeacherMapper.supervisorResultMap"/><span style="white-space:pre"></span></resultMap></mapper>
1
2
3
4
5
<!--在classpath中以相对路径的形式引用映射文件-->
<
mappers
>
<
mapper
resource
=
"resources/mappers/StudentMapper.xml"
/>
<
mapper
resource
=
"resources/mappers/TeacherMapper.xml"
/>
</
mappers
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package
com.demo;
import
org.springframework.context.ApplicationContext;
import
com.abc.mapper.StudentMapper;
import
com.abc.mapper.TeacherMapper;
import
com.abc.domain.Teacher;
import
com.abc.domain.Student;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
import
java.util.List;
public
class
CollectionDemo
{
private
static
ApplicationContext ctx;
static
{
//在类路径下寻找resources/beans.xml文件
ctx =
new
ClassPathXmlApplicationContext(
"resources/beans.xml"
);
}
public
static
void
main(String[] args)
{
//从Spring容器中请求映射器
TeacherMapper mapper =
(TeacherMapper)ctx.getBean(
"teacherMapper"
);
//查询id为1的教师
Teacher teacher = mapper.getById(
1
);
if
(teacher ==
null
)
{
System.out.println(
"未找到相关教师信息。"
);
}
else
{
//教师信息
System.out.println(
"**********************************************"
);
System.out.println(
"教师姓名:"
+
" "
+ teacher.getName());
System.out.println(
"教师职称:"
+
" "
+ teacher.getTitle());
System.out.println(
"**********************************************"
);
System.out.println(
"指导学生信息:"
);
//遍历指导的学生
for
(Student s : teacher.getSupStudents())
{
System.out.println(
"**********************************************"
);
System.out.println( s.getName() +
" "
+ s.getGender() +
" "
+ s.getGrade()
+
" "
+ s.getMajor());
//从学生端访问教师
System.out.println(
"指导教师研究方向:"
+ s.getSupervisor().getResearchArea());
}
System.out.println(
"**********************************************"
);
}
}
}
1
2
3
4
5
6
7
8
9
<
target
name
=
"run"
depends
=
"compile"
>
<!--指定CollectionDemo为要运行的类-->
<
java
fork
=
"true"
classname
=
"com.demo.CollectionDemo"
classpathref
=
"library"
>
<!--把classes目录添加到工程的classpath中。
${targetdir}是指引用上面定义的property元素targetdir-->
<
classpath
path
=
"${targetdir}"
/>
</
java
>
</
target
>
0 0
- MyBatis一对多双向关联——MyBatis学习笔记之七
- MyBatis学习(二)之双向一对多关联映射查询
- MyBatis学习(二)之双向一对多关联映射查询
- mybatis---一对多双向关联
- Mybatis一对多双向关联
- Mybatis学习笔记-一对多关联
- mybatis学习笔记(五) --- 一对多关联
- MyBatis 一对多双向关联查询
- mybatis的一对多双向关联映射
- 【Mybatis学习笔记】系列之二:Mybatis双项一对多关联
- myBatis学习笔记(5)——一对多关联表查询
- mybatis学习笔记(十)一对多关联表查询
- MyBatis学习笔记:一对一和一对多关联表查询
- Mybatis一对多关联
- mybatis一对多关联
- MyBatis框架的学习(五)——一对一关联映射和一对多关联映射
- MyBatis学习(六):MyBatis的一对多关联如何操作
- Mybatis学习笔记-关联表查询的问题 (一对多关联)
- 面向对象基础之抽象类与接口 下
- Android动画效果translate、scale、alpha、rotate详解
- Windows相关
- js倒计时
- sourceforge上的Top 25个项目
- MyBatis一对多双向关联——MyBatis学习笔记之七
- c++中怎样获取本地时间 以及获得本地 时间一周的天数
- iOS 本地化多语言支持
- 出现 strtime timezone 错误时,调整方法?
- 搭建Apache Mina框架并实现Server与Client端的简单消息传递
- Oracle创建物化视图
- 经典代码:调换字符串的字符位置
- 从数据库里到处txt数据,然后再把txt文档转成Excel文档,结果被一个换行符终止了,
- 在iPhone开发中,全局变量的几种使用方法