这里讲一下使用注解方式实现mybatis的关系映射(一对一映射和一对多映射)
一、一对一映射
1.新建类Address(直接从前面讲使用xml配置方式实现关系映射的工程中复制即可):
package com.test.model;
public class Address {
private Integer id;
private String sheng;
private String shi;
private String qu;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getSheng() {
return sheng;
}
public void setSheng(String sheng) {
this.sheng = sheng;
}
public String getShi() {
return shi;
}
public void setShi(String shi) {
this.shi = shi;
}
public String getQu() {
return qu;
}
public void setQu(String qu) {
this.qu = qu;
}
@Override
public String toString() {
return "Address [id=" + id + ", sheng=" + sheng + ", shi=" + shi + ", qu=" + qu + "]";
}
}
2.新建AddressMapper接口:这里使用注解的方式实现通过id查找地址信息
package com.test.mappers;
import com.test.model.Address;
public interface AddressMapper {
@Select("select * from t_address where id=#{id}")
public Address findById(Integer id);
}
3.表t_student中添加字段addressId,并其与t_address关联
4.查询学生的时候,把地址信息也查询出来。这就涉及到了一对一的关联。类Student中添加Address对象:
生成这个对象的get和set方法,并重写一下Student类的toString方法,把所有的属性打印输出。
5.使用注解方式添加操作方法接口:
@Select("select * from t_student where id=#{id}")
@Results(
{
@Result(id=true,column="id",property="id"),
@Result(column="name",property="name"),
@Result(column="age",property="age"),
@Result(column="addressId",property="address",one=@One(select="com.test.mappers.AddressMapper.findById"))
}
)
public Student selectStudentWidhAddress(Integer id);
与使用xml配置文件进行对比,这里就不难理解了。主要是记住语法。
6.添加测试方法:
@Test
public void testSelectStudentWidhAddress() {
logger.info("注解方式查找带地址的学生信息");
Student student = studentMapper.selectStudentWidhAddress(1);
System.out.println(student);
}
运行测试方法,控制台输出:
这里查询到了要查的学生。
二、一对多映射
1.与之前使用xml配置一样,把Grade类和GradeMapper复制过来。
package com.test.model;
import java.util.List;
public class Grade {
private Integer id;
private String gradeName;
private List<Student> students;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getGradeName() {
return gradeName;
}
public void setGradeName(String gradeName) {
this.gradeName = gradeName;
}
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
@Override
public String toString() {
return "Grade [id=" + id + ", gradeName=" + gradeName + "]";
}
}
GradeMapper:这里使用注解方式实现通过id查找年级信息。
package com.test.mappers;
import org.apache.ibatis.annotations.Select;
import com.test.model.Grade;
public interface GradeMapper {
public Grade findById(Integer id);
}
2. t_student表中添加gradeId字段,并把表t_student和t_grade进行关联。
3.通过年级查找出学生信息。使用注解方式实现,修改GradeMapper为:
package com.test.mappers;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import com.test.model.Grade;
public interface GradeMapper {
@Select("select * from t_grade where id=#{id}")
@Results(
{
@Result(id=true,column="id",property="id"),
@Result(column="gradeName",property="gradeName"),
@Result(column="id",property="students",many=@Many(select="com.test.mappers.StudentMapper.findStudentsByGradeId"))
}
)
public Grade findById(Integer id);
}
这里注解的意义和之前使用xml配置一样,不再讲述。这里查找学生信息时会把查询结果的id列的值带入到StudentMapper中的findStudentsByGradeId方法中。
4.在StudentMapper中新建findStudentsByGradeId方法:
@Select("select * from t_student where gradeId=#{gradeId}")
@Results(
{
@Result(id=true,column="id",property="id"),
@Result(column="name",property="name"),
@Result(column="age",property="age"),
@Result(column="addressId",property="address",one=@One(select="com.test.mappers.AddressMapper.findById"))
}
)
public List<Student> findStudentsByGradeId(Integer gradeId);
5.添加测试方法:
@Test
public void testSelectGradeWidhStudent() {
logger.info("注解方式查找带学生的地址信息");
Grade grade = gradeMapper.findById(1);
System.out.println(grade);
List<Student> studentList = grade.getStudents();
for(Student s:studentList){
System.out.println(s);
}
}
运行这个测试方法,查找到了班级信息以及该班级下的所有学生信息。
这里实现的通过年级查找学生信息。
三、实现双向查询
上面已经实现了通过年级查找年级中的所有学生信息。反过来也应该能够通过学生查找到学生所在的年级信息。
1.StudentMapper中新建方法:
@Select("select * from t_student where id=#{id}")
@Results(
{
@Result(id=true,column="id",property="id"),
@Result(column="name",property="name"),
@Result(column="age",property="age"),
@Result(column="addressId",property="address",one=@One(select="com.test.mappers.AddressMapper.findById")),
@Result(column="gradeId",property="grade",one=@One(select="com.test.mappers.GradeMapper.findById"))
}
)
public Student selectStudentWidhAddressAndGrade(Integer id);
2.新建测试方法:
@Test
public void testSelectStudentWidhAddressAndGrade() {
logger.info("注解方式查找带地址和年级的学生信息");
Student student = studentMapper.selectStudentWidhAddressAndGrade(1);
System.out.println(student);
}
运行测试方法,查找到了学生以及学生所在的班级信息。