数据库表:
t_class
t_student
实体类:
Student.java
public class Student {
private Integer sid;
private String name;
private Cla cla;
public Student() {
}
public Student(Integer sid, String name) {
this.sid = sid;
this.name = name;
}
public Integer getSid() {
return sid;
}
public void setSid(Integer sid) {
this.sid = sid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Cla getCla() {
return cla;
}
public void setCla(Cla cla) {
this.cla = cla;
}
@Override
public String toString() {
return "Student{" +
"sid=" + sid +
", name='" + name + '\'' +
", cla=" + cla +
'}';
}
}
Cla.java
package com.example.mybatis;
import java.util.List;
public class Cla {
private Integer cid;
private String cname;
private List<Student> students;
@Override
public String toString() {
return "Cla{" +
"cid=" + cid +
", cname='" + cname + '\'' +
", students=" + students +
'}';
}
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
public Cla() {
}
public Cla(Integer cid, String cname) {
this.cid = cid;
this.cname = cname;
}
public Integer getCid() {
return cid;
}
public void setCid(Integer cid) {
this.cid = cid;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
}
一、collection处理
ClatwoMapper.java
package com.example.mybatis;
import org.apache.ibatis.annotations.Param;
public interface ClatwoMapper {
Cla getquery(@Param("cid") Integer cid);
}
ClatwoMapper.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.example.mybatis.ClatwoMapper">
<resultMap id="get" type="com.example.mybatis.Cla">
<id property="cid" column="cid"></id>
<result property="cname" column="cname"></result>
<collection property="students" ofType="com.example.mybatis.Student">
<id property="sid" column="sid"></id>
<result property="name" column="name"></result>
</collection>
</resultMap>
<select id="getquery" resultMap="get">
select cla.*,student.* from t_class cla left join t_student student on cla.cid = student.cid where cla.cid = #{cid}
</select>
</mapper>
test.java
@Test
public void getquery() throws IOException {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
SqlSession sqlSession = sqlSessionFactoryBuilder.build(is).openSession(true);
Cla getquery = sqlSession.getMapper(ClatwoMapper.class).getquery(1);
System.out.println(getquery);
}
二、分步查询
ClatwoMapper.java
package com.example.mybatis;
import org.apache.ibatis.annotations.Param;
public interface ClatwoMapper {
Cla getClaAndStuone(@Param("cid") Integer cid);
}
ClatwoMapper.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.example.mybatis.ClatwoMapper">
<resultMap id="ClaAndStu" type="com.example.mybatis.Cla">
<id property="cid" column="cid"></id>
<result property="cname" column="cname"></result>
<collection property="students"
select="com.example.mybatis.StudentoneMapper.getClaAndStutwo"
column="cid">
</collection>
</resultMap>
<select id="getClaAndStuone" resultMap="ClaAndStu">
select * from t_class where cid = #{cid}
</select>
</mapper>
StudentoneMapper.java
package com.example.mybatis;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface StudentoneMapper {
List<Student> getClaAndStutwo(@Param("cid") Integer cid);
}
StudentoneMapper.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.example.mybatis.StudentoneMapper">
<select id="getClaAndStutwo" resultType="com.example.mybatis.Student">
select * from t_student where cid = #{cid}
</select>
</mapper>
test.java
@Test
public void queryClaAndStu() throws IOException {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
SqlSession sqlSession = sqlSessionFactoryBuilder.build(is).openSession(true);
Cla cla = sqlSession.getMapper(ClatwoMapper.class).getClaAndStuone(1);
System.out.println(cla);
}