MyBatis系列之关联查询(一对一)

 

我们以一个例子demo的形式,说明Mybatis的一对一关联查询

设计teachers表

classes表设置外键

classes表

 

classes表是课程表,teachers表是教师信息表

这里是一对一的关联关系,当然实际中一般不是这样的,一般可以是多对多,不过这里只是举例子

项目的结构,如图:

 

Classes.java:


  
  1. package com.mybatis.entity;
  2. import java.util.List;
  3. public class Classes {
  4. private int id;
  5. private String name;
  6. private Teacher teacher;
  7. private List<Student> students;
  8. public List<Student> getStudents() {
  9. return students;
  10. }
  11. public void setStudents(List<Student> students) {
  12. this.students = students;
  13. }
  14. public int getId() {
  15. return id;
  16. }
  17. public void setId(int id) {
  18. this.id = id;
  19. }
  20. public String getName() {
  21. return name;
  22. }
  23. public void setName(String name) {
  24. this.name = name;
  25. }
  26. public Teacher getTeacher() {
  27. return teacher;
  28. }
  29. public void setTeacher(Teacher teacher) {
  30. this.teacher = teacher;
  31. }
  32. @Override
  33. public String toString() {
  34. return "Classes [id=" + id + ", name=" + name + "]";
  35. }
  36. }

Teacher.java:

 


  
  1. package com.mybatis.entity;
  2. public class Teacher {
  3. private int id;
  4. private String name;
  5. public int getId() {
  6. return id;
  7. }
  8. public void setId(int id) {
  9. this.id = id;
  10. }
  11. public String getName() {
  12. return name;
  13. }
  14. public void setName(String name) {
  15. this.name = name;
  16. }
  17. }


这里以两种方式进行使用

 

 

 

 

classesMapper.xml:


  
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  3. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  4. <!--
  5. 为这个mapper指定一个唯一的namespace,namespace的值习惯上设置成包名+sql映射文件名,这样就能够保证namespace的值是唯一的
  6. -->
  7. <mapper namespace="com.mybatis.mapper.classesMapper">
  8. <!--
  9. 在select标签中编写查询的SQL语句, 设置select标签的id属性为findById,id属性值必须是唯一的,不能够重复
  10. parameterType可以在MyBatis的配置文件里设置,也可以直接用包名+类名
  11. -->
  12. <select id="findById" parameterType="int" resultMap="ClassesResultMap">
  13. select * from classes c,teachers t where c.teacher_id=t.t_id and c.c_id=#{id}
  14. </select>
  15. <resultMap type="Classes" id="ClassesResultMap">
  16. <id property="id" column="c_id" />
  17. <result property="name" column="c_name" />
  18. <association property="teacher" column="teacher_id" javaType="Teacher">
  19. <id property="id" column="t_id" />
  20. <result property="name" column="t_name" />
  21. </association>
  22. </resultMap>
  23. <select id="findById2" parameterType="int" resultMap="ClassesResultMap2">
  24. select * from classes where c_id=#{id}
  25. </select>
  26. <resultMap type="Classes" id="ClassesResultMap2">
  27. <id property="id" column="c_id" />
  28. <result property="name" column="c_name" />
  29. <association property="teacher" column="teacher_id" javaType="Teacher" select="getTeacherInfo">
  30. </association>
  31. </resultMap>
  32. <select id="getTeacherInfo" parameterType="int" resultType="Teacher">
  33. select t_id,t_name from teachers where t_id=#{id}
  34. </select>
  35. </mapper>

然后对mybatis进行配置:


  
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  3. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  4. <configuration>
  5. <!-- 数据库配置文件 -->
  6. <properties resource="db.properties" />
  7. <!-- 实体类,简称 -设置别名 -->
  8. <typeAliases>
  9. <typeAlias alias="User" type="com.mybatis.entity.User" />
  10. <typeAlias alias="Classes" type="com.mybatis.entity.Classes" />
  11. <typeAlias alias="Student" type="com.mybatis.entity.Student" />
  12. <typeAlias alias="Teacher" type="com.mybatis.entity.Teacher" />
  13. </typeAliases>
  14. <!-- 读取db.properties里的数据,进行数据库配置 -->
  15. <environments default="development">
  16. <environment id="development">
  17. <transactionManager type="JDBC" />
  18. <dataSource type="POOLED">
  19. <property name="driver" value="${driver}"/>
  20. <property name="url" value="${url}" />
  21. <property name="username" value="${username}"/>
  22. <property name="password" value="${password}"/>
  23. </dataSource>
  24. </environment>
  25. </environments>
  26. <!--
  27. 注册userMapper.xml文件
  28. <mappers>
  29. <mapper resource="com/mybatis/mapper/userMapper.xml"/>
  30. </mappers>
  31. -->
  32. <mappers>
  33. <mapper class="com.mybatis.mapper.UserMapper"/>
  34. <mapper resource="com/mybatis/mapper/classesMapper.xml"/>
  35. </mappers>
  36. </configuration>

db,properties:


  
  1. driver=com.mysql.jdbc.Driver
  2. url=jdbc\:mysql\://localhost\:3306/db_mysql?useUnicode\=true&characterEncoding\=utf-8
  3. username=root
  4. password=111



编写测试类:

 


  
  1. String resource = "mybatis-config.xml";
  2. try{
  3. //加载Mybatis的配置文件
  4. Reader reader = Resources.getResourceAsReader(resource);
  5. //构建sqlSession的工厂
  6. SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
  7. //创建可以执行sql的SqlSession
  8. SqlSession session = sessionFactory.openSession();
  9. UserMapper mapper = session.getMapper(UserMapper.class);
  10. User user = mapper.findById(1);
  11. System.out.println(user);
  12. Classes c = session.selectOne("com.mybatis.mapper.classesMapper.findById1",1);
  13. System.out.println(c);
  14. }catch (Exception e) {
  15. e.printStackTrace();
  16. }

 

 

 

 

 

 

 

文章来源: smilenicky.blog.csdn.net,作者:smileNicky,版权归原作者所有,如需转载,请联系作者。

原文链接:smilenicky.blog.csdn.net/article/details/51292271

(完)