Mybatis often encountered in writing query list when queried, Mybatis the contingency table queries written as follows:
(1) according to the corresponding class information classId inquiry, including students, teachers
-- 创建表和数据: create database mybatis; use mybatis; CREATE TABLE users(id INT PRIMARY KEY AUTO_INCREMENT, NAME VARCHAR(20), age INT); INSERT INTO users(NAME, age) VALUES('Tom', 12); INSERT INTO users(NAME, age) VALUES('Jack', 11); CREATE TABLE student( s_id INT PRIMARY KEY AUTO_INCREMENT, s_name VARCHAR(20), class_id INT );
// Class public class Clazz { Private int CID; Private String cName; Private String teacherId; Private Teacher Teacher; // the Teacher encapsulated into the class // GET, SET } // teacher public class Teacher { Private int ID; Private String name; // GET, SET }
1-one or many-to-time
Secondary Query
<?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.zhiyou100.hhz.dao.ClazzDao"> <select id="selectById" parameterType="int" resultMap="mymap"> select * from class where c_id=#{cid} </select> <select id="selectByTeacherId" resultType="com.zhiyou100.hhz.bean.Teacher"> select t_id tid,t_name tname from teacher where t_id=#{tid} </select> <resultMap type="com.zhiyou100.hhz.bean.Clazz" id="mymap"> <id column="c_id" property="cId"/> <result column="c_name" property="cName"/> <result column="teacher_id" property="teacherId"/> <association property="teacher" javaType="com.zhiyou100.hhz.bean.Teacher" column="teacher_id" select="selectByTeacherId"></association> </resultMap> </mapper>
United-table query
<?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.zhiyou100.hhz.dao.ClazzDao"> <select id="selectByTeacherId" resultMap="TeacherMap"> select * from teacher t join class c on t.t_id=c.teacher_id where c_id=#{id} </select> <resultMap type="com.zhiyou100.hhz.bean.Clazz" id="TeacherMap"> <id column="c_id" property="cId"/> <result column="c_name" property="cName"/> <result column="teacher_id" property="teacherId"/> <association property="teacher" javaType="com.zhiyou100.hhz.bean.Teacher"> <result column="t_id" property="tId"/> <result column="t_name" property="tName"/> </association> </resultMap> </mapper>
2. many
Secondary Query
<?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.zhiyou100.hhz.dao.ClazzDao"> <select id="selectById" parameterType="int" resultMap="mymap"> select * from class where c_id=#{cid} </select> <select id="selectByTeacherId" resultType="com.zhiyou100.hhz.bean.Teacher"> select t_id tid,t_name tname from teacher where t_id=#{tid} </select> <select id="selectByClassId" resultType="com.zhiyou100.hhz.bean.Student"> select s_id id,s_name name from student where class_id=#{classid} </select> <resultMap type="com.zhiyou100.hhz.bean.Clazz" id="mymap"> <id column="c_id" property="cId"/> <result column="c_name" property="cName"/> <result column="teacher_id" property="teacherId"/> <association property="teacher" javaType="com.zhiyou100.hhz.bean.Teacher" column="teacher_id" select="selectByTeacherId"></association> <collection property="students" ofType="com.zhiyou100.hhz.bean.Student" select="selectByClassId" column="c_id"></collection> </resultMap> </mapper>
United-table query
<?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.zhiyou100.hhz.dao.ClazzDao"> <select id="selectByTeacherId" resultMap="TeacherMap"> select * from teacher t join class c on t.t_id=c.teacher_id join student s on s.class_id=c.c_id where c_id=#{id} </select> <resultMap type="com.zhiyou100.hhz.bean.Clazz" id="TeacherMap"> <id column="c_id" property="cId"/> <result column="c_name" property="cName"/> <result column="teacher_id" property="teacherId"/> <association property="teacher" javaType="com.zhiyou100.hhz.bean.Teacher"> <result column="t_id" property="tId"/> <result column="t_name" property="tName"/> </association> <collection property="students" ofType="com.zhiyou100.hhz.bean.Student"> <result column="s_id" property="id"/> <result column="s_name" property="name"/> </collection> </resultMap> </mapper>