zoukankan      html  css  js  c++  java
  • mybatis多表查询

    方式1:

    创建结果对象,mapper接口和mapper.xml文件等

    #pojo
    package com.e3mall.cms.pojo;
    import lombok.Data;
    //item+item_cat
    @Data
    public class demo1 {
        private Long id;
        private String title;
        private String sellPoint;
        private Long price;
        private String catName;
    }
    
    #mapper接口
    @Component
    public interface Demo1Mapper {
        List<demo1> getDemoList();
    }
    
    #mapper.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.e3mall.cms.dao.mapper.Demo1Mapper" >
        <resultMap id="BaseResultMap" type="com.e3mall.cms.pojo.demo1" >
            <id column="id" property="id" jdbcType="BIGINT" />
            <result column="title" property="title" jdbcType="VARCHAR" />
            <result column="sell_point" property="sellPoint" jdbcType="VARCHAR" />
            <result column="price" property="price" jdbcType="BIGINT" />
            <result column="catName" property="catName" jdbcType="VARCHAR" />
        </resultMap>
        <select id="getDemoList" resultMap="BaseResultMap" >
            SELECT a.id , a.title , a.sell_point , a.price , b.name catName FROM tb_item a , tb_item_cat b where a.cid = b.id AND a.id = 816448
        </select>
    </mapper>
    View Code

     方式2:

    与方式1类似,也要自己创建结果对象,但是不需要创建mapper接口和mapper.xml文件,而是在原来的其中一个mapper中添加查询sql

    #mapper.xml
    #添加一个新的resultMap,注意id不能重复
    <resultMap id="Demo1ResultMap" type="com.e3mall.cms.pojo.demo1" >
        <id column="id" property="id" jdbcType="BIGINT" />
        <result column="title" property="title" jdbcType="VARCHAR" />
        <result column="sell_point" property="sellPoint" jdbcType="VARCHAR" />
        <result column="price" property="price" jdbcType="BIGINT" />
        <result column="catName" property="catName" jdbcType="VARCHAR" />
      </resultMap>
    
    #添加对应的查询语句
     <select id="getDemoList" resultMap="Demo1ResultMap" >
        SELECT a.id , a.title , a.sell_point , a.price , b.name catName FROM tb_item a , tb_item_cat b where a.cid = b.id AND a.id &lt; 635906
      </select>
    View Code

     方式3:

    将有关联的pojo中,添加外键对象,这里有个不错的博客,就不实验了

    博客:https://cloud.tencent.com/developer/article/1120380

  • 相关阅读:
    Angular2 组件通信
    vue跨组件通信的几种方法
    Angular React 和 Vue的比较
    vue对比其他框架
    ReactJS 生命周期、数据流与事件
    LeetCode 1089. 复写零(Duplicate Zeros) 72
    LeetCode 421. 数组中两个数的最大异或值(Maximum XOR of Two Numbers in an Array) 71
    12
    11
    10
  • 原文地址:https://www.cnblogs.com/tianphone/p/10949865.html
Copyright © 2011-2022 走看看