t_student 表(学生表)
t_grade(年级表):
首先来看看一对多
Grade类:
import java.util.List;
public class Grade {
private int gid;
private String gname;
private List<Student> students;
get....set(大家都懂省略感觉这样看着更加清晰)
Student:
public class Student {
private int id;
private String name;
private int gradeId;
get...set....
获取session的工具类:
import java.io.IOException;
import java.io.InputStream;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class FactoryUtil {
private static SqlSessionFactory sessionFactory=null;
static{//单例
if(sessionFactory==null){
String url="mybatis-config.xml";
InputStream is=null;
try {
is = Resources.getResourceAsStream(url);
sessionFactory=new SqlSessionFactoryBuilder().build(is);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static SqlSession getSession(){
return sessionFactory.openSession();
}
}
GradeMapper接口:
public interface GradeMapper {
public Grade findGrade(Integer gid);
}
StudentMapper接口:
public interface StudentMapper {
public Student findStudent(Integer gradeId);
}
GradeMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.czy.mappers.GradeMapper">
<resultMap type="Grade" id="grade">
<id property="gid" column="gid"/>
<result property="gname" column="gname"/>
<collection property="students" ofType="Student">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="gradeId" column="gradeId"/>
</collection>
</resultMap>
<select id="findGrade" parameterType="Integer" resultMap="grade">
select *from t_grade t1,t_student t2 where t1.gid=t2.gradeId and t1.gid=#{gid}
</select>
</mapper>
StudentMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.czy.mappers.StudentMapper">
<resultMap type="Student" id="student">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="gradeId" column="gradeId"/>
</resultMap>
<select id="findStudent" parameterType="Integer" resultMap="student">
select *from t_student where gradeId=#{gradeId}
</select>
</mapper>
main:
public class Tmain {
public static void main(String[] args) {
SqlSession session=FactoryUtil.getSession();
GradeMapper mapper=session.getMapper(GradeMapper.class);
Grade grade=mapper.findGrade(1);
System.out.println(grade.toString());
ArrayList<Student> list=(ArrayList<Student>) grade.getStudents();
for(Student one:list){
System.out.println(one.toString());
}
}
}
运行结果:
上面写的是一对多,也就是通过年级(一)来查找学生(多),下面将在上面的基础上加上多对一,也就是通过学生来查询年级
Student类参照上文,不做修改
Grade类:添加一个Grade对象,后面set 、get方法加上
public class Student {
private int id;
private String name;
private int gradeId;
private Grade grade;
GradeMapper接口:
public interface GradeMapper {
public Grade findGrade(Integer gid);//通过gid来找Grade
public Grade toStudentfindGrade(Integer gid);//通过gid来查找grade,但是这个是用StudentMapper发起查找,是先调用StudentMapper中的findStudentId方法然后将gradeId传过来做参数
}
StudentMapper:
public interface StudentMapper {
public Student findStudentid(Integer id);//通过id来查找student
}
GradeMapper.xml
<mapper namespace="com.czy.mappers.GradeMapper">
<resultMap type="Grade" id="grade">
<id property="gid" column="gid"/>
<result property="gname" column="gname"/>
<collection property="students" ofType="Student">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="gradeId" column="gradeId"/>
</collection>
</resultMap>
<select id="findGrade" parameterType="Integer" resultMap="grade">
select *from t_grade t1,t_student t2 where t1.gid=t2.gradeId and t1.gid=#{gid}
</select>
<select id="toStudentfindGrade" parameterType="Integer" resultMap="grade">
select *from t_grade where gid=#{gid}
</select>
</mapper>
StudentMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.czy.mappers.StudentMapper">
<resultMap type="Student" id="student">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="gradeId" column="gradeId"/>
<association property="grade" select="com.czy.mappers.GradeMapper.toStudentfindGrade" column="gid"/>
</resultMap>
<select id="findStudent" parameterType="Integer" resultMap="student">
select *from t_student where gradeId=#{gradeId}
</select>
<select id="findStudentid" parameterType="Integer" resultMap="student">
select *from t_student t1,t_grade t2 where t1.gradeId=t2.gid and t1.id=#{id}
</select>
</mapper>