zoukankan      html  css  js  c++  java
  • mybatis中resultMap标签的使用规则

    转自:https://blog.csdn.net/jokerzhan/article/details/88085127/

    自定义结果映射规则

    <!-- resultMap自定义某个javabean的封装规则

           type:自定义规则的java类型

           id:唯一id方便引用

         -->

        <resultMap type="entity.Employee" id="getEmpByIdMap">

           <!-- id指定主键列的封装规则

               column:指定哪一列

               property:指定对应的javabean属性

            -->

           <id column="id" property="id"/>

           <!-- result定义普通列封装规则,若属性名与数据库对应表的列名相同可不写,

                mybatis会自动封装,但建议将所有的映射规则都写上

           -->

           <result column="name" property="name"/>

           <result column="sex" property="sex"/>

           <result column="email" property="email"/>

        </resultMap>

        <!-- public Employee getEmpById(Integer id) -->

        <select id="getEmpById" resultMap="getEmpByIdMap">

           select * from employee where id=#{id}

        </select>

    association联合查询

    association可以指定联合的javabean对象

    property="dept":指定哪个属性是联合对象

    javaType:指定这个属性的类型

    <resultMap type="entity.Employee" id="getEmpAndDeptMap">

           <id column="id" property="id"/>

           <result column="empName" property="name"/>

           <result column="sex" property="sex"/>

           <result column="email" property="email"/>

           <!-- association可以指定联合的javabean对象

                property="dept":指定哪个属性是联合对象

                javaType:指定这个属性的类型-->

           <association property="dept" javaType="entity.Department">

               <id column="did" property="id"/>

               <result column="deptName" property="departmentName"/>

           </association>

        </resultMap>

        <!-- public Employee getEmpAndDept(Integer id) -->

        <select id="getEmpAndDept" resultMap="getEmpAndDeptMap">

           select e.id id,e.name empName,e.email email,e.sex sex,e.d_id d_id,

               d.id did,d.name deptName from employee e,dept d

               where e.d_id=d.id and e.id=#{id}

        </select>

    使用association进行分布查询

     1、先按照员工id查询员工信息将会调用查询员工的sql

    2、根据查询员工信息中的d_id值去部门表中查出部门信息

    3、部门设置到员工中

            

    <resultMap type="entity.Employee" id="getEmpAndDeptStepMap">

           <id column="id" property="id"/>

           <result column="name" property="name"/>

           <result column="sex" property="sex"/>

           <result column="email" property="email"/>

           <!-- association定义关联对象的封装规则

                select:表明当前属性是调用select指定的方法查出的结果

                column:指定将那一列的值作为参数传给这个方法

                 流程:使用select指定的方法(传入column指定的这列参数的值)查出对象,

                 并封装给property指定的属性

                -->

                <!-- discriminator鉴别器

                     column:指定判定的列名

                     javaType:列值对应的java类型

                 -->

           <discriminator javaType="string" column="sex">

               <!-- resultType不能缺少 -->

               <case value="" resultType="entity.Employee">

                  <association property="dept" select="dao.DepartmentMapper.getDeptById"

                      column="d_id">

                  </association>

               </case>

           </discriminator>

        </resultMap>

        <!-- public Employee getEmpByIdStep(Integer id) -->

        <select id="getEmpByIdStep" resultMap="getEmpAndDeptStepMap">

           select * from employee where id=#{id}

        </select>

    嵌套结果集的方式,使用collection标签定义关联的集合类型的属性封装规则

    <resultMap type="entity.Department" id="getDeptByIdPlusMap">

           <id column="did" property="id"/>

           <result column="deptName" property="departmentName"/>

           <!-- collection定义关联集合类型的属性的封装规则

                ofType:指定集合里面元素的类型             

            -->

           <collection property="emps" ofType="entity.Employee">

               <!-- 定义这个集合中元素的封装规则 -->

               <id column="eid" property="id"/>

               <result column="empName" property="name"/>

               <result column="sex" property="sex"/>

               <result column="email" property="email"/>

           </collection>

        </resultMap>

        <!-- public Department getDeptByIdPlus(Integer id) -->

        <select id="getDeptByIdPlus" resultMap="getDeptByIdPlusMap">

           select d.id did,d.name deptName,e.id eid,

               e.name empName,e.sex,e.email

               from dept d left join employee e

               on d.id=e.d_id

               where d.id=#{id}

        </select>

    collection分步查询

    <resultMap type="entity.Department" id="getDeptByIdStepMap">

           <id column="id" property="id"/>

           <result column="name" property="departmentName"/>

           <collection property="emps" select="dao.EmployeeMapperPlus.getEmpsByDeptId"

               column="{id}">

          <!-- 或则 column="{deptId=id}"-->

           </collection>

        </resultMap>

       <!-- public List<Employee> getEmpsByDeptId(Integer deptId -->

       <select id="getEmpsByDeptId" resultType="entity.Employee">

           select * from employee where d_id=#{deptId}

        </select>

        <!-- public Department getDeptByIdStep(Integer id) -->

        <select id="getDeptByIdStep" resultMap="getDeptByIdStepMap">

           select * from dept where id=#{id}

        </select>

    当分布查询需要传递多个多个值时,将多个值封装map传递

    colum=“{key1=column1,key2=colum2...}”

  • 相关阅读:
    About_Web
    神奇的 SQL 之性能优化 → 让 SQL 飞起来
    Java实现Kafka生产者和消费者的示例
    Android屏幕绘制一问到底(无代码)
    关于数据库事务和锁的必会知识点,你掌握了多少?
    【Azure Cloud Services】云服务频繁发生服务器崩溃的排查方案
    Choreographer全解析
    气之争,聊聊算法岗位的门户之见!
    资深首席架构师预测:2021年云计算的8个首要趋势
    【并发编程】- 内存模型(针对JSR-133内存模型)篇
  • 原文地址:https://www.cnblogs.com/sharpest/p/13705463.html
Copyright © 2011-2022 走看看