1、配置文件

db.properties

  1. db.driver=com.mysql.jdbc.Driver
  2. db.url=jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf8
  3. db.username=root
  4. db.password=123456

SqlMapConfig.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE configuration
  3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  5. <configuration>
  6.  
  7. <!-- 加载java的配置文件 -->
  8. <properties resource="config/db.properties"/>
  9.  
  10. <!-- 配置mybatis的环境信息,与spring整合,该信息由spring来管理 -->
  11. <environments default="development">
  12. <environment id="development">
  13. <!-- 配置JDBC事务控制,由mybatis进行管理 -->
  14. <transactionManager type="JDBC"></transactionManager>
  15. <!-- 配置数据源,采用mybatis连接池 -->
  16. <dataSource type="POOLED">
  17. <property name="driver" value="${db.driver}" />
  18. <property name="url" value="${db.url}" />
  19. <property name="username" value="${db.username}" />
  20. <property name="password" value="${db.password}" />
  21. </dataSource>
  22. </environment>
  23. </environments>
  24.  
  25. <!-- 加载映射文件 -->
  26. <mappers>
  27. <mapper resource="config/mapper.xml" />
  28. </mappers>
  29.  
  30. </configuration>

mapper.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.xiaostudy.domain.Mapper">
  6.  
  7. <!-- ========================================================================================== -->
  8. <!-- 一对多之resultMap1 -->
  9. <select id="findOfTeacher" resultMap="OfTeacher">
  10. select * from teacher t, student s where t.tid=s.tid and t.tid=#{id}
  11. </select>
  12. <resultMap type="com.xiaostudy.domain.Teacher" id="OfTeacher">
  13. <id column="tid" property="tid"/>
  14. <result column="tname" property="tname"/>
  15. <collection property="student" ofType="com.xiaostudy.domain.Student" ><!-- javaType="HashSet" -->
  16. <id column="sid" property="sid"></id>
  17. <result column="tid" property="tid"/>
  18. <result column="sname" property="sname"/>
  19. </collection>
  20. </resultMap>
  21. <!-- ========================================================================================== -->
  22.  
  23. <!-- ========================================================================================== -->
  24. <!-- 一对多之resultMap2 -->
  25. <select id="findOfTeachers" resultMap="OfTeachers">
  26. select * from teacher t, student s where t.tid=s.tid and t.tid=#{id}
  27. </select>
  28. <resultMap type="com.xiaostudy.domain.Teacher" id="OfTeachers">
  29. <id column="tid" property="tid"/>
  30. <result column="tname" property="tname"/>
  31. <collection property="student" ofType="com.xiaostudy.domain.Student" select="getStudent" column="tid"/>
  32. </resultMap>
  33.  
  34. <select id="getStudent" parameterType="int" resultType="com.xiaostudy.domain.Student">
  35. select * from student s where s.tid = #{id}
  36. </select>
  37. <!-- ========================================================================================== -->
  38.  
  39. </mapper>

2、domain类

Student.java

  1. package com.xiaostudy.domain;
  2.  
  3. public class Student {
  4. private int sid;
  5. private int tid;
  6. private String sname;
  7. private Teacher teacher;
  8.  
  9. public int getSid() {
  10. return sid;
  11. }
  12.  
  13. public void setSid(int sid) {
  14. this.sid = sid;
  15. }
  16.  
  17. public int getTid() {
  18. return tid;
  19. }
  20.  
  21. public void setTid(int tid) {
  22. this.tid = tid;
  23. }
  24.  
  25. public String getSname() {
  26. return sname;
  27. }
  28.  
  29. public void setSname(String sname) {
  30. this.sname = sname;
  31. }
  32.  
  33. public Teacher getTeacher() {
  34. return teacher;
  35. }
  36.  
  37. public void setTeacher(Teacher teacher) {
  38. this.teacher = teacher;
  39. }
  40.  
  41. @Override
  42. public String toString() {
  43. return "Student [sid=" + sid + ", tid=" + tid + ", sname=" + sname
  44. + ", teacher=" + teacher + "]";
  45. }
  46.  
  47. }

