Mybatis的注解应用之关系映射

来源:互联网 发布:mysql if exists用法 编辑:程序博客网 时间:2024/06/02 07:13

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

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package com.owen.mybatis.mappers;  
  2. public interface StudentMapper  
  3. {  
  4. @Select("SELECT * FROM STUDENTS")  
  5. @Results({  
  6. @Result(id=true, column="stud_id", property="studId"),  
  7. @Result(column="name", property="name"),  
  8. @Result(column="email", property="email"),  
  9. @Result(column="addr_id", property="address.addrId")  
  10. })  
  11. List<Student> findAllStudents();  
  12. }  
例如我们可以看到下面的findStudentBy()和findAliStudents()的方法。

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. @Select("SELECT * FROM STUDENTS WHERE STUD_ID=#{studId}")  
  2. @Results({  
  3. @Result(id=true, column="stud_id", property="studId"),  
  4. @Result(column="name", property="name"),  
  5. @Result(column="email", property="email"),  
  6. @Result(column="addr_id", property="address.addrId")  
  7. })  
  8. Student findStudentById(int studId);  
  9. @Select("SELECT * FROM STUDENTS")  
  10. @Results({  
  11. @Result(id=true, column="stud_id", property="studId"),  
  12. @Result(column="name", property="name"),  
  13. @Result(column="email", property="email"),  
  14. @Result(column="addr_id", property="address.addrId")  
  15. })  
  16. List<Student> findAllStudents();  

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

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

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <mapper namespace="com.owen.mybatis.mappers.StudentMapper">  
  2. <resultMap type="Student" id="StudentResult">  
  3. <id property="studId" column="stud_id"/>  
  4. <result property="name" column="name"/>  
  5. <result property="email" column="email"/>  
  6. <result property="phone" column="phone"/>  
  7. </resultMap>  
  8. </mapper>  

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

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. public interface StudentMapper  
  2. {  
  3. @Select("SELECT * FROM STUDENTS WHERE STUD_ID=#{studId}")  
  4. @ResultMap("com.owen.mybatis.mappers.StudentMapper.StudentResult")  
  5. Student findStudentById(int studId);  
  6. @Select("SELECT * FROM STUDENTS")  
  7. @ResultMap("com.owen.mybatis.mappers.StudentMapper.StudentResult")  
  8. List<Student> findAllStudents();  
  9. }  

