Mybatis連表查詢
阿新 • • 發佈:2020-09-15
Mybatis 連表查詢
準備
建立資料表
SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for student -- ---------------------------- CREATE TABLE `student` ( `id` int(255) NOT NULL AUTO_INCREMENT, `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, `teacher_id` int(11) NOT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic; -- ---------------------------- -- Records of student -- ---------------------------- INSERT INTO `student` VALUES (1, '小王', 1); INSERT INTO `student` VALUES (2, '小李', 1); INSERT INTO `student` VALUES (3, '小孫', 1); SET FOREIGN_KEY_CHECKS = 1; -- ---------------------------- -- Table structure for teacher -- ---------------------------- DROP TABLE IF EXISTS `teacher`; CREATE TABLE `teacher` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic; -- ---------------------------- -- Records of teacher -- ---------------------------- INSERT INTO `teacher` VALUES (1, '張老師'); SET FOREIGN_KEY_CHECKS = 1;
建立實體類
public class Student { private int id; private String name; private Teacher teacher; public Student(int id, String name, Teacher teacher) { this.id = id; this.name = name; this.teacher = teacher; } public Student() { } 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 Teacher getTeacher() { return teacher; } public void setTeacher(Teacher teacher) { this.teacher = teacher; } @Override public String toString() { return "Student{" + "id=" + id + ", name='" + name + '\'' + ", teacher=" + teacher + '}'; } }
public class Teacher { private int id; private String name; public Teacher(int id, String name) { this.id = id; this.name = name; } public 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; } @Override public String toString() { return "Teacher{" + "id=" + id + ", name='" + name + '\'' + '}'; } }
多對一
建立介面類
public interface StudentDao {
List<Student> getStudentList();
}
配置xml
<?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.example.dao.StudentDao">
<select id="getStudentList" resultMap="studentResultMap">
select
s.id, s.name as s_name, t.name as t_name
from
student as s, teacher as t
where
s.teacher_id = t.id;
</select>
<resultMap id="studentResultMap" type="com.example.pojo.Student">
<result property="id" column="id"/>
<result property="name" column="s_name"/>
<association property="teacher" javaType="com.example.pojo.Teacher">
<result property="name" column="t_name"/>
</association>
</resultMap>
</mapper>
在mybatis裡面註冊
<mappers>
<mapper resource="studentMapper.xml"/>
</mappers>
一對多
實體類
import lombok.Data;
@Data
public class Student {
private int id;
private String name;
private int teacherId;
}
import lombok.Data;
import java.util.List;
@Data
public class Teacher {
private int id;
private String name;
private List<Student> students;
}
介面類
import com.example.pojo.Teacher;
import org.apache.ibatis.annotations.Param;
public interface TeacherDao {
Teacher getTeacherById(@Param("tid") int id);
}
xml配置
<?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.example.dao.TeacherDao">
<select id="getTeacherById" resultMap="teacherResultMap">
select
s.id as s_id, s.name as s_name, s.teacher_id as st_id, t.name as t_name
from
student as s, teacher as t
where
s.teacher_id = t.id and t.id = #{tid};
</select>
<resultMap id="teacherResultMap" type="com.example.pojo.Teacher">
<result property="id" column="id"/>
<result property="name" column="t_name"/>
<collection property="students" ofType="com.example.pojo.Student">
<result property="id" column="s_id"/>
<result property="name" column="s_name"/>
<result property="teacherId" column="st_id"/>
</collection>
</resultMap>
</mapper>
最後將xml配置到mybatis的配置裡面
<mappers>
<mapper resource="teacherMapper.xml"/>
</mappers>