United-table query
-- 创建表和数据: 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 );
You need to be associated with the class table in the main category
For example student teacher, for teachers-many, and many-is for students, a teacher multiple student
// Class public class Clazz { Private int CID; Private String cName; Private String teacherId; Private Teacher Teacher; // the class encapsulates the Teacher // GET, SET } // teacher public class Teacher { Private int ID; Private String name; // GET, SET }
One to One: (position does not matter sql statement, may also be below the above)
One way: Nested Results: By using nested result mappings combined result of the processing to repeat the subset
Package data table associated query (remove duplicate data)
<select id="getUsers" resultMap="ClazzMapper"> select * from class c,teacher t where t.t_id=c.teacher_id and c.c_id=#{cid} </select> <resultMap type="com.zhiyou100.wyf.bean.Clazz" id="ClazzMapper"> <id column="c_id" property="cid"/> <result column="c_name" property="cname"/> <association property="teacher" column="teacher_id" javaType="com.zhiyou100.wyf.bean.Teacher"> <id property="tid" column="t_id"/> <result property="tname" column="t_name"/> </association> </resultMap>
Second way: nested query: returns expected by executing another mapped SQL statement is complex type
SELECT * FROM class WHERE c_id=1;
Value SELECT * FROM teacher WHERE t_id = 1 // 1 is on a query of the resulting teacher_id
<resultMap type="com.zhiyou100.wyf.bean.Clazz" id="ClazzMapper"> <id column="c_id" property="cid"/> <result column="c_name" property="cname"/> <result column="teacher_id" property="teacher_id"/> <association property="teacher" column="teacher_id" select="com.zhiyou100.wyf.dao.TeacherDao.getTeacher" javaType="com.zhiyou100.wyf.bean.Teacher"> </association> </resultMap> <select id="getUsers" resultMap="ClazzMapper"> select * from class where c_id =#{cid} </select>
<select id="getTeacher" resultType="com.zhiyou100.wyf.bean.Teacher"> select t_id tid,t_name tname from teacher where t_id=#{tid} </select>
Many:
Creating tables and data
CREATE TABLE student( s_id INT PRIMARY KEY AUTO_INCREMENT, s_name VARCHAR(20), class_id INT ); INSERT INTO student(s_name, class_id) VALUES('xs_A', 1); INSERT INTO student(s_name, class_id) VALUES('xs_B', 1); INSERT INTO student(s_name, class_id) VALUES('xs_C', 1); INSERT INTO student(s_name, class_id) VALUES('xs_D', 2); INSERT INTO student(s_name, class_id) VALUES('xs_E', 2); INSERT INTO student(s_name, class_id) VALUES('xs_F', 2);
A mode : Nested Results : By using nested result mapping result of the joint processing to duplicate a subset of
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=1
<resultMap type="com.zhiyou100.wyf.bean.Clazz" id="ClazzMapper"> <id column="c_id" property="cid"/> <result column="c_name" property="cname"/> <result column="teacher_id" property="teacher_id"/> <association property="teacher" column="teacher_id" select="com.zhiyou100.wyf.dao.TeacherDao.getTeacher" javaType="com.zhiyou100.wyf.bean.Teacher"> </association> <collection property="student" ofType="com.zhiyou100.wyf.bean.Student"> <id property="s_id" column="s_id"/> <result property="s_name" column="s_name"/> </collection> </resultMap> <select id="getUsers" resultMap="ClazzMapper"> select * from class c,student s,teacher t where c.teacher_id=t.t_id and s.class_id=c.c_id and c.c_id=#{cid} </select>
Second way: Nesting: By executing another
SQL
to return the expected complex type mapped statement
SELECT * FROM class WHERE c_id=1;
SELECT * FROM teacher WHERE t_id = 1 // 1
is a query to get the
teacher_id
value
SELECT * FROM student WHERE class_id = 1 // 1
is obtained by the first query
c_id
value field
<mapper namespace="com.zhiyou100.wyf.dao.ClazzDao"> <select id="selectById" parameterType="int" resultMap="mymap"> select * from class where c_id=#{cid} </select> <select id="selectByTeacherId" resultType="com.zhiyou100.wyf.bean.Teacher"> select t_id tid,t_name tname from teacher where t_id=#{tid} </select> <select id="selectByClassId" resultType="com.zhiyou100.wyf.bean.Student"> select s_id id,s_name name from student where class_id=#{classid} </select> <resultMap type="com.zhiyou100.wyf.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.wyf.bean.Teacher" column="teacher_id" select="selectByTeacherId"></association> <collection property="students" ofType="com.zhiyou100.wyf.bean.Student" select="selectByClassId" column="c_id"></collection> </resultMap> </mapper>
Note: Mybatis the difference between the $ and #
1. # {} Is a pre-compiler process, $ {} string is replaced.
2.Mybatis when processing # {}, will be in sql number # {} with the call set assignment method PreparedStatement to?;
When processing 3.Mybatis $ {}, {} $ is to replace the value of the variable.
4.} # {SQL injection can effectively prevent, improve system security.
https://www.cnblogs.com/xdp-gacl/p/4262895.html