1.1. Why use mapping results
Solve the table field names and object properties names are not the same situation (such as: many-to-table, many to many, one to one, one to many ) .
Queries related objects, in mybatis does not default to check out , need to query the results of their own and by resultMap to configure
1.2. The association mapping classification
One pair a : only one employee ID number. Free to design a field party
Pairs a : multiple employees corresponds to a department. In general design of a multi-party property staff inside the design department field
One pair of multi- : a department with more employees. Or in the maintenance of multi-subject relationship
Pairs and more : an employee has multiple roles. A character belonging to multiple employees. Intermediate table to represent
In essence: Many, one is the same, just deal with an association (association). And many, many to many is the same process is a collection (collection)
1.3. The association mapping approach
MyBatis provides two ways to deal with our associated objects, nested queries and nested results .
Nested Results : transmitting . 1 Article SQL, query all the information ( itself + associated objects )
Nested query: transmitting 1 + N article SQL .
Next, each of two many-way, and one-to-many, many-processing.
2.1. Many-to-one to one (find a teacher by the students)
2.1.1. Prepare a Studen.java student class, and a Teacher.java teacher class
Studen.java student class :
/** * Student class, multi-party */ public class Student { private Long id; private String name; private Integer age; private String hobby; /** * Teacher party */ private Teacher teacher; // GET and set and tostring ... (get and set and tostring to, and not too long copy) }
Teacher.java teacher class
/** * Class teacher, a party */ public class Teacher { private Long id; private String name; private Integer age; // GET and set and tostring ... (not too long copy) }
2.1.2 Interface for students to write a class StudenMapper.java
import java.util.List; public interface StudentMapper { // Search student data corresponding to students and teacher data List <Student> queryBy (); }
2.1.2 for students to add Mapper configuration file StudentMapper.xml (emphasis here)
2.1.2.1-- 2.1.2.3 optionally one
2.1.2.1. Nested Results
<!-- 1. Nested Results id = "studentMap": and select the following resultMap = "studentMap" corresponds to the value of the same double quotes property: java class attributes which <id property="id" column="id"></id>:就是主键 column: check out the column names (took alias alias as write sql: t.name tname; that column = "tname") javaType: javatype type of attribute is specified teacher object (e.g., id, name, age) --> <resultMap id="studentMap" type="student"> <id property="id" column="id"></id> <result property="name" column="name"></result> <result property="hobby" column="hobby"></result> <association property="teacher" javaType="teacher"> <id property="id" column="tid"></id> <result property="name" column="tname"></result> <result property="age" column="tage"></result> </association> </resultMap> <!-- id: id value to your StudenMapper.java interface queryBy () method of the same name resultMap: id values above resultMap resultMap value corresponding to the same --> <select id="queryBy" resultMap="studentMap"> SELECT s.id, s.name, s.hobby, t.id time, t.name tname, t.age tage FROM t_student s JOIN t_teacher t on s.tid = t.id </select>
2.1.2.2. The results expand nested
<!--1.1嵌套结果-扩展--> <resultMap id="studentMap" type="Student"> <id property="id" column="id"></id> <result property="name" column="name"></result> <result property="hobby" column="hobby"></result> <!-- property = "teacher.id": You have a teacher in the field Studen.java student class and Teacher teacher field is based, Teacher.teacher fields can be assigned --> <result property="teacher.id" column="cid"></result> <result property="teacher.name" column="cname"></result> <result property="teacher.age" column="cage"></result> </resultMap> <select id="queryBy" resultMap="studentMap"> SELECT s.id,s.name,s.hobby,t.id cid,t.name cname,t.age cage FROM t_student s JOIN t_teacher t ON s.id = t.id </select>
2.1.2.3 nested query
<!--2.1嵌套查询--> <resultMap id="studenMap" type="Student"> <id property="id" column="id"></id> <result property="name" column="name"></result> <result property="hobby" column="hobby"></result> <!-- Properties explained: property: object corresponding field column: data tables returned, this column will be passed as a parameter, according to the method queryByT javaType: Specifies the type of property! select: query method, and its value to <select id = "queryByT" resultType = "Teacher"> ... </ select> Id attribute values match --> <association property="teacher" column="tid" javaType="Teacher" select="queryByT"></association> </resultMap> <select id="queryBy" resultMap="studenMap"> SELECT id,name,hobby,tid FROM t_student </select> <select id="queryByT" resultType="Teacher"> SELECT id,name,age FROM t_teacher WHERE id = #{id} </select>
Or saying a 2.1.2.1-- 2.1.2.3 Choose one
2.2 Test
MyBataisUtil.MYBATAIS.getSqlSession (); see below the package can be derived sqlSession
public class StudentMapperTest { @Test public void queryBy () throws Exception { // MyBataisUtil.MYBATAIS.getSqlSession () themselves encapsulated SQLSESSION the SqlSession SQLSESSION = MyBataisUtil.MYBATAIS.getSqlSession (); StudentMapper mapper = sqlSession.getMapper(StudentMapper.class); List<Student> students = mapper.queryBy(); for (Student student : students) { System.out.println(student); } } }
Package sqlSession:
create a MyBataisUtil.java class
// Enumeration singleton pattern achieved package SQLSESSION public enum MyBataisUtil { MYBATAIS; private static SqlSessionFactory sqlSessionFactory; static { the try { // read the configuration file Reader = Resources.getResourceAsReader Reader ( "the Config.xml-MyBatis" ); // Get sqlSessionFactory (sql session factory) SqlSessionFactory = new new the SqlSessionFactoryBuilder () Build (Reader).; } catch (IOException e) { e.printStackTrace (); } } public SqlSession getSqlSession(){ //获取sqlSession SqlSession sqlSession = sqlSessionFactory.openSession(); return sqlSession; } }