本文主要是介绍mybatis高级查询(一对一查询),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
实例需求
一个老师对应一个班级,根据班级id查询班级信息(带老师的信息)
- 老师实体类Teacher.java
package com;public class Teacher {private int id; private String name; 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;}@Overridepublic String toString() {return "Teacher [id=" + id + ", name=" + name + "]";}
}
- 班级实体类Classes.java
package com;public class Classes {private int id; private String name;private 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 Teacher getTeacher() {return teacher;}public void setTeacher(Teacher teacher) {this.teacher = teacher;}@Overridepublic String toString() {return "Classes [id=" + id + ", name=" + name + ", teacher=" + teacher+ "]";}
}
- classMapper.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.classMapper"><!-- 根据班级id查询班级信息(带老师的信息)##1. 联表查询SELECT * FROM class c,teacher t WHERE c.teacher_id=t.t_id AND c.c_id=1;##2. 执行两次查询SELECT * FROM class WHERE c_id=1; //teacher_id=1SELECT * FROM teacher WHERE t_id=1;//使用上面得到的teacher_id--><!-- 方法一: 联表查询 --><select id="getClass" parameterType="int" resultMap="ClassResultMap">select * from class c, teacher t where c.teacher_id=t.t_id and c.c_id=#{id}</select><resultMap type="com.Classes" id="ClassResultMap"><id property="id" column="c_id"/><result property="name" column="c_name"/><association property="teacher" javaType="com.Teacher"><id property="id" column="t_id"/><result property="name" column="t_name"/></association></resultMap><!-- 方法二:嵌套查询 --><select id="getClass2" parameterType="int" resultMap="ClassResultMap2">select * from class c where c_id=#{id}</select><resultMap type="com.Classes" id="ClassResultMap2"><id property="id" column="c_id"/><result property="name" column="c_name"/><association property="teacher" column="teacher_id" select="getTeacher" /></resultMap><select id="getTeacher" parameterType="int" resultType="com.Teacher">SELECT t_id id, t_name name FROM teacher WHERE t_id=#{id}</select>
</mapper>
- 测试类TestTeacerAndClasses.java
package pojo;import java.io.IOException;
import java.io.Reader;import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;import com.Classes;public class TestTeacerAndClasses {public static void main(String[] args) throws IOException {String resource = "sqlConfig.xml";Reader reader = Resources.getResourceAsReader(resource);SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder().build(reader);SqlSession session = sqlMapper.openSession();
// Classes classes = session.selectOne("com.classMapper.getClass",1);// 根据班级id查询班级信息(带老师的信息)
// System.out.println(classes);Classes classes = session.selectOne("com.classMapper.getClass2",1);System.out.println(classes);}
}
这篇关于mybatis高级查询(一对一查询)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!