Mybatis的注解应用之关系映射

我们可以应用内联的或运用@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);
}












你可能感兴趣的:(java,spring,mybatis,web开发)