Mybaits總結--一對多查詢
阿新 • • 發佈:2018-11-12
一、表結構
CREATE TABLE `class` ( `c_id` int(2) NOT NULL AUTO_INCREMENT, `c_name` varchar(30) DEFAULT NULL, `teacher_id` int(2) DEFAULT NULL, PRIMARY KEY (`c_id`), KEY `t_id_fk` (`teacher_id`), CONSTRAINT `t_id_fk` FOREIGN KEY (`teacher_id`) REFERENCES `teacher` (`t_id`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8; CREATE TABLE `teacher` ( `t_id` int(2) NOT NULL AUTO_INCREMENT, `t_name` varchar(30) DEFAULT NULL, PRIMARY KEY (`t_id`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8; CREATE TABLE `student` ( `s_id` int(2) NOT NULL AUTO_INCREMENT, `s_name` varchar(30) DEFAULT NULL, `class_id` int(11) DEFAULT NULL, PRIMARY KEY (`s_id`), KEY `c_id_fk` (`class_id`), CONSTRAINT `c_id_fk` FOREIGN KEY (`class_id`) REFERENCES `class` (`c_id`) ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
二、實體,在一的一方有多的一方的集合(這邊例項包含了一對一和一對多)
class實體
public class Classes {
//省去getter和setter
private Integer id;
private String name;
private Teacher teacher;
private List<Student> students;
}
teacher實體
public class Teacher { //省去getter和setter private Integer id; private String name; }
student實體
public class Student {
private Integer id;
private String name;
}
三、mapper對映檔案
1、巢狀結果,使用SQL的聯表查詢,使用<collection>標籤,其中ofType 屬性指定List<T> 中的 T的全類名或者別名
<select id="getClasses" parameterType="int" resultMap="getClassMap"> select * from class c, teacher t, student s where c.teacher_id = t.t_id and c.c_id = s.class_id and c.c_id = #{id} </select> <resultMap type="Classes" id="getClassMap"> <id property="id" column="c_id"/> <result property="name" column="c_name"/> <association property="teacher" javaType="Teacher"> <id property="id" column="teacher_id"/> <result property="name" column="t_id"/> </association> <collection property="students" ofType="Student"> <id property="id" column="s_id"/> <result property="name" column="s_name"/> </collection> </resultMap>
2、巢狀查詢,sql 語句使用多條sql 語句查詢,使用<collection>標籤不需要指定ofType 屬性,List<T> 中的T 根據select 屬性指定的select 的標籤返回的型別,column 屬性指定select 標籤的sql 語句的引數
<select id="getClasses2" parameterType="int" resultMap="getClssesMap2">
select * from class where c_id = #{id}
</select>
<select id="getTeacher" resultType="Teacher">
select t_id id, t_name name from teacher where t_id = #{id}
</select>
<select id="getStudent" resultType="Student">
select s_id id, s_name name from student where class_id = #{id}
</select>
<resultMap type="Classes" id="getClssesMap2">
<id property="id" column="c_id"/>
<result property="name" column="c_name"/>
<association property="teacher" select="getTeacher" column="teacher_id"></association>
<collection property="students" select="getStudent" column="c_id"></collection>
</resultMap>
3.、查詢一對多結果的集合,採用第一種巢狀結果的sql聯表查詢
<select id="getClasses3" resultMap="getClassMap">
select * from class c left join teacher t on c.teacher_id = t.t_id left join student s on c.c_id = s.class_id
</select>
四、呼叫
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Test;
import zhuojing.mybaits.onetoone.Classes;
import zhuojing.mybaits.util.MybaitsUtils;
public class TestOneToMany {
@Test
public void test1(){
SqlSessionFactory factory = MybaitsUtils.getFactory();
SqlSession session = factory.openSession();
String statement ="zhuojing.mybaits.onetomany.classesMapper.getClasses";
Classes classes = session.selectOne(statement,1);
System.out.println(classes);
session.close();
}
@Test
public void test2(){
SqlSessionFactory factory = MybaitsUtils.getFactory();
SqlSession session = factory.openSession();
String statement ="zhuojing.mybaits.onetomany.classesMapper.getClasses2";
Classes classes = session.selectOne(statement,2);
System.out.println(classes);
session.close();
}
@Test
public void test3(){
SqlSessionFactory factory = MybaitsUtils.getFactory();
SqlSession session = factory.openSession();
String statement ="zhuojing.mybaits.onetomany.classesMapper.getClasses3";
List<Classes> list = session.selectList(statement);
for (Classes classes : list) {
System.out.println(classes);
}
session.close();
}
}
import java.io.InputStream;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class MybaitsUtils {
public static SqlSessionFactory getFactory(){
InputStream in = MybaitsUtils.class.getClassLoader().getResourceAsStream("conf.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
return factory;
}
}