Teacher.java

  1. package com.xiaostudy.domain;
  2.  
  3. import java.util.Set;
  4.  
  5. public class Teacher {
  6. private int tid;
  7. private String tname;
  8. private Set<Student> student;
  9.  
  10. public Set<Student> getStudent() {
  11. return student;
  12. }
  13.  
  14. public void setStudent(Set<Student> student) {
  15. this.student = student;
  16. }
  17.  
  18. public int getTid() {
  19. return tid;
  20. }
  21.  
  22. public void setTid(int tid) {
  23. this.tid = tid;
  24. }
  25.  
  26. public String getTname() {
  27. return tname;
  28. }
  29.  
  30. public void setTname(String tname) {
  31. this.tname = tname;
  32. }
  33.  
  34. @Override
  35. public String toString() {
  36. return "Teacher [tid=" + tid + ", tname=" + tname + ", student=" + student + "]";
  37. }
  38.  
  39. }

3、代理类Mapper.java

  1. package com.xiaostudy.domain;
  2.  
  3. import java.util.List;
  4.  
  5. public interface Mapper {
  6.  
  7. // 一对多之resultMap1
  8. public List<Teacher> findOfTeacher(int id);
  9.  
  10. // 一对多之resultMap2
  11. public List<Teacher> findOfTeachers(int id);
  12.  
  13. }

4、测试类

  1. package com.xiaostudy.test;
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.util.List;
  6.  
  7. import org.apache.ibatis.io.Resources;
  8. import org.apache.ibatis.session.SqlSession;
  9. import org.apache.ibatis.session.SqlSessionFactory;
  10. import org.apache.ibatis.session.SqlSessionFactoryBuilder;
  11.  
  12. import com.xiaostudy.domain.Mapper;
  13. import com.xiaostudy.domain.Teacher;
  14.  
  15. /**
  16. * @desc 测试类
  17. * @author xiaostudy
  18. *
  19. */
  20. public class MybatisTest {
  21.  
  22. public static void main(String[] args) throws IOException {
  23. String resource = "config/SqlMapConfig.xml";
  24. InputStream inputStream = Resources.getResourceAsStream(resource);
  25.  
  26. // 创建SqlSessionFactory
  27. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
  28.  
  29. // 创建SqlSession
  30. SqlSession sqlSession = sqlSessionFactory.openSession();
  31.  
  32. // 获取一个代理dao实现
  33. Mapper mapper = sqlSession.getMapper(Mapper.class);
  34.  
  35. //一对多之resultMap1
  36. /*List<Teacher> list = mapper.findOfTeacher(1);
  37. for(Teacher t : list) {
  38. System.out.println(t);
  39. }*/
  40.  
  41. //一对多之resultMap2
  42. List<Teacher> list = mapper.findOfTeachers(1);
  43. for(Teacher t : list) {
  44. System.out.println(t);
  45. }
  46.  
  47. sqlSession.close();
  48.  
  49. }
  50.  
  51. }

5、数据库

teacher表

student表


