zoukankan      html  css  js  c++  java
  • mybatis学习 十三 resultMap标签 一对一

    1 .<resultMap>标签

      写在mapper.xml中,由程序员控制SQL查询结果与实体类的映射关系. 在写<select>标签中,有一个resultType属性,此时select标签中,select语句的返回字段要与resultType属性指定的类的属性名称一致,此时成为自动映射,但是当要接收查询接口的类的属性名称,与select语句的返回字段不一致时,mybatis时无法自动进行映射的,此时需要程序员手动指定映射,此时就用到resultMap标签。

    2. <resultMap>标签的使用

      使用<resultMap>标签时,<select>标签不写 resultType 属性,而是使用 resultMap 属性引用<resultMap>标签. <resultMap>标签中的type属性的值就是select语句返回字段要映射的类

    <resultMap type="teacher" id="mymap">
        <!-- 主键使用 id 标签配置映射关系 -->
        <id column="id" property="id1" />
        <!-- 其他列使用 result 标签配置映射关系 -->
        <result column="name" property="name1"/>
    </resultMap>
    <select id="selAll" resultMap="mymap">
        select * from teacher
    </select>

    3. 使用 resultMap 实现关联单个对象(N+1 方式)

      N+1 查询方式,先查询出某个表的全部信息,根据这个表的信息查询另一个表的信息.在 service 里面写的代码,由 mybatis 完成装配。

    public class Student {
        private int id;
        private String name;
        private int age;
        private int tid;
        private Teacher teacher;
    }
    public class Teacher {
        private int id;
        private String name;
        
        
    }

    studentMapper.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.xxx.mapper.StudentMapper">
              <resultMap type="student" id="stuMap">
                  <id column="id" property="id"/>
                  <result column="name" property="name"/>
                  <result column="age" property="age"/>
                  <result column="tid" property="tid"/>
                  
                  <association property="teacher" column="tid" select="com.xxx.mapper.TeacherMapper.selById" >
                  </association>
              </resultMap>
              <select id="selAll" resultMap="stuMap">
                  select * from student
              </select>
      </mapper>

    TeacherMappe.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.xxx.mapper.TeacherMapper">
              <select id="selById" resultType="teacher" parameterType="int">
                  select * from teacher where id=#{0}
              </select>
      </mapper>

     (1)<association> 装配一个对象时使用

    (2) property: 对象在类中的属性名

    (3)select:通过哪个查询查询出这个对象的信息

    (4) column: 把当前表的哪个列的值做为参数传递给另一个查询

     (5)   大前提使用 N+1 方式.时如果列名和属性名相同可以不配置,使用 Auto mapping 特性.但是 mybatis 默认只会给列专配一次,  即如果把一个列当做另外一个查询的查询条件,别的映射列可以不用配置,但是这个当做参数的列一定要配置。即<result column="tid" property="tid"/>一定要配置,因为

     <association property="teacher" column="tid" select="com.xxx.mapper.TeacherMapper.selById" >
                  </association>中的查询用到了tid作为参数。
    (6)javaType 属性:<association/>专配完后返回一个什么类型的对象.取值是一个类(或类的别名)
  • 相关阅读:
    try_files $uri $uri/ /index.php?$query_string;
    关于declare(strict_types=1)的有效范围
    SVN客户端安装与使用
    Java日志框架中真的需要判断log.isDebugEnabled()吗?
    Spring4自动装配(default-autowire)
    java的@PostConstruct注解
    Google Guava之--cache
    Java类加载机制与Tomcat类加载器架构
    搞懂JVM类加载机制
    Java 类加载机制
  • 原文地址:https://www.cnblogs.com/cplinux/p/9656613.html
Copyright © 2011-2022 走看看