需求:一个教师只带一个班 一个班只有一个老师 根据班级id查询班级信息(带老师的信息)
1、目录结构
2、在数据库中建表并插入数据
教师表
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);
3、定义实体类
package com.sxau.rjxy.MyBatis_test5;
public class Classes {
private int id;
private String name;
private Teacher teacher;
public Classes(int id,String name,Teacher teacher){
this.id = id;
this.name = name;
this.teacher = teacher;
}
public Classes(){}
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 "Classes [id=" + id + ", name=" + name + ", teacher=" + teacher + "]";
}
}
package com.sxau.rjxy.MyBatis_test5;
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 + "]";
}
}
4、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.sxau.rjxy.MyBatis_test5.ClassMapper">
<select id="getClass2" parameterType="int" resultMap="getClass2Map">
select * from class where c_id=#{id}
</select>
<select id="getTeacher" parameterType="int"
resultType="com.sxau.rjxy.MyBatis_test5.Teacher">
select t_id id,t_name name from teacher where t_id=#{id}
</select>
<resultMap type="com.sxau.rjxy.MyBatis_test5.Classes" id="getClass2Map">
<id property="id" column="c_id"/>
<result property="name" column="c_name"/>
<association property="teacher" column="teacher_id" select="getTeacher">
</association>
</resultMap>
<!--
<select id="getClass" parameterType ="int"
resultType="Order" resultMap="ClassResultMapper">
select * from class c,teacher t where c.teacher_id = t.t_id and c.c_id = #{id}
</select>
<resultMap type = "com.sxau.rjxy.MyBatis_test5.Classes" id="ClassResultMapper">
<id property="id" column="c_id" />
<id property="name" column="c_name" />
<association property="teacher" javaType="com.sxau.rjxy.MyBatis_test5.Teacher">
<id property="id" column="t_id"/>
<result property="name" column="t_name"/>
</association>
</resultMap>
-->
</mapper>
两种查询方法 注释的为嵌套查询。未被注释的是分开查询。
conf.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="db.properties" >
</properties>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${driver}" />
<property name="url" value="${url}" /><!-- 数据库名可更改 -->
<property name="username" value="${username}" />
<property name="password" value="${password}" /><!-- 密码可更改 -->
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/sxau/rjxy/MyBatis_test/userMapper.xml" />
<mapper resource="com/sxau/rjxy/MyBatis_test2/userMapper1.xml" /><!-- 这个是右键userMapper.xml选择 copy qualified Name复制,并删除com之前的东西-->
<mapper resource="com/sxau/rjxy/MyBatis_test3/userMapper.xml" />
<mapper resource="com/sxau/rjxy/MyBatis_test4/orderMapper.xml" />
<mapper resource="com/sxau/rjxy/MyBatis_test5/ClassMapper.xml" />
</mappers>
</configuration>
测试类
package com.sxau.rjxy.MyBatis_test5;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import com.sxau.rjxy.MyBatis_test2.MyBatisUtils;
public class Test5 {
public static void main(String[] args){
//构建sqlSession的工厂
SqlSessionFactory factory = MyBatisUtils.getFactory();
//创建能执行映射文件中sql的sqlSession
SqlSession session = factory.openSession();
//映射sql的标识字符串
String statement = "com.sxau.rjxy.MyBatis_test5.ClassMapper.getClass";
statement = "com.sxau.rjxy.MyBatis_test5.ClassMapper.getClass2";//分开查询的时候多一句这个
//执行查询返回一个唯一user对象的sql
Classes c = session.selectOne(statement, 1);
System.out.println(c);
session.close();
}
}