mybatis多对一与一对多
步骤:
1.创建maven项目
2.编写工具类
3.编写实体类
4.编写mapper接口
5.配置xml
6.测试
多对一:多个学生关联一个老师
工具类:
//sqlSessionFactory -->sqlSession
public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory;
static {
try {//获取sqlSession对象
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession(true);
}
}
其中
String resource = "mybatis-config.xml";
mybatis-config.xml中需要注册mapper和连接数据库等操作
实体类:
public class Student {
private int id;
private String name;
private Teacher teacher;
}
//省略get/set方法
public class Teacher {
private int id;
private String name;
}
//省略get/set方法
mapper:
public interface TeacherMapper {
Teacher getTeacher(@Param("tid") Integer id);
Teacher getTeacher2(@Param("tid") Integer id);
}
xml:
方式一(嵌套查询)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.kuang.dao.StudentMapper">
<select id="getStudent" resultMap="StudentTeacher">
select * from mybatis.student
</select>
<resultMap id="StudentTeacher" type="Student">
<id property="id" column="id"></id>
<id property="name" column="name"></id>
<association property="teacher" column="tid" javaType="Teacher" select="getTeacher"></association>
</resultMap>
<select id="getTeacher" resultType="Teacher">
select * from teacher where id = #{id}
</select>
</mapper>
<association property="teacher" column="tid" javaType="Teacher" select="getTeacher"></association>
中的select要与下面的select标签中的id一致,相当于在里面在嵌套一个查询语句
方式二(子查询)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.kuang.dao.StudentMapper">
<select id="getStudent2" resultMap="StudentTeacher2">
select s.id sid,s.name sname,t.name tname,t.id teid
from mybatis.student s,mybatis.teacher t where s.tid = t.id
</select>
<resultMap id="StudentTeacher2" type="Student">
<result property="id" column="sid"></result>
<result property="name" column="sname"></result>
<association property="teacher" javaType="Teacher">
<result property="id" column="teid"></result>
<result property="name" column="tname"></result>
</association>
</resultMap>
</mapper>
对以上标签解释:
namespace是命名空间 对应的是一个Dao/Mapper接口(注意要写的是全限定类名!!)
select标签中的id名要与相关mapper中的方法名保持一致!!
resultMap是对复杂类型的结果集映射(resultMap中的id要与select中的resultMap中的名称一致!!)
type属性也可以直接写全限定类名,若想简洁一点必须在xml文件中配置别名!!!
resultMap中的id字段是对主键进行映射,而result是对普通字段进行映射
result标签中的property对应的是实体类中的属性名,colum对应的是数据库中的字段名
javaType 用来指定实体类的属性 ofType 是指泛型中的约束类型(一对多会用到)
一对多:一个老师教多名学生
实体类中的属性有所改变
public class Teacher {
private int id;
private String name;
//一个老师拥有多个学生
private List<Student> students;
}
public class Student {
private int id;
private String name;
private int tid;
}
这里返回的是一个集合List 而上面的多对一返回的是一个对象Teacher
<select id="getTeacher" resultMap="StudentTeacher">
SELECT s.id sid,s.name sname,t.name tname,t.id tid
FROM teacher t,student s where t.id = s.tid and t.id=#{tid}
</select>
<resultMap id="StudentTeacher" type="Teacher">
<result property="id" column="tid"></result>
<result property="name" column="tname"></result>
<collection property="students" ofType="Student">
<result property="id" column="sid"></result>
<result property="name" column="sname"></result>
<result property="tid" column="tid"></result>
</collection>
</resultMap>
ofType 是指泛型中的约束类型 老师实体中有的属性是
List<Student> students
其中List是实体类的属性 Student就是泛型约束类型
总结:
对于复杂的属性 我们需要单独处理 对象:association 集合:collection
多对一中是使用association(因为在学生实体类中Teacher是一个对象) 而一对多中是使用到collection(因为查询到的是一个集合类型)
嵌套查询sql语句会简单一点,但是映射时会复杂一些,而子查询是sql语句复杂一些,映射会相对简单和容易理解

浙公网安备 33010602011771号