1.SQL语句建表
CREATE TABLE teacher(
t_id INT PRIMARY KEY AUTO_INCREMENT, t_name VARCHAR(20)
);
CREATE TABLE class(
c_id INT PRIMARY KEY AUTO_INCREMENT, c_name VARCHAR(20),
teacher_id INT
);
ALTER TABLE class ADD CONSTRAINT fk_teacher_id FOREIGN KEY (teacher_id) REFERENCES teacher(t_id);
INSERT INTO teacher(t_name) VALUES('LS1'); INSERT INTO teacher(t_name) VALUES('LS2');
INSERT INTO class(c_name, teacher_id) VALUES('bj_a', 1); INSERT INTO class(c_name, teacher_id) VALUES('bj_b', 2);
sql
2.实体类
package com.zhengbin.entity; 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 + "]";
}
public Teacher(int id, String name) {
super();
this.id = id;
this.name = name;
}
public Teacher() {
super();
// TODO Auto-generated constructor stub
}
}
Teacher.java
package com.zhengbin.entity; public class Classes {
private int id;
private String name;
private Teacher teacher;
@Override
public String toString() {
return "Classes [id=" + id + ", name=" + name + ", teacher=" + teacher + "]";
}
public Classes() {
super();
}
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;
}
}
Classes.java
3.实体类映射文件
两种方式:
1. 联表查询
关键是返回值的设置
2. 执行两次查询
关键是返回值的设置,和第二次查询所需参数的传递
<?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">
<!-- 每个映射文件的namespace应该是唯一的 -->
<mapper namespace="com.zhengbin.entity.classMapper">
<!-- parameterType 参数表示需要参数的类型 -->
<!-- resultType 参数表示返回结果的类型,该可以写为实体包的全路径,或者在conf.xml配置文件中,声明实体的别名 --> <!--
方式一:
嵌套结果:使用嵌套结果映射来处理重复的联合结果的子集 封装联表查询的数据(去除重复的数据)
-->
<select id="getClass" parameterType="int" resultMap="getClassMap">
select * from class c,teacher t where c.teacher_id=t.t_id and c.c_id=#{id};
</select>
<resultMap type="Classes" id="getClassMap">
<id property="id" column="c_id"/>
<result property="name" column="c_name"/>
<association property="teacher" javaType="Teacher">
<id property="id" column="t_id"/>
<result property="name" column="t_name"/>
</association>
</resultMap>
<!--
方式二:
嵌套查询:通过执行另外一个 SQL 映射语句来返回预期的复杂类型
SELECT * FROM class WHERE c_id=1;
SELECT * FROM teacher WHERE t_id=1 //1 是上一个查询得到的 teacher_id 的值
-->
<select id="getClass2" parameterType="int" resultMap="getClass2Map">
select * from class where c_id=#{id}
</select>
<select id="getTeacher" parameterType="int" resultType="Teacher">
select t_id id,t_name name from teacher where t_id=#{id}
</select>
<resultMap type="Classes" id="getClass2Map">
<id property="id" column="c_id"/>
<result property="name" column="c_name"/>
<association property="teacher" select="getTeacher" column="teacher_id"/>
</resultMap>
</mapper>
classMapper.xml
4.测试类
package com.zhengbin.test; import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory; import com.zhengbin.entity.Classes;
import com.zhengbin.util.MyBatisUtils; public class Test3 {
@org.junit.Test
public void getTeacher(){
SqlSessionFactory sessionFactory = MyBatisUtils.getFactory();
// 参数为TRUE,相当于session.commit();
SqlSession session = sessionFactory.openSession(true);
// 读取映射文件
String statement = "com.zhengbin.entity.classMapper" + ".getClass";
// String statement = "com.zhengbin.entity.classMapper" + ".getClass2";
Classes c = session.selectOne(statement,1);
System.out.println(c);
session.close();
}
}
Test3.java
5.几个关键的属性
association : 用于一对一的关联查询
property : 对象属性的名称
javaType : 对象属性的类型
column : 所对应的外键字段名称
select : 使用另一个查询封装的结果