This case is a maven project:
1. build a database environment:
Environmental data corresponding to more than one student is a teacher:
CREATE TABLE `teacher` ( `id` INT(10) NOT NULL, `name` VARCHAR(30) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=INNODB DEFAULT CHARSET=utf8 INSERT INTO teacher(`id`, `name`) VALUES (1, '秦老师'); CREATE TABLE `student` ( `id` INT(10) NOT NULL, `name` VARCHAR(30) DEFAULT NULL, `tid` INT(10) DEFAULT NULL, PRIMARY KEY (`id`), KEY `fktid` (`tid`), CONSTRAINT `fktid` FOREIGN KEY (`tid`) REFERENCES `teacher` (`id`) ) ENGINE=INNODB DEFAULT CHARSET=utf8 INSERT INTO `student` (`id`, `name`, `tid`) VALUES ('1' , ' Military students ' , ' . 1 ' ); the INSERT the INTO `student` (id``, `name`,` tid`) the VALUES ( ' 2 ' , ' LiTongXue ' , ' . 1 ' ); the INSERT the INTO `Student `(` id`, `name`,` tid`) the VALUES ( ' . 3 ' , ' military students ' , ' . 1 ' ); the INSERT the INTO `student` (id``, `name`,`tid`) VALUES ( '4 ' , ' Yao students ' , ' 1 ' ); the INSERT the INTO `student` (id``, `name`,` tid`) the VALUES ( ' . 5 ' , ' summer students ' , ' 1 ' );
Requirements: Requirements to get all the student's name, student number, corresponding to the teacher's name. (Note: involves two tables)
2. student's pojo entity classes:
public class Student { Private int ID; Private String name; Private int TID; Private Teacher Teacher; // note corresponding to a plurality of teacher student public Student () { } public Student ( int ID, String name, int TID, Teacher Teacher) { the this .id = ID; the this .name = name; the this .tid = TID; the this .teacher = Teacher; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getTid() { return tid; } public void setTid(int tid) { this.tid = tid; } public Teacher getTeacher() { return teacher; } public void setTeacher(Teacher teacher) { this.teacher = teacher; } @Override public String toString() { return "Student{" + "id=" + id + ", name='" + name + '\'' + ", tid=" + tid + ", teacher=" + teacher + '}'; } }
3.Dao layer write StudentMapper interface:
public interface StudentDAO {
// 1; // get all students // by way of a database, a table lookup even List <Student> getStudents (); // Method two:
// get all students // is the object-oriented manner, query List <Student> getStudents2 (); }
Write 4.StudentMapper.xml file:
<?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.xbf.dao.StudentDao"> <select id="getStudents" resultMap="StudentTeacher"> select * from student </select> <resultMap id="StudentTeacher" type="Student"> <id column="id" property="id"/> <result column="name" property="name"/> <association column="tid" property="teacher" javaType="Teacher" select="getTeacherName"/> </resultMap> <select id="getTeacherName" resultType="Teacher"> select name from teacher where id=#{id} </select> <!--cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc--> <select id="getStudents2" resultMap="StudentTeacher2"> select s.id,s.name,t.name as tname from student s,teacher t where s.tid=t.id </select> <resultMap id="StudentTeacher2" type="Student"> <id column="id" property="id"/> <result column="name" property="name"/> <association property="teacher" javaType="Teacher"> <result property="name" column="tname"></result> </association> </resultMap> </mapper>
Core write 5.mybatis-config.xml file:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <properties resource="database.properties"/> <settings> <setting name="logImpl" value="LOG4J"/> </settings> <typeAliases> <package name="com.xbf.pojo"></package> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"></transactionManager> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/xbf/dao/teacherMapper.xml"></mapper> </mappers> </configuration>
6. Test preparation classes:
public class StudentDaoTest { @Test public void getStudents(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); StudentDao mapper = sqlSession.getMapper(StudentDao.class); List<Student> students = mapper.getStudents(); for (Student student : students) { System.out.println(student.getName()+student.getTeacher().getName()); } } @Test public void getStudents2(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); StudentDao mapper = sqlSession.getMapper(StudentDao.class); List<Student> students = mapper.getStudents2(); for (Student student : students) { System.out.println(student.getName()+student.getTeacher().getName()); } } }
to sum up:
mybatis in the case of many-to get in StudentMapper.xml (corresponding to the interface configuration file) use assosiation label, there are two ways to solve the problem:
Method 1: Use connections to the database table query;
Method 2: Use object associated object-oriented.