这次查询的表中引入了外键。两个表关联起来了,如何进行查询?
一、代码实现
(1)表结构
创建class表的外键teacherid和teacher表的id。
插入数据:
(2)写出两个对应的实体类Teacher和Class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
package test; 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; } @Override public String toString() { return "Teacher [id=" + id + ", name=" + name + "]"; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
package test; class Class1 { 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; } @Override public String toString() { return "Class [id=" + id + ", name=" + name + ", teacher=" + teacher + "]"; } } |
这里需要特别注意:
因为class表中有一个teacherID字段,所以要在Class类中定义一个teacher属性,用于维护teacher和class之间的一对一关系。
如果在表中有另一个表的属性关联,那么在建立实体类的时候就需要建立两个实体类,其中一个在另一个之中作为一个属性。
(3)写出sql映射文件ClassMapper1.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
<?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="test.ClassMapper1"> <!-- 方式一:嵌套结果:使用嵌套结果映射来处理重复的联合结果的子集 封装联表查询的数据(去除重复的数据) --> <!-- select * from class c,teacher t where c.teacherid = t.id and c.id = #{id} --> <resultMap type="test.Class1" id="ClassResultMap1"> <id property="id" column="id" /> <result property="name" column="name" /> <!-- 使用resultMap映射实体类和字段之间的一一对应关系 --> <association property="teacher" javaType="test.Teacher"> <id property="id" column="id" /> <result property="name" column="name" /> </association> </resultMap> <select id="selectbyid1" parameterType="int" resultMap="ClassResultMap1"> select * from class c,teacher t where c.teacherid = t.id and c.id = #{id} </select> <!-- 方式二:嵌套查询:通过执行另外一个SQL映射语句来返回预期的复杂类型 --> <resultMap type="test.Class1" id="ClassResultMap2"> <id property="id" column="id"/> <result property="name" column="name"/> <association property="teacher" column="teacherid" select="getTeacher"></association> </resultMap> <select id="getTeacher" parameterType="int" resultType="test.Teacher"> select id , name from teacher where id = #{id} </select> <select id="selectbyid2" parameterType="int" resultMap="ClassResultMap2"> select * from class where id = #{id} </select> </mapper> |
可以看见有两种方式:
1.嵌套结果
使用嵌套结果映射来处理重复的联合结果的子集 封装联表查询的数据(去除重复的数据)。
就是用resultMap去定义回显格式,然后完成输出。
2.嵌套查询
通过执行另外一个SQL映射语句来返回预期的复杂类型。
就是一开始的类先独立进行查询,然后包含的类再次进行查询,把结果放在resultMap定义返回格式。
(4)在mybatis配置文件conf.xml中注册sql配置文件ClassMapper1.xml
conf.xml
1 2 3 4 |
<!-- 注册mapper --> <mappers> <mapper resource="test/ClassMapper1.xml"/> </mappers> |
(5)最后写出测试类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
package test; import java.io.InputStream; import java.util.List; import org.apache.ibatis.annotations.Result; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.Test; public class test { public static SqlSessionFactory getFactory() { String resource = "conf.xml"; InputStream inputStream = test.class.getClassLoader().getResourceAsStream(resource); SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream); return factory; } @Test public void selectbyid1(){ SqlSession sqlSession = getFactory().openSession(); String statement = "test.ClassMapper1.selectbyid1"; List<Class1> list = sqlSession.selectList(statement, 1); System.out.println(list.toString()); } @Test public void selectbyid2(){ SqlSession sqlSession = getFactory().openSession(); String statement = "test.ClassMapper1.selectbyid2"; List<Class1> list = sqlSession.selectList(statement, 1); System.out.println(list.toString()); } } |
测试结果:
1 |
[Class [id=1, name=godlikexie, teacher=Teacher [id=1, name=xie]]] |
成功进行查询。
二、总结
以上就是简单的单对单关联表查询方式,下次尝试进行多对多关联表查询。
[…] mybatis 完成一对一关联单表查询 […]