我们可以应用内联的或运用@Resuts注解来映射查询的结果。让我们看一下如何运用@Results注解来执行SELECT查询。

package com.owen.mybatis.mappers;
public interface StudentMapper
{
@Select("SELECT * FROM STUDENTS")
@Results({
@Result(id=true, column="stud_id", property="studId"),
@Result(column="name", property="name"),
@Result(column="email", property="email"),
@Result(column="addr_id", property="address.addrId")
})
List<Student> findAllStudents();
}

例如我们可以看到下面的findStudentBy()和findAliStudents()的方法。

@Select("SELECT * FROM STUDENTS WHERE STUD_ID=#{studId}")
@Results({
@Result(id=true, column="stud_id", property="studId"),
@Result(column="name", property="name"),
@Result(column="email", property="email"),
@Result(column="addr_id", property="address.addrId")
})
Student findStudentById(int studId);
@Select("SELECT * FROM STUDENTS")
@Results({
@Result(id=true, column="stud_id", property="studId"),
@Result(column="name", property="name"),
@Result(column="email", property="email"),
@Result(column="addr_id", property="address.addrId")
})
List<Student> findAllStudents();

这里注解 @Results配置与其它几个是相似的,但是我们需要去复制它。这就是我们的问题所在。我们可以创建一个Mapper XML的文件,然后配置<resultMap>元素和涉及到resultMap运用@ResultMap的注解。

定义<resultMap>的IDj是StudentResult在StudentMapper.xml配置文件中。

<mapper namespace="com.owen.mybatis.mappers.StudentMapper">
<resultMap type="Student" id="StudentResult">
<id property="studId" column="stud_id"/>
<result property="name" column="name"/>
<result property="email" column="email"/>
<result property="phone" column="phone"/>
</resultMap>
</mapper>

在StudentMapper.java中。涉及到的resultMap属性的StudentResult,我们就可以直接使用@ResultMap.

public interface StudentMapper
{
@Select("SELECT * FROM STUDENTS WHERE STUD_ID=#{studId}")
@ResultMap("com.owen.mybatis.mappers.StudentMapper.StudentResult")
Student findStudentById(int studId);
@Select("SELECT * FROM STUDENTS")
@ResultMap("com.owen.mybatis.mappers.StudentMapper.StudentResult")
List<Student> findAllStudents();
}

1.一对一映射

MyBatis提供@ one的注解来加载一对一的注解,和运用Nested-Select声明。让我们来看一下,获取student信息连带address的信息,使用@One注解。

