mybatis中的association和collection

简介: mybatis中的association和collection

1 association

<resultMap type="SysUser" id="SysUserResult">
        <id     property="userId"       column="user_id"      />
        <result property="deptId"       column="dept_id"      />
        <result property="userName"     column="user_name"    />
        <result property="nickName"     column="nick_name"    />
        <result property="remark"       column="remark"       />
        <association property="dept"    column="dept_id" javaType="SysDept" resultMap="deptResult" />
    </resultMap>

    <resultMap id="deptResult" type="SysDept">
        <id     property="deptId"   column="dept_id"     />
        <result property="parentId" column="parent_id"   />
        <result property="deptName" column="dept_name"   />
    </resultMap>

2 collection

<resultMap id="PersonResult" type="Person">
        <result property="personId"    column="person_id"    />
        <result property="name"    column="name"    />
        <result property="remark"    column="remark"    />
    </resultMap>

    <resultMap id="PersonAndCarResult" type="Person" extends="PersonResult">
        <collection property="CarList" notNullColumn="person_id" javaType="java.util.List" resultMap="CarResult" />
    </resultMap>

    <resultMap id="CarResult" type="Car">
        <result property="carId"    column="car_id"    />
        <result property="carName"    column="car_name"    />
        <result property="remark"    column="remark"    />
    </resultMap>

3 一起使用

<resultMap type="SysUser" id="SysUserResult">
        <id     property="userId"       column="user_id"      />
        <result property="deptId"       column="dept_id"      />
        <result property="userName"     column="user_name"    />
        <result property="nickName"     column="nick_name"    />
        <result property="email"        column="email"        />
        <result property="phonenumber"  column="phonenumber"  />
        <result property="sex"          column="sex"          />
        <result property="avatar"       column="avatar"       />
        <result property="password"     column="password"     />
        <result property="status"       column="status"       />
        <result property="delFlag"      column="del_flag"     />
        <result property="loginIp"      column="login_ip"     />
        <result property="loginDate"    column="login_date"   />
        <result property="createBy"     column="create_by"    />
        <result property="createTime"   column="create_time"  />
        <result property="updateBy"     column="update_by"    />
        <result property="updateTime"   column="update_time"  />
        <result property="remark"       column="remark"       />
        <association property="dept"    column="dept_id" javaType="SysDept" resultMap="deptResult" />
        <collection  property="roles"   javaType="java.util.List"        resultMap="RoleResult" />
    </resultMap>

    <resultMap id="deptResult" type="SysDept">
        <id     property="deptId"   column="dept_id"     />
        <result property="parentId" column="parent_id"   />
        <result property="deptName" column="dept_name"   />
        <result property="orderNum" column="order_num"   />
        <result property="leader"   column="leader"      />
        <result property="status"   column="dept_status" />
    </resultMap>

    <resultMap id="RoleResult" type="SysRole">
        <id     property="roleId"       column="role_id"        />
        <result property="roleName"     column="role_name"      />
        <result property="roleKey"      column="role_key"       />
        <result property="roleSort"     column="role_sort"      />
        <result property="dataScope"     column="data_scope"    />
        <result property="status"       column="role_status"    />
    </resultMap>
目录
相关文章
|
Java 数据库连接 mybatis
【Mybatis用法】Mybatis框架中一对一,一对多association和collection的使用举例方法
【Mybatis用法】Mybatis框架中一对一,一对多association和collection的使用举例方法
526 0
|
Java 数据库连接 mybatis
MyBatis中对象映射关联之association使用实践
MyBatis中对象映射关联之association使用实践
959 1
mybatis复习04高级查询 一对多,多对一的映射处理,collection和association标签的使用
文章介绍了MyBatis中高级查询的一对多和多对一映射处理,包括创建数据库表、抽象对应的实体类、使用resultMap中的association和collection标签进行映射处理,以及如何实现级联查询和分步查询。此外,还补充了延迟加载的设置和用法。
mybatis复习04高级查询 一对多,多对一的映射处理,collection和association标签的使用
|
SQL Java 数据库连接
mybatis 中 foreach collection的常用用法
mybatis 中 foreach collection的常用用法
609 1
|
XML Java 数据库连接
mybatis报错:java.lang.IllegalArgumentException: Mapped Statements collection does not contain
mybatis报错:java.lang.IllegalArgumentException: Mapped Statements collection does not contain
|
SQL XML Java
mybatis元素类型为 "resultMap" 的内容必须匹配 "(constructor?,id *,result*,association报错解决
mybatis元素类型为 "resultMap" 的内容必须匹配 "(constructor?,id *,result*,association报错解决
812 0
|
Java 数据库连接 mybatis
一文彻底搞懂Mybatis系列(十二)之MyBatis多对一映射延迟加载(association和lazyLoadingEnabled)
一文彻底搞懂Mybatis系列(十二)之MyBatis多对一映射延迟加载(association和lazyLoadingEnabled)
350 0
|
SQL IDE Java
MyBatis【问题 01】mapper传入array\collection\list类型的参数时报BindingException:Parameter ‘xx‘ not found问题复现及解决
MyBatis【问题 01】mapper传入array\collection\list类型的参数时报BindingException:Parameter ‘xx‘ not found问题复现及解决
819 0
|
SQL Java 数据库连接
Mybatis使用collection标签实现一对多关联查询,返回结果集list中嵌套list
Mybatis使用collection标签实现一对多关联查询,返回结果集list中嵌套list
1404 0