示例:注解一对多
程序清单:com.po.Student.java
package com.po;
public class Student {
private Integer id;
private String name;
private String sex;
private Integer age;
private Clazz clazz;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Clazz getClazz() {
return clazz;
}
public void setClazz(Clazz clazz) {
this.clazz = clazz;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", sex=" + sex + ", age=" + age + "]";
}
}
程序清单:com.po.Clazz.java
package com.po;
import java.util.List;
public class Clazz {
private Integer id;
private String code;
private String name;
private List<Student> students;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
}
程序清单:com.mapper.StudentMapper.java
package com.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import com.po.Student;
public interface StudentMapper {
@Select("select * from t_student where cid=#{cid}")
@Results({@Result(id=true,column="id",property="id"),
@Result(column="name",property="name"),
@Result(column="sex",property="sex"),
@Result(column="age",property="age")})
List<Student> getStudentById(Integer cid);
}
程序清单:com.mapper.ClazzMapper.java
package com.mapper;
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 org.apache.ibatis.mapping.FetchType;
import com.po.Clazz;
public interface ClazzMapper {
@Select("select * from t_clazz where id=#{id}")
@Results({@Result(id=true,column="id",property="id"),
@Result(column="code",property="code"),
@Result(column="name",property="name"),
@Result(column="id",property="students",
many=@Many(select="com.mapper.StudentMapper.getStudentById",fetchType=FetchType.LAZY))})
Clazz getClazzById(Integer id);
}
程序清单:com.dao.TestDao.java
@Test
public void getClazzById() throws IOException {
session=getSqlSession();
ClazzMapper cm=session.getMapper(ClazzMapper.class);
Clazz clazz=cm.getClazzById(1);
System.out.println("Clazz [id="+clazz.getId()+", code="+clazz.getCode()+", name="+clazz.getName()+"]");
List<Student> students=clazz.getStudents();
for (Student student : students) {
System.out.println(student);
}
}
运行TestDao的getClazzById()方法,控制台显示如下: