mybatis多表查询(xml)

多表查询都用resultMap

resultMap

说白了就是他可以手动设置映射参数,例如

可以指定

column代表数据库的参数 property 代表实体类的参数

java 复制代码
<id column="roleid" property="id"></id> column代表数据库的参数 property 代表实体类的参数

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

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

一对多查询

一个订单可以被多个用户拥有

复制代码
    <resultMap id="orderResultMap" type="order">  第一个参数 定义标识符 第二个参数 要填写的实体类
XML 复制代码
        <id column="oid" property="id"></id>           
        <result column="ordertime" property="ordertime"></result>
        <result property="total" column="total"></result>
<!--        <result property="user.id" column="uid"></result>-->
<!--        <result property="user.username" column="username"></result>-->
<!--        <result property="user.password" column="password"></result>-->
<!--        <result property="user.birthday" column="birthday"></result>-->
        设置专门的user对象
        <association property="user" javaType="user">   这个标签专门用于对象
            <result column="uid" property="id"></result>
            <result column="username" property="username"></result>
            <result column="password" property="password"></result>
            <result column="birthday" property="birthday"></result>
        </association>
    </resultMap>
​
    <select id="findAll"  resultMap="orderResultMap">
        select * ,o.id oid from user u,orders o where u.id =o.uid;
    </select>
XML 复制代码
  <resultMap id="userAndRoleMap" type="user">
        <id column="userid" property="id"></id>
        <result property="birthday" column="birthday"></result>
        <result property="username" column="username"></result>
         <result property="password" column="password"></result>
        <collection property="listRole" ofType="role"> 还有一种是专门用于集合
            <id column="roleid" property="id"></id>
            <result property="roleDesc" column="roleDesc"></result>
            <result property="rolename" column="rolename"></result>
        </collection>
    </resultMap>
相关推荐
叫我詹躲躲7 分钟前
救命!MySQL 误删数据找不回?老运维私藏的备份技巧,免费给
运维·数据库
剑小麟12 分钟前
maven中properties和dependencys标签的区别
java·前端·maven
Chief_fly13 分钟前
Logback 配置精细化包日志控制
java·logback
i源15 分钟前
IDEA好用的插件
java·intellij-idea
_大学牲20 分钟前
Flutter 之魂 Dio🔥:四两拨千斤的网络库
前端·数据库·flutter
方二华21 分钟前
7 mysql对order by group by join limit count的实现
数据库·mysql
程序定小飞22 分钟前
基于springboot的论坛网站设计与实现
java·开发语言·spring boot·后端·spring
SelectDB22 分钟前
更高效的数据处理解决方案:基于 MinIO 部署 Apache Doris 存算分离版本实践
数据库·数据分析·apache
ZHE|张恒1 小时前
Java 泛型详解:类型参数的力量
java
寒月霜华1 小时前
JavaWeb后端-MySQL
数据库·mysql