一对一关联查询(一对一关联的时候使用association标签)
1、创建有两个关联性的类
package com.mybatis.test;
public class Student {
private int id;
private String student_id;
private String student_name;
private studentCard studentcard;//该类与studentCard类关联
public Student() {
}
public Student(int id, String student_id, String student_name) {
this.id = id;
this.student_id = student_id;
this.student_name = student_name;
}
public studentCard getStudentcard() {
return studentcard;
}
public void setStudentcard(studentCard studentcard) {
this.studentcard = studentcard;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getStudent_id() {
return student_id;
}
public void setStudent_id(String student_id) {
this.student_id = student_id;
}
public String getStudent_name() {
return student_name;
}
public void setStudent_name(String student_name) {
this.student_name = student_name;
}
}
package com.mybatis.test;
public class studentCard {
private int cardid;
private String cardname;
public int getCardid() {
return cardid;
}
public void setCardid(int cardid) {
this.cardid = cardid;
}
public String getCardname() {
return cardname;
}
public void setCardname(String cardname) {
this.cardname = cardname;
}
}
2、设置xml的配置
<!-- 进行一对一关联查询 -->
<select id="relationselect" parameterType="int" resultMap="relationMap">
select s.*,c.* from student s inner join studentcard c
on s.cardid=c.cardid
where id = #{id}
</select>
<resultMap type="com.mybatis.test.Student" id="relationMap">
<id property="id" column="id"></id>
<result property="student_id" column="student_id"/>
<result property="student_name" column="student_name"/>
<!-- 一对一关联的时候使用association标签,javaType:指定该属性的类型 -->
<association property="studentcard" javaType="com.mybatis.test.studentCard">
<id property="cardid" column="cardid"/>
<result property="cardname" column="cardname"/>
</association>
</resultMap>
3、在映射类中加入该方法
package com.mybatis.mapper;
import java.util.List;
import com.mybatis.test.Student;
import com.mybatis.test.StudentName;
public interface StudentMapper {
Student relationselect(int in);
}
4、进行测试
//进行一对一的关联查询
public static void relationselect() throws IOException {
Reader read = Resources.getResourceAsReader("config.xml");
SqlSessionFactory sqlsession = new SqlSessionFactoryBuilder().build(read);
SqlSession session = sqlsession.openSession();
//
// String statement = "com.mybatis.test.studentMapper.studentSelect";
// Student stu = session.selectOne(statement, 1);
StudentMapper studentmapper = session.getMapper(StudentMapper.class);
Student stud = studentmapper.relationselect(1);
System.out.println(stud.getStudent_id()+","+stud.getStudent_name()+","+
stud.getStudentcard().getCardid()+","+stud.getStudentcard().getCardname());
session.close();
}
一对多关联查询(一对一关联的时候使用collection标签)
1、创建对应类
package com.mybatis.test;
public class Student {
private int id;
private String student_id;
private String student_name;
private int classid;
public Student() {
}
public Student(String student_id , String student_name) {
this.student_id = student_id;
this.student_name = student_name;
}
public int getClassid() {
return classid;
}
public void setClassid(int classid) {
this.classid = classid;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getStudent_id() {
return student_id;
}
public void setStudent_id(String student_id) {
this.student_id = student_id;
}
public String getStudent_name() {
return student_name;
}
public void setStudent_name(String student_name) {
this.student_name = student_name;
}
package com.mybatis.test;
import java.util.List;
public class studentClass {
private int classid;
private String classname;
//使用集合来存储对应的多个学生
List <Student> student;
public int getClassid() {
return classid;
}
public List<Student> getStudent() {
return student;
}
public void setStudent(List<Student> student) {
this.student = student;
}
public void setClassid(int classid) {
this.classid = classid;
}
public String getClassname() {
return classname;
}
public void setClassname(String classname) {
this.classname = classname;
}
}
2、配置xml文件中的SQL
<!-- 进行一对多关联查询 -->
<select id="morerelationselect" parameterType="String" resultMap="morerelationMap">
select s.*,c.* from class c
inner join student s
on c.classid=s.classid
where c.classname=#{classname}
</select>
<resultMap type="com.mybatis.test.studentClass" id="morerelationMap">
<id property="classid" column="classid"/>
<result property="classname" column="classname"/>
<collection property="student" ofType="com.mybatis.test.Student">
<id property="id" column="id"/>
<result property="student_id" column="student_id"/>
<result property="student_name" column="student_name"/>
</collection>
</resultMap>
3、设置xml配置对应的方法
package com.mybatis.mapper;
import java.util.List;
import com.mybatis.test.Student;
import com.mybatis.test.StudentName;
import com.mybatis.test.studentClass;
public interface StudentMapper {
studentClass morerelationselect(String classname);
}
4、进行测试
//进行一对多的关联查询
public static void morerelationselect() throws IOException {
Reader read = Resources.getResourceAsReader("config.xml");
SqlSessionFactory sqlsession = new SqlSessionFactoryBuilder().build(read);
SqlSession session = sqlsession.openSession();
//
// String statement = "com.mybatis.test.studentMapper.studentSelect";
// Student stu = session.selectOne(statement, 1);
StudentMapper studentmapper = session.getMapper(StudentMapper.class);
studentClass stud = studentmapper.morerelationselect("class2");
List <Student> scl = stud.getStudent();
System.out.println(stud.getClassid()+","+stud.getClassname());
for (Student ss : scl) {
System.out.println(ss.getId()+","+ss.getStudent_id()+","+ss.getStudent_name());
}
session.close();
}