阅读更多
1. MyBatis3注解的关系映射
1) 一对一映射;
2) 一对多映射;
package com.andrew.mappers;
import org.apache.ibatis.annotations.Select;
import com.andrew.model.Address;
public interface AddressMapper {
@Select("select * from t_address where id=#{id}")
public Address findById(Integer id);
}
package com.andrew.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.andrew.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.andrew.mappers.StudentMapper.selectStudentByGradeId"))
})
public Grade findById(Integer id);
}
package com.andrew.mappers;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import com.andrew.model.Student;
public interface StudentMapper {
@Insert("insert into t_student values(null,#{name},#{age},1,1,1,1)")
public int insertStudent(Student student);
@Update("update t_student set name=#{name},age=#{age} where id=#{id}")
public int updateStudent(Student student);
@Delete("delete from t_student where id=#{id}")
public int deleteStudent(int id);
@Select("select * from t_student where id=#{id}")
public Student getStudentById(Integer id);
@Select("select * from t_student")
@Results({
@Result(id=true,column="id",property="id"),
@Result(column="name",property="name"),
@Result(column="age",property="age")
})
public List findStudents();
@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.andrew.mappers.AddressMapper.findById"))
})
public Student selectStudentWithAddress(int id);
@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.andrew.mappers.AddressMapper.findById"))
})
public Student selectStudentByGradeId(int gradeId);
@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.andrew.mappers.AddressMapper.findById")),
@Result(column="gradeId",property="grade",one=@One(select="com.andrew.mappers.GradeMapper.findById"))
})
public Student selectStudentWithAddressAndGrade(int id);
}
package com.andrew.model;
public class Address {
private Integer id;
private String sheng;
private String shi;
private String qu;
@Override
public String toString() {
return "Address [id=" + id + ", sheng=" + sheng + ", shi=" + shi
+ ", qu=" + qu + "]";
}
// getter and setter
}
package com.andrew.model;
import java.util.List;
public class Grade {
private Integer id;
private String gradeName;
private List students;
@Override
public String toString() {
return "Grade [id=" + id + ", gradeName=" + gradeName + "]";
}
// getter and setter
}
package com.andrew.model;
public class Student {
private Integer id;
private String name;
private Integer age;
private Address address;
private Grade grade;
public Student() {
super();
}
public Student(String name, Integer age) {
super();
this.name = name;
this.age = age;
}
public Student(Integer id, String name, Integer age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age
+ ", address=" + address + ", grade=" + grade + "]";
}
// getter and setter
}
package com.andrew.service;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.andrew.mappers.GradeMapper;
import com.andrew.mappers.StudentMapper;
import com.andrew.model.Grade;
import com.andrew.model.Student;
import com.andrew.util.SqlSessionFactoryUtil;
public class StudentGradeTest {
private SqlSession sqlSession = null;
private StudentMapper studentMapper = null;
private GradeMapper gradeMapper = null;
@Before
public void setUp() throws Exception {
sqlSession = SqlSessionFactoryUtil.openSession();
studentMapper = sqlSession.getMapper(StudentMapper.class);
gradeMapper = sqlSession.getMapper(GradeMapper.class);
}
@After
public void tearDown() throws Exception {
sqlSession.close();
}
@Test
public void testSelectStudentWithAddress() {
Student student = studentMapper.selectStudentWithAddress(2);
System.out.println(student);
}
@Test
public void testSelectGradeWithStudents() {
Grade grade = gradeMapper.findById(2);
System.out.println(grade);
List studentList = grade.getStudents();
for (Student student : studentList) {
System.out.println(student);
}
}
@Test
public void testSelectStudentWithAddressAndGrade() {
Student student = studentMapper.selectStudentWithAddressAndGrade(1);
System.out.println(student);
}
}