Mybatis 学习(四)
2020-05-26 本文已影响0人
时代劫匪
多对一处理
测试环境搭建(参考之前)
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Teacher {
private int id;
private String name;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
private int id;
private String name;
private Teacher teacher;
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- 核心配置文件 -->
<!-- 思路:1.查询多有学生的信息
2.根据查询出来的学生的tid,寻找对应的老师!-->
<mapper namespace="com.sun.dao.StudentMapper">
<select id="getStudentList" resultMap="StudentTeacher">
select * from student
</select>
<resultMap id="StudentTeacher" type="Student">
<result property="id" column="id"></result>
<result property="name" column="name"></result>
<!-- 复杂的属性,我们需要单独处理 对象 :association 集合:collection -->
<association property="teacher" column="tid" javaType="Teacher" select="getteacher"></association>
</resultMap>
<select id="getteacher" resultType="Teacher">
select * from teacher
</select>
<select id="getStudentList2" resultMap="StudentMap" >
select a.id sid,a.name sname,a.tid ,b.name teachername from student a, teacher b where a.tid=b.id
</select>
<resultMap id="StudentMap" type="Student">
<result property="id" column="sid"></result>
<result property="name" column="sname"></result>
<association property="teacher" javaType="Teacher">
<result property="id" column="tid"></result>
<result property="name" column="teachername"></result>
</association>
</resultMap>
</mapper>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- 核心配置文件 -->
<mapper namespace="com.sun.dao.TeacherMapper">
</mapper>
package com.sun.dao;
import com.sun.pojo.Student;
import java.util.List;
public interface StudentMapper {
List<Student> getStudentList();
List<Student> getStudentList2();
}
package com.sun.dao;
import com.sun.pojo.Teacher;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface TeacherMapper {
@Select("select * from Teacher")
List<Teacher> getTeacher();
}
mybatis-config.xml
<?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"></properties>
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
<!-- 可以给实体类起别名 -->
<typeAliases>
<package name="com.sun.pojo"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper class="com.sun.dao.TeacherMapper"></mapper>
<mapper class="com.sun.dao.StudentMapper"></mapper>
</mappers>
</configuration>
===============================================================
总结
- 按查询嵌套处理
<select id="getStudentList" resultMap="StudentTeacher">
select * from student
</select>
<resultMap id="StudentTeacher" type="Student">
<result property="id" column="id"></result>
<result property="name" column="name"></result>
<!-- 复杂的属性,我们需要单独处理 对象 :association 集合:collection -->
<association property="teacher" column="tid" javaType="Teacher" select="getteacher"></association>
</resultMap>
<select id="getteacher" resultType="Teacher">
select * from teacher
</select>
- 按结果嵌套处理
<select id="getStudentList2" resultMap="StudentMap" >
select a.id sid,a.name sname,a.tid ,b.name teachername from student a, teacher b where a.tid=b.id
</select>
<resultMap id="StudentMap" type="Student">
<result property="id" column="sid"></result>
<result property="name" column="sname"></result>
<association property="teacher" javaType="Teacher">
<result property="id" column="tid"></result>
<result property="name" column="teachername"></result>
</association>
</resultMap>