• Mybatis多表查询


    一、一对一查询

    <mapper namespace="com.finnlee.mapper.OrderMapper">
      <resultMap id="orderMap" type="com.finnlee.pojo.Order">
        <result column="uid" property="user.id"></result>
        <result column="username" property="user.username"></result>
        <result column="password" property="user.password"></result>
        <result column="birthday" property="user.birthday"></result>
      </resultMap>
      <select id="findAll" resultMap="orderMap">
        select * from orders o,user u where o.uid=u.id
      </select>
    </mapper>
    

      

    <resultMap id="orderMap" type="com.finnlee.pojo.Order">
      <result property="id" column="id"></result>
      <result property="ordertime" column="ordertime"></result>
      <result property="total" column="total"></result>
      <!-- property="user" 当前实体属性名 javaType="com.finnlee.pojo.User" 当前实体属性类型 -->   <association property="user" javaType="com.finnlee.pojo.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>

      

    二、一对多|多对多查询

    <?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.finnlee.mapper.MemberMapper">
        <resultMap id="memberMap" type="com.finnlee.pojo.Member">
            <id column="id" property="id"></id>
            <result column="username" property="username" ></result>
            <collection property="orderList" ofType="com.finnlee.pojo.Order" >
                <result column="order_id" property="id"></result>
                <result column="order_sn" property="order_sn"></result>
            </collection>
            <collection property="roleGroup" ofType="com.finnlee.pojo.RoleGroup" >
                <result column="title" property="title"></result>
            </collection>
        </resultMap>
        <select id="findAll" resultMap="memberMap">
            select a.id,a.username ,b.order_sn ,b.id AS order_id,c.title from `member` a LEFT JOIN `order` b ON a.id = b.mid
            LEFT JOIN role_group c ON a.id = c.mid
        </select>
    </mapper>
    

      

  • 相关阅读:
    Docker Dockerfile 定制镜像
    Nginx之URL重写(rewrite)配置
    Jenkins可用环境变量列表以及环境变量的使用(Shell/Command/Maven/Ant)
    vue实现element-ui对话框可拖拽功能
    配置了ssh免密登录还是提示权限不足怎么解决
    一篇文章彻底搞懂base64编码原理
    想不到吧
    async_retrying
    aiojobs
    python字典不区分大小写
  • 原文地址:https://www.cnblogs.com/finnlee/p/16006511.html
Copyright © 2020-2023  润新知