public interface StudentMapper
{
@Select("SELECT ADDR_ID AS ADDRID, STREET, CITY, STATE, ZIP, COUNTRY
FROM ADDRESSES WHERE ADDR_ID=#{id}")
Address findAddressById(int id);
@Select("SELECT * FROM STUDENTS WHERE STUD_ID=#{studId} ")
@Results({
@Result(id=true, column="stud_id", property="studId"),
@Result(column="name", property="name"),
@Result(column="email", property="email"),
@Result(property="address", column="addr_id",
one=@One(select="com.owen.mybatis.mappers.StudentMapper.
findAddressById"))
})
Student selectStudentWithAddress(int studId);
}

这里我们运用@one作为select的属性,这个将会返回Address的对象。属性column=”addr_id”,这个属性值add_id是来自于STUDENTS表的,而且将会通过findAddressById()方法来查找。如果查询的结果是多行,那么将会报TooManyResultException的错误。

int studId = 1;
StudentMapper studentMapper =
sqlSession.getMapper(StudentMapper.class);
Student student = studentMapper.selectStudentWithAddress(studId);
System.out.println("Student :"+student);
System.out.println("Address :"+student.getAddress());

在StudentMapper.xml中配置<resultMap>的属性如下:

<mapper namespace="com.owen.mybatis.mappers.StudentMapper">
<resultMap type="Address" id="AddressResult">
<id property="addrId" column="addr_id"/>
<result property="street" column="street"/>
<result property="city" column="city"/>
<result property="state" column="state"/>
<result property="zip" column="zip"/>
<result property="country" column="country"/>
</resultMap>
<resultMap type="Student" id="StudentWithAddressResult">
<id property="studId" column="stud_id"/>
<result property="name" column="name"/>
<result property="email" column="email"/>
<association property="address" resultMap="AddressResult"/>
</resultMap>
</mapper>
public interface StudentMapper
{
@Select("select stud_id, name, email, a.addr_id, street, city,
state, zip, country"+" FROM students s left outer join addresses a
on s.addr_id=a.addr_id"+" where stud_id=#{studId} ")
@ResultMap("com.owen.mybatis.mappers.StudentMapper.
StudentWithAddressResult")
Student selectStudentWithAddress(int id);
}

2. 一对多映射

MyBatis提供了@Many注解来加载一对多的注解,运用Nested-SELECT声明。

现在我们一起来看一下如何获取一名教师的教授课程,使用@Many注解。

public interface TutorMapper
{
@Select("select addr_id as addrId, street, city, state, zip,
country from addresses where addr_id=#{id}")
Address findAddressById(int id);
@Select("select * from courses where tutor_id=#{tutorId}")
@Results({
@Result(id=true, column="course_id", property="courseId"),
@Result(column="name", property="name"),
@Result(column="description", property="description"),
@Result(column="start_date" property="startDate"),
@Result(column="end_date" property="endDate")
})
List<Course> findCoursesByTutorId(int tutorId);
@Select("SELECT tutor_id, name as tutor_name, email, addr_id
FROM tutors where tutor_id=#{tutorId}")
@Results({
@Result(id=true, column="tutor_id", property="tutorId"),
@Result(column="tutor_name", property="name"),
@Result(column="email", property="email"),
@Result(property="address", column="addr_id",
one=@One(select=" com.owen.mybatis.
mappers.TutorMapper.findAddressById")),
@Result(property="courses", column="tutor_id",
many=@Many(select="com.owen.mybatis.mappers.TutorMapper.
findCoursesByTutorId"))
})
Tutor findTutorById(int tutorId);
}

这里我们应用@Many作为select的注解,这个方法将会返回List<Course>的对象。带有column=”tutor_id”,这个tutor_id的列值是来自于TUTOR表的行,这个要通过findCoursesByTutorId()的方法来获取的。

在TutorMaper.xml中配置<resultMap>的信息如下:

mapper namespace="com.owen.mybatis.mappers.TutorMapper">
<resultMap type="Address" id="AddressResult">
<id property="addrId" column="addr_id"/>
<result property="street" column="street"/>
<result property="city" column="city"/>
<result property="state" column="state"/>
<result property="zip" column="zip"/>
<result property="country" column="country"/>
</resultMap>
<resultMap type="Course" id="CourseResult">
<id column="course_id" property="courseId"/>
<result column="name" property="name"/>
<result column="description" property="description"/>
<result column="start_date" property="startDate"/>
<result column="end_date" property="endDate"/>
</resultMap>
<resultMap type="Tutor" id="TutorResult">
<id column="tutor_id" property="tutorId"/>
<result column="tutor_name" property="name"/>
<result column="email" property="email"/>
<association property="address" resultMap="AddressResult"/>
<collection property="courses" resultMap="CourseResult"/>
</resultMap>
</mapper>

调用方法

public interface TutorMapper
{
@Select("SELECT T.TUTOR_ID, T.NAME AS TUTOR_NAME, EMAIL,
A.ADDR_ID, STREET, CITY, STATE, ZIP, COUNTRY, COURSE_ID, C.NAME,
DESCRIPTION, START_DATE, END_DATE FROM TUTORS T LEFT OUTER
JOIN ADDRESSES A ON T.ADDR_ID=A.ADDR_ID LEFT OUTER JOIN COURSES
C ON T.TUTOR_ID=C.TUTOR_ID WHERE T.TUTOR_ID=#{tutorId}")
@ResultMap("com.owen.mybatis.mappers.TutorMapper.TutorResult")
Tutor selectTutorById(int tutorId);
}

Mybatis的注解应用之关系映射相关推荐

  1. MyBatis:注解简化一对一关系

    上期 注解式是真的好用! 一对一的关系:客户拥有一张身份证,而身份证也只有一个客户. 客户表中有身份证的cid,可以通过这个cid匹配身份证表中的cid,拿到那个具体的身份证信息 反之,身份证通过本身 ...

  2. Spring Boot 实战 —— MyBatis(注解版)使用方法

    原文链接: Spring Boot 实战 -- MyBatis(注解版)使用方法 简介 MyBatis 官网 是这么介绍它自己的: MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过 ...

  3. Mybatis—— 使用注解实现一对一复杂关系映射及延迟加载

    @Results 注解 代替的是标签<resultMap> 该注解中可以使用单个@Result 注解,也可以使用@Result 集合 @Results({@Result(),@Result ...

  4. mybatis使用全注解的方式案例(包含一对多关系映射)

    前面我写过ssh:ssh(Spring+Spring mvc+hibernate)简单增删改查案例 和ssm:ssm(Spring+Spring mvc+mybatis)的案例,需要了解的可以去看看, ...

  5. 组成关系映射(注解)

    组成关系映射(注解) @Embeddable:表明该JavaBean是组件类. @Embedded:用于组件属性,以标明实体Bean的组件属性. @AttributeOverrides:该注释作用于组 ...

  6. MyBatis 关系映射XML配置

    关系映射 在我看来这些实体类就没啥太大关联关系,不就是一个sql语句解决的问题,直接多表查询就完事,程序将它设置关联就好 xml里面配置也是配置了sql语句,下面给出几个关系的小毛驴(xml) 一对多 ...

  7. hibernate注解方式来处理映射关系

    在hibernate中,通常配置对象关系映射关系有两种,一种是基于xml的方式,另一种是基于annotation的注解方式,熟话说,萝卜青菜,可有所爱,每个人都有自己喜欢的配置方式,我在试了这两种方式 ...

  8. mybatis crud_MyBatis教程– CRUD操作和映射关系–第2部分

    mybatis crud 为了说明这一点,我们正在考虑以下示例域模型: 会有用户,每个用户可能都有一个博客,每个博客可以包含零个或多个帖子. 这三个表的数据库结构如下: CREATE TABLE us ...

  9. hibernate annotation注解方式来处理映射关系

    2019独角兽企业重金招聘Python工程师标准>>> 在hibernate中,通常配置对象关系映射关系有两种,一种是基于xml的方式,另一种是基于annotation的注解方式,熟 ...

  10. MyBatis 3(2)实体映射 CRUD 操作(XML/注解方式)

    MyBatis 实体映射 Mybatis 配置数据映射主要有 2 种方式,通过 XML 配置文件进行,通过注解进行: Mybatis 对于实体映射是通过接口调用的方式,在底层上使用 Java 的动态代 ...

最新文章

  1. 简单认识Hexo的目录结构
  2. SpringFlux入门(上篇)
  3. 0X8009310B (ASN:276) win7安装证书时出现错误消息:找不到与此证书文件相关联的证书申请微软官方文档
  4. ChannelFactory.Endpoint 上的地址属性为空。ChannelFactory 的终结点必须指定一个有效的地址。...
  5. SSH整合中,使用父action重构子类action类.(在父类中获取子类中的泛型对象)
  6. 仅对此用户禁用 java_Spring Security实现禁止用户重复登陆的配置原理
  7. 【英语学习】【Python】Programming in Python 3 的目录
  8. ubuntu中如何查看系统信息(uname)
  9. Redis数据结构及内部编码
  10. TG Pro for mac电脑温度管理工具
  11. spring实战笔记6---springMVC的请求过程
  12. r730服务器安装系统蓝屏6,安装系统蓝屏解决解决方法
  13. python中的dot是什么意思_Python 的NumPy 库中dot()函数详解
  14. Cadence软件盗版
  15. 倪光南院士:构建安全可控的信息技术体系,云宏高安全的云计算关键核心技术先行
  16. 前端浏览器常见兼容性问题及解决方案
  17. 一招教你用Kettle整合大数据和Hive,HBase的环境!
  18. 0基础快速入门C语言单片机编程
  19. BCM43438 android6.0移植
  20. tplink软件升级有用吗_tp-link路由器固件怎么升级更新

热门文章

  1. BestCoder Round #89
  2. 【网络流24题】No.4 魔术球问题 (二分+最小路径覆盖)
  3. 关于 Unix 用户权限及进程权限及 Saved set-user-id
  4. URAL 1934 Black Spot --- 最短的简单修改
  5. 判断触摸的点在那个 View上
  6. 1.1 OC类的认识
  7. MP3文件格式说明 (转)
  8. windows程序设设计(2) SDK贴图
  9. 关于企业软件资质申请流程以及时间规划(二)——软件登记测试
  10. shell编程之正则表达式与文本工具