mybatis的一对多的更多相关文章

  1. mybatis的一对多,多对一,以及多对对的配置和使用

    1.本文章是无意中看见易百教程的Mybatis教程才注意到这个问题,平时都仅仅是在用CRUD,忽略了这方面的问题,真实十分羞愧   2.首先我们开始对mybatis的一对多的探究   根据这个应用场景 ...

  2. Mybatis配置一对多的关联关系(五)

    问题:是查询一个部门中的员工? 一.web项目构架 二.lib文件的jar 三.配置大小配置和该工具类 1大配置mybatis-config.xml <?xml version="1. ...

  3. Mybatis学习——一对多关联表查询

    1.实体类 public class Student { private int id; private String name; } public class Classes { private i ...

  4. Mybatis 中一对多,多对一的配置

    现在有很多电商平台,就拿这个来说吧.顾客跟订单的关系,一个顾客可以有多张订单,但是一个订单只能对应一个顾客. 一对多的顾客 <?xml version="1.0" encod ...

  5. Mybatis【一对多、多对一、多对多】知识要点

    Mybatis[多表连接] 我们在学习Hibernate的时候,如果表涉及到两张的话,那么我们是在映射文件中使用<set>..<many-to-one>等标签将其的映射属性关联 ...

  6. mybatis进行一对多时发现的问题总结

    1.定义一对多xml文件时,所有的resultMap中的column的值一定不要重复,否则mybatis会发生错误,如果有重名,定义别名,column中的名字一定要与查询出的名字一致,如: 52行的别 ...

  7. mybatis之一对多

    今天主要话题围绕这么几个方面? mybatis一对多示例 sql优化策略 一.mybatis之一对多 在说一对多之前,顺便说一下一对一. 一对一,常见的例子,比如以常见的班级例子来说,一个班主任只属于 ...

  8. mybatis 中一对多、多对一、多对多、父子继承关系

    mybatis 中处理一对多.多对一.多对多.父子继承关系的有关键词:association .collection .discriminator id – 一个 ID 结果:标记出作为 ID 的结果 ...

  9. MyBatis:一对多关联查询

    MyBatis从入门到放弃四:一对多关联查询 前言 上篇学习了一对一关联查询,这篇我们学习一对多关联查询.一对多关联查询关键点则依然是配置resultMap,在resultMap中配置collecti ...

  10. mybatis实现一对多连接查询

    问题:两个对象User和Score,它们之间的关系为一对多. 底层数据库为postgresql,ORM框架为mybatis. 关键代码如下: mybatis配置文件如下: mybatis.xml文件内 ...

随机推荐

  1. BitCoin Trading Strategies BackTest With PyAlgoTrade

    Written by Khang Nguyen Vo, khangvo88@gmail.com, for the RobustTechHouse blog. Khang is a graduate f ...

  2. 借助 Django 的 smart_str 和 smart_unicode 进行编码转换(转)

    原文:http://www.dirk.sh/diary/using-django-smart_str-smart_unicode/ Django 为字符编码的转换提供了非常简洁的方法: 1.djang ...

  3. Linux vim编辑器常用命令

    Vim是一个类似于Vi的著名的功能强大.高度可定制的文本编辑器 常用的vim命令如下图 补充: num+命令 对命令执行num次,如  5dd:剪切一行 * 5  即剪切5行,其它如此 /text   ...

  4. 网页中Cache各字段含义

    Pragma 当该字段值为"no-cache"的时候(事实上现在RFC中也仅标明该可选值),会知会客户端不要对该资源读缓存,即每次都得向服务器发一次请求才行. Expires 有了 ...

  5. notepade++使用

    Notepad++也可以实现双视图/双窗口对比显示,目前最新版本(6.32)只能支持双视图显示,而且只能支持左右视图,希望后续版本能得到改进. 我们打开两个需要对比显示的源文件 默认的情况下是分成了两 ...

  6. gbdt调参的小结

    关键部分转自http://www.cnblogs.com/pinard/p/6143927.html 第一次知道网格搜索这个方法,不知道在工业中是不是用这种方式 1.首先从步长和迭代次数入手,选择一个 ...

  7. boost单元测试框架

    头文件: #include <boost/test/unit_test.hpp> 编译加:-lboost_unit_test_framework 单元测试: 需要定义BOOST_TEST_ ...

  8. boost implicit_cast

    在stackoverflow上看到这个帖子, 于是发现了boost::implicit_cast这个小东西. 先来看看这段代码: struct top {}; struct mid_a : top { ...

  9. matplotlib.pyplot 让数据可视化

    1.条形图 import matplotlib.pyplot as plt plt.style.use('ggplot') # 使用ggplot样式来模拟ggplot2风格的图形,ggplot2是一个 ...

  10. Gentoo64无法启动eth0的问题

    Gentoo64在net文件中配置好eth0的静态IP 代码 1.2: /etc/conf.d/net文件的一个示例 # DHCP config_eth0=( "dhcp" ) # ...