第一节:一对一关系实现

需要实现一对一的关系,首先我们有两张表,t-addree和t_student。

  1. CREATE TABLE `t_address` (
  2. `id` int(11) NOT NULL AUTO_INCREMENT,
  3. `sheng` varchar(20) DEFAULT NULL,
  4. `shi` varchar(20) DEFAULT NULL,
  5. `qu` varchar(20) DEFAULT NULL,
  6. PRIMARY KEY (`id`)
  7. ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
  1. insert into `t_address`(`id`,`sheng`,`shi`,`qu`) values (1,'江苏省','苏州市','姑苏区'),(2,'江苏省','南京市','鼓楼区');
  1. CREATE TABLE `t_student` (
  2. `id` int(11) NOT NULL AUTO_INCREMENT,
  3. `name` varchar(20) DEFAULT NULL,
  4. `age` int(11) DEFAULT NULL,
  5. `addressId` int(11) DEFAULT NULL,
  6. PRIMARY KEY (`id`)
  7. ) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;
  1. insert into `t_student`(`id`,`name`,`age`,`addressId`) values (1,'张三',10,1),(2,'李四',11,2),(3,'李四',11,2),(4,'李四',11,2),(5,'李四',11,2),(6,'李四',11,2),(7,'李四',11,2);

然后写model层

Address.java

  1. package com.javaxk.model;
  2.  
  3. public class Address {
  4.  
  5. private Integer id;
  6. private String sheng;
  7. private String shi;
  8. private String qu;
  9.  
  10. public Integer getId() {
  11. return id;
  12. }
  13. public void setId(Integer id) {
  14. this.id = id;
  15. }
  16. public String getSheng() {
  17. return sheng;
  18. }
  19. public void setSheng(String sheng) {
  20. this.sheng = sheng;
  21. }
  22. public String getShi() {
  23. return shi;
  24. }
  25. public void setShi(String shi) {
  26. this.shi = shi;
  27. }
  28. public String getQu() {
  29. return qu;
  30. }
  31. public void setQu(String qu) {
  32. this.qu = qu;
  33. }
  34. @Override
  35. public String toString() {
  36. return "Address [id=" + id + ", sheng=" + sheng + ", shi=" + shi
  37. + ", qu=" + qu + "]";
  38. }
  39.  
  40. }

Student.java

  1. package com.javaxk.model;
  2.  
  3. public class Student {
  4.  
  5. private Integer id;
  6. private String name;
  7. private Integer age;
  8. private Address address;
  9.  
  10. public Student() {
  11. super();
  12. // TODO Auto-generated constructor stub
  13. }
  14.  
  15. public Student(Integer id, String name, Integer age) {
  16. super();
  17. this.id = id;
  18. this.name = name;
  19. this.age = age;
  20. }
  21.  
  22. public Student(String name, Integer age) {
  23. super();
  24. this.name = name;
  25. this.age = age;
  26. }
  27.  
  28. public Integer getId() {
  29. return id;
  30. }
  31. public void setId(Integer id) {
  32. this.id = id;
  33. }
  34. public String getName() {
  35. return name;
  36. }
  37. public void setName(String name) {
  38. this.name = name;
  39. }
  40. public Integer getAge() {
  41. return age;
  42. }
  43. public void setAge(Integer age) {
  44. this.age = age;
  45. }
  46. public Address getAddress() {
  47. return address;
  48. }
  49.  
  50. public void setAddress(Address address) {
  51. this.address = address;
  52. }
  53.  
  54. @Override
  55. public String toString() {
  56. return "Student [id=" + id + ", name=" + name + ", age=" + age
  57. + ", address=" + address + "]";
  58. }
  59.  
  60. }

mappers映射类

AddressMapper.java

  1. package com.javaxk.mappers;
  2.  
  3. import com.javaxk.model.Address;
  4.  
  5. public interface AddressMapper {
  6.  
  7. public Address findById(Integer id);
  8.  
  9. }

StudentMapper.java

  1. package com.javaxk.mappers;
  2.  
  3. import java.util.List;
  4.  
  5. import com.javaxk.model.Student;
  6.  
  7. public interface StudentMapper {
  8.  
  9. public Student findStudentWithAddress(Integer id);
  10. }

主程序运行类

StudentTest3.java

  1. package com.javaxk.service;
  2.  
  3. import java.util.List;
  4.  
  5. import org.apache.ibatis.session.SqlSession;
  6. import org.apache.log4j.Logger;
  7. import org.junit.After;
  8. import org.junit.Before;
  9. import org.junit.Test;
  10.  
  11. import com.javaxk.mappers.StudentMapper;
  12. import com.javaxk.model.Student;
  13. import com.javaxk.util.SqlSessionFactoryUtil;
  14.  
  15. public class StudentTest3 {
  16.  
  17. private static Logger logger=Logger.getLogger(StudentTest.class);
  18. private SqlSession sqlSession=null;
  19. private StudentMapper studentMapper=null;
  20.  
  21. /**
  22. * 测试方法前调用
  23. * @throws Exception
  24. */
  25. @Before
  26. public void setUp() throws Exception {
  27. sqlSession=SqlSessionFactoryUtil.openSession();
  28. studentMapper=sqlSession.getMapper(StudentMapper.class);
  29. }
  30.  
  31. /**
  32. * 测试方法后调用
  33. * @throws Exception
  34. */
  35. @After
  36. public void tearDown() throws Exception {
  37. sqlSession.close();
  38. }
  39.  
  40. @Test
  41. public void testFindStudentWithAddress() {
  42. logger.info("查询学生(带地址)");
  43. Student student=studentMapper.findStudentWithAddress(1);
  44. System.out.println(student);
  45. }
  46.  
  47. }

Mapper映射文件的不同写法。。。

AddressMapper.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.javaxk.mappers.AddressMapper">
  6.  
  7. <resultMap type="Address" id="AddressResult">
  8. <result property="id" column="id"/>
  9. <result property="sheng" column="sheng"/>
  10. <result property="shi" column="shi"/>
  11. <result property="qu" column="qu"/>
  12. </resultMap>
  13.  
  14. <select id="findById" parameterType="Integer" resultType="Address">
  15. select * from t_address where id=#{id}
  16. </select>
  17.  
  18. </mapper>

第一种方式:

  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.javaxk.mappers.StudentMapper">
  6.  
  7. <resultMap type="Student" id="StudentResult">
  8. <id property="id" column="id"/>
  9. <result property="name" column="name"/>
  10. <result property="age" column="age"/>
  11.  
  12. <result property="address.id" column="addressId"/>
  13. <result property="address.sheng" column="sheng"/>
  14. <result property="address.shi" column="shi"/>
  15. <result property="address.qu" column="qu"/>
  16. </resultMap>
  17. <select id="findStudentWithAddress" resultMap="StudentResult" parameterType="Integer">
  18. select * from t_student t1,t_address t2 where t1.addressId=t2.id and t1.id=#{id}
  19. </select>
  20. </mapper>

第二种方式:

  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.javaxk.mappers.StudentMapper">
  6. <resultMap type="Address" id="AddressResult">
  7. <result property="id" column="id"/>
  8. <result property="sheng" column="sheng"/>
  9. <result property="shi" column="shi"/>
  10. <result property="qu" column="qu"/>
  11. </resultMap>
  12.  
  13. <resultMap type="Student" id="StudentResult">
  14. <id property="id" column="id"/>
  15. <result property="name" column="name"/>
  16. <result property="age" column="age"/>
  17. <association property="address" resultMap="AddressResult"/>
  18. </resultMap>
  19.  
  20. <select id="findStudentWithAddress" resultMap="StudentResult" parameterType="Integer">
  21. select * from t_student t1,t_address t2 where t1.addressId=t2.id and t1.id=#{id}
  22. </select>
  23. </mapper>

第三种方式:

  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.javaxk.mappers.StudentMapper">
  6. <resultMap type="Student" id="StudentResult">
  7. <id property="id" column="id"/>
  8. <result property="name" column="name"/>
  9. <result property="age" column="age"/>
  10. <association property="address" javaType="Address">
  11. <result property="id" column="id"/>
  12. <result property="sheng" column="sheng"/>
  13. <result property="shi" column="shi"/>
  14. <result property="qu" column="qu"/>
  15. </association>
  16. </resultMap>
  17.  
  18. <select id="findStudentWithAddress" resultMap="StudentResult" parameterType="Integer">
  19. select * from t_student t1,t_address t2 where t1.addressId=t2.id and t1.id=#{id}
  20. </select>
  21.  
  22. </mapper>

第四种方式:

  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.javaxk.mappers.StudentMapper">
  6. <resultMap type="Student" id="StudentResult">
  7. <id property="id" column="id"/>
  8. <result property="name" column="name"/>
  9. <result property="age" column="age"/>
  10. <association property="address" column="id" select="com.javaxk.mappers.AddressMapper.findById"></association>
  11. </resultMap>
  12.  
  13. <select id="findStudentWithAddress" resultMap="StudentResult" parameterType="Integer">
  14. select * from t_student t1,t_address t2 where t1.addressId=t2.id and t1.id=#{id}
  15. </select>
  16.  
  17. </mapper>

第二节:一对多关系实现

StudentMapper.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.javaxk.mappers.StudentMapper">
  6.  
  7. <resultMap type="Student" id="StudentResult">
  8. <id property="id" column="id"/>
  9. <result property="name" column="name"/>
  10. <result property="age" column="age"/>
  11. <association property="address" column="addressId" select="com.javaxk.mappers.AddressMapper.findById"></association>
  12. <association property="grade" column="gradeId" select="com.javaxk.mappers.GradeMapper.findById"></association>
  13. </resultMap>
  14.  
  15. <select id="findStudentWithAddress" resultMap="StudentResult" parameterType="Integer">
  16. select * from t_student t1,t_address t2 where t1.addressId=t2.id and t1.id=#{id}
  17. </select>
  18.  
  19. </mapper>

AddressMapper.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.javaxk.mappers.AddressMapper">
  6.  
  7. <resultMap type="Address" id="AddressResult">
  8. <result property="id" column="id"/>
  9. <result property="sheng" column="sheng"/>
  10. <result property="shi" column="shi"/>
  11. <result property="qu" column="qu"/>
  12. </resultMap>
  13.  
  14. <select id="findById" parameterType="Integer" resultType="Address">
  15. select * from t_address where id=#{id}
  16. </select>
  17.  
  18. </mapper>

GradeMapper.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.javaxk.mappers.GradeMapper">
  6.  
  7. <resultMap type="Grade" id="GradeResult">
  8. <result property="id" column="id"/>
  9. <result property="gradeName" column="gradeName"/>
  10.  
  11. </resultMap>
  12.  
  13. <select id="findById" parameterType="Integer" resultType="Grade">
  14. select * from t_grade where id=#{id}
  15. </select>
  16.  
  17. </mapper>

测试主类:

  1. @Test
  2. public void testFindStudentWithGrade(){
  3. logger.info("查询学生(带年级)");
  4. Student student=studentMapper.findStudentWithAddress(1);
  5. System.out.println(student);
  6. }

(四)MyBatis关系映射的更多相关文章

  1. MyBatis 关系映射XML配置

    关系映射 在我看来这些实体类就没啥太大关联关系,不就是一个sql语句解决的问题,直接多表查询就完事,程序将它设置关联就好 xml里面配置也是配置了sql语句,下面给出几个关系的小毛驴(xml) 一对多 ...

  2. hibernate学习四(关系映射一对一与组件映射)

    一.关系映射简介 在数据库中,表与表的关系,仅有外键.但使用hibernate后,为面向对象的编程,对象与对象的关系多样化:如 一对一,一对多,多对多,并具有单向和双向之分. 开始练习前,复制上一次项 ...

  3. Hibernate学习笔记(四)关系映射之一对一关联映射

    一. 一对一关联映射 ²        两个对象之间是一对一的关系,如Person-IdCard(人—身份证号) ²        有两种策略可以实现一对一的关联映射 Ø        主键关联:即让 ...

  4. Mybatis关系映射

    一.一对一关系映射 使用resultType+包装类实现 1.假设问题背景是要求在某一个购物平台的后台程序中添加一个这样的功能:查询某个订单的信息和下该订单的用户信息.首先我们可以知道,一般这样的平台 ...

  5. Hibernate基础学习(四)—对象-关系映射(上)

    一.映射对象标识符      Java语言按内存地址来识别或区分同一个类的不同对象,而关系数据库按主键值来识别或区分同一个表的不同记录.Hibernate使用对象标识符(OID)来建立内存中的对象和数 ...

  6. mybatis 关系映射

    一:订单商品数据模型 1.数据库执行脚本 创建数据库表代码: 1 CREATE TABLE items ( 2 id INT NOT NULL AUTO_INCREMENT, 3 itemsname ...

  7. Spring+SpringMVC+MyBatis深入学习及搭建(四)——MyBatis输入映射与输出映射

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6878529.html 前面有讲到Spring+SpringMVC+MyBatis深入学习及搭建(三)——My ...

  8. mybatis关系映射之一对多和多对一

    本实例使用用户(User)和博客(Post)的例子做说明: 一个用户可以有多个博客, 一个博客只对应一个用户 一. 例子(本实体采用maven构建): 1. 代码结构图: 2. 数据库: t_user ...

  9. Java基础-SSM之mybatis一对多和多对一关系映射

    Java基础-SSM之mybatis一对多和多对一关系映射 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.准备测试环境(创建数据库表)  1>.创建customers表: ...

随机推荐

  1. LGP4588[JSOI2018]扫地机器人

    题解 需要先说明一点东西: 1 同一副对角线方向相同,共有$gcd(n,m)$条不同的副对角线,机器人的行为是一个$gcd(n,m)$的循环:: 如果左上方是$(1,1)$,容易看出所有的路径是从左或 ...

  2. 【arc073D】Many Moves

    Portal -->arc073D Description ​ 有\(n\)个格子,编号从左到右为\(1\sim n\),一开始有两个棋子,位置给定,接下来要依次进行\(Q\)次操作,第\(i\ ...

  3. python学习(24) 使用Xpath解析并抓取美女图片

    Xpath最初用来处理XML解析,同样适用于HTML文档处理.相比正则表达式更方便一些 Xpath基本规则 nodename 表示选取nodename 节点的所有子节点 / 表示当前节点的直接子节点 ...

  4. R语言:克里金插值

    基于空间自相关,R语言克里金插值 library(gstat) Warning message: In scan(file = file, what = what, sep = sep, quote ...

  5. PHP判断变量类型和类型转换的三种方式

    前言: PHP 在变量定义中不需要(不支持)明确的类型定义.变量类型是根据使用该变量的上下文所决定的.所以,在面对页码跳转.数值计算等严格的格式需求时,就要对变量进行类型转换. 举例如下: $foo ...

  6. [洛谷P4491] [HAOI2018]染色

    洛谷题目链接:[HAOI2018]染色 题目背景 HAOI2018 Round2 第二题 题目描述 为了报答小 C 的苹果, 小 G 打算送给热爱美术的小 C 一块画布, 这块画布可 以抽象为一个长度 ...

  7. nginx client ip配置

    server { listen 80; server_name localhost; location /{ root html; index index.html index.htm; proxy_ ...

  8. .Net多线程之线程安全

    ConcurrentDictionary是.net4.0推出的一套线程安全集合里的其中一个,和它一起被发行的还有ConcurrentStack,ConcurrentQueue等类型,它们的单线程版本( ...

  9. Java开发者应该列入年度计划的5件事

    本文写了我今年计划要做的5件事.为了能跟踪计划执行的进度,就把这些事都列了出来.我觉得这些事对其它Java开发者而言也是不错的参考方向. 1.开发一个应用,通过Java来操作一种NoSQL数据库实现存 ...

  10. IIS 无法显示网页问题

    今天服务器上的项目突然无法访问,之前也碰到过,都是重启服务器解决的,因为重启IIS无效,另外检查发现w3wp.exe进程正常,其他端口及相关的都没什么问题,最后无奈只能想到用重启来解决了,今天又出现这 ...