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

posted @ 2019-05-31 13:22  一天到晚想名字  阅读(293)  评论(0编辑  收藏  举报