1.一对一映射

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

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. public interface StudentMapper  
  2. {  
  3. @Select("SELECT ADDR_ID AS ADDRID, STREET, CITY, STATE, ZIP, COUNTRY  
  4. FROM ADDRESSES WHERE ADDR_ID=#{id}")  
  5. Address findAddressById(int id);  
  6. @Select("SELECT * FROM STUDENTS WHERE STUD_ID=#{studId} ")  
  7. @Results({  
  8. @Result(id=true, column="stud_id", property="studId"),  
  9. @Result(column="name", property="name"),  
  10. @Result(column="email", property="email"),  
  11. @Result(property="address", column="addr_id",  
  12. one=@One(select="com.owen.mybatis.mappers.StudentMapper.  
  13. findAddressById"))  
  14. })  
  15. Student selectStudentWithAddress(int studId);  
  16. }  

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

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. int studId = 1;  
  2. StudentMapper studentMapper =  
  3. sqlSession.getMapper(StudentMapper.class);  
  4. Student student = studentMapper.selectStudentWithAddress(studId);  
  5. System.out.println("Student :"+student);  
  6. System.out.println("Address :"+student.getAddress());  

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

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <mapper namespace="com.owen.mybatis.mappers.StudentMapper">  
  2. <resultMap type="Address" id="AddressResult">  
  3. <id property="addrId" column="addr_id"/>  
  4. <result property="street" column="street"/>  
  5. <result property="city" column="city"/>  
  6. <result property="state" column="state"/>  
  7. <result property="zip" column="zip"/>  
  8. <result property="country" column="country"/>  
  9. </resultMap>  
  10. <resultMap type="Student" id="StudentWithAddressResult">  
  11. <id property="studId" column="stud_id"/>  
  12. <result property="name" column="name"/>  
  13. <result property="email" column="email"/>  
  14. <association property="address" resultMap="AddressResult"/>  
  15. </resultMap>  
  16. </mapper>  
[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. public interface StudentMapper  
  2. {  
  3. @Select("select stud_id, name, email, a.addr_id, street, city,  
  4. state, zip, country"+" FROM students s left outer join addresses a  
  5. on s.addr_id=a.addr_id"+" where stud_id=#{studId} ")  
  6. @ResultMap("com.owen.mybatis.mappers.StudentMapper.  
  7. StudentWithAddressResult")  
  8. Student selectStudentWithAddress(int id);  
  9. }  

2. 一对多映射

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

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

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. public interface TutorMapper  
  2. {  
  3. @Select("select addr_id as addrId, street, city, state, zip,  
  4. country from addresses where addr_id=#{id}")  
  5. Address findAddressById(int id);  
  6. @Select("select * from courses where tutor_id=#{tutorId}")  
  7. @Results({  
  8. @Result(id=true, column="course_id", property="courseId"),  
  9. @Result(column="name", property="name"),  
  10. @Result(column="description", property="description"),  
  11. @Result(column="start_date" property="startDate"),  
  12. @Result(column="end_date" property="endDate")  
  13. })  
  14. List<Course> findCoursesByTutorId(int tutorId);  
  15. @Select("SELECT tutor_id, name as tutor_name, email, addr_id  
  16. FROM tutors where tutor_id=#{tutorId}")  
  17. @Results({  
  18. @Result(id=true, column="tutor_id", property="tutorId"),  
  19. @Result(column="tutor_name", property="name"),  
  20. @Result(column="email", property="email"),  
  21. @Result(property="address", column="addr_id",  
  22. one=@One(select=" com.owen.mybatis.  
  23. mappers.TutorMapper.findAddressById")),  
  24. @Result(property="courses", column="tutor_id",  
  25. many=@Many(select="com.owen.mybatis.mappers.TutorMapper.  
  26. findCoursesByTutorId"))  
  27. })  
  28. Tutor findTutorById(int tutorId);  
  29. }  

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

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

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. mapper namespace="com.owen.mybatis.mappers.TutorMapper">  
  2. <resultMap type="Address" id="AddressResult">  
  3. <id property="addrId" column="addr_id"/>  
  4. <result property="street" column="street"/>  
  5. <result property="city" column="city"/>  
  6. <result property="state" column="state"/>  
  7. <result property="zip" column="zip"/>  
  8. <result property="country" column="country"/>  
  9. </resultMap>  
  10. <resultMap type="Course" id="CourseResult">  
  11. <id column="course_id" property="courseId"/>  
  12. <result column="name" property="name"/>  
  13. <result column="description" property="description"/>  
  14. <result column="start_date" property="startDate"/>  
  15. <result column="end_date" property="endDate"/>  
  16. </resultMap>  
  17. <resultMap type="Tutor" id="TutorResult">  
  18. <id column="tutor_id" property="tutorId"/>  
  19. <result column="tutor_name" property="name"/>  
  20. <result column="email" property="email"/>  
  21. <association property="address" resultMap="AddressResult"/>  
  22. <collection property="courses" resultMap="CourseResult"/>  
  23. </resultMap>  
  24. </mapper>  

调用方法

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. public interface TutorMapper  
  2. {  
  3. @Select("SELECT T.TUTOR_ID, T.NAME AS TUTOR_NAME, EMAIL,  
  4. A.ADDR_ID, STREET, CITY, STATE, ZIP, COUNTRY, COURSE_ID, C.NAME,  
  5. DESCRIPTION, START_DATE, END_DATE FROM TUTORS T LEFT OUTER  
  6. JOIN ADDRESSES A ON T.ADDR_ID=A.ADDR_ID LEFT OUTER JOIN COURSES  
  7. C ON T.TUTOR_ID=C.TUTOR_ID WHERE T.TUTOR_ID=#{tutorId}")  
  8. @ResultMap("com.owen.mybatis.mappers.TutorMapper.TutorResult")  
  9. Tutor selectTutorById(int tutorId);  
  10. }  
0 0
原创粉丝点击