Java mybatis一对一和一对多嵌套查询

问题遇到的现象和发生背景

java: 需要class, interface或enum

问题相关代码,请勿粘贴截图

1.Bill.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 属性的值为对应 mapper的全限定类名:路径+类的名称。通过该方法关联接口和XML文件-->
<mapper namespace="mapper.BillMapper">
    <!--一对一-->
    <resultMap id="mybill" type="bill" autoMapping="true">
        <id property="id" column="id"/>
        <result property="billCode" column="billCode"/>

        <association property="provider" javaType="provider" autoMapping="true"
                     column="pid"
                     select="ProviderMapper.findProviderById">
            <id property="id" column="pid"/>
            <result property="proname" column="proname"/>
        </association>
    </resultMap>

    <select id="findBillByIdWithProvider" resultMap="mybill">
        select * from smbms_bill where id = #{id}
    </select>

    <!--一对多-->
    <select id="findBillByPid" resultType="bill">
        select * from smbms_bill where pid = #{pid}
    </select>

    <resultMap id="myprovider" type="provider" autoMapping="true">
        <id property="id" column="id"/>

        <collection property="list" ofType="bill" autoMapping="true"
                    column="id"
                    select="BillMapper.findBillByPid">
            <id property="id" column="oid"/>
        </collection>
    </resultMap>

    <select id="findProviderByIdWithBill" resultMap="myprovider">
        select * from smbms_bill where provider.id = #{pid}
    </select>

</mapper>

2.BillMapper

package mapper;


import pojo.smbms_bill;
import pojo.smbms_provider;

import java.util.List;

public interface BillMapper {
    //一对一关系
    smbms_bill findBillByIdWithProvider(int id);

    //一对多关系
    smbms_provider findProviderByIdWithBill(int pid);
    List<smbms_bill> findBillByPid(int pid);

}

3.Provider.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 属性的值为对应 mapper的全限定类名:路径+类的名称。通过该方法关联接口和XML文件-->

<mapper namespace="mapper.ProviderMapper">

      <select id="findProviderById" resultType="user">
          select * from smbms_provider where id = #{id}
      </select>

</mapper>

4.ProviderMapper

package mapper;

import pojo.smbms_provider;

public interface ProviderMapper {

    smbms_provider findProviderById(int id);
}

5.smbms_bill实体类

package pojo;

import java.util.Date;
//订单
public class smbms_bill {
    private int id;
    private String billCode;
    private String productName;
    private String productDesc;
    private String productUnit;
    private int productCount;
    private int totalPrice;
    private int isPayment;
    private int createdBy;
    private Date creationDate;
    private  int modifyBy;
    private Date modifyDate;

    private  int providerId; //外键

    private smbms_provider provider; //订单对应供应商

    public smbms_bill() {
    }

    public smbms_bill(int id, String billCode, String productName, String productDesc, String productUnit, int productCount, int totalPrice, int isPayment, int createdBy, Date creationDate, int modifyBy, Date modifyDate, int providerId, smbms_provider provider) {
        this.id = id;
        this.billCode = billCode;
        this.productName = productName;
        this.productDesc = productDesc;
        this.productUnit = productUnit;
        this.productCount = productCount;
        this.totalPrice = totalPrice;
        this.isPayment = isPayment;
        this.createdBy = createdBy;
        this.creationDate = creationDate;
        this.modifyBy = modifyBy;
        this.modifyDate = modifyDate;
        this.providerId = providerId;
        this.provider = provider;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getBillCode() {
        return billCode;
    }

    public void setBillCode(String billCode) {
        this.billCode = billCode;
    }

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public String getProductDesc() {
        return productDesc;
    }

    public void setProductDesc(String productDesc) {
        this.productDesc = productDesc;
    }

    public String getProductUnit() {
        return productUnit;
    }

    public void setProductUnit(String productUnit) {
        this.productUnit = productUnit;
    }

    public int getProductCount() {
        return productCount;
    }

    public void setProductCount(int productCount) {
        this.productCount = productCount;
    }

    public int getTotalPrice() {
        return totalPrice;
    }

    public void setTotalPrice(int totalPrice) {
        this.totalPrice = totalPrice;
    }

    public int getIsPayment() {
        return isPayment;
    }

    public void setIsPayment(int isPayment) {
        this.isPayment = isPayment;
    }

    public int getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(int createdBy) {
        this.createdBy = createdBy;
    }

    public Date getCreationDate() {
        return creationDate;
    }

    public void setCreationDate(Date creationDate) {
        this.creationDate = creationDate;
    }

    public int getModifyBy() {
        return modifyBy;
    }

    public void setModifyBy(int modifyBy) {
        this.modifyBy = modifyBy;
    }

    public Date getModifyDate() {
        return modifyDate;
    }

    public void setModifyDate(Date modifyDate) {
        this.modifyDate = modifyDate;
    }

    public int getProviderId() {
        return providerId;
    }

    public void setProviderId(int providerId) {
        this.providerId = providerId;
    }

    public smbms_provider getProvider() {
        return provider;
    }

    public void setProvider(smbms_provider provider) {
        this.provider = provider;
    }

    @Override
    public String toString() {
        return "smbms_bill{" +
                "id=" + id +
                ", billCode='" + billCode + '\'' +
                ", productName='" + productName + '\'' +
                ", productDesc='" + productDesc + '\'' +
                ", productUnit='" + productUnit + '\'' +
                ", productCount=" + productCount +
                ", totalPrice=" + totalPrice +
                ", isPayment=" + isPayment +
                ", createdBy=" + createdBy +
                ", creationDate=" + creationDate +
                ", modifyBy=" + modifyBy +
                ", modifyDate=" + modifyDate +
                ", providerId=" + providerId +
                ", provider=" + provider +
                '}';
    }

6.smbms_provider实体类

package pojo;

import java.util.Date;
import java.util.List;
//供应商
public class smbms_provider {
    private int id;
    private String proCode;
    private String proName;
    private String proDesc;
    private String proContact;
    private String proPhone;
    private String proAddress;
    private String proFax;
    private int createdBy;
    private Date creationDate;
    private Date modifyDate;
    private  int modifyBy;
    //地址信息,一对多的关系,多就需要用List接收,该属性在数据库表中并没有
    private List<smbms_bill> billList;

    public smbms_provider() {
    }

    public smbms_provider(int id, String proCode, String proName, String proDesc, String proContact, String proPhone, String proAddress, String proFax, int createdBy, Date creationDate, Date modifyDate, int modifyBy, List<smbms_bill> billList) {
        this.id = id;
        this.proCode = proCode;
        this.proName = proName;
        this.proDesc = proDesc;
        this.proContact = proContact;
        this.proPhone = proPhone;
        this.proAddress = proAddress;
        this.proFax = proFax;
        this.createdBy = createdBy;
        this.creationDate = creationDate;
        this.modifyDate = modifyDate;
        this.modifyBy = modifyBy;
        this.billList = billList;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getProCode() {
        return proCode;
    }

    public void setProCode(String proCode) {
        this.proCode = proCode;
    }

    public String getProName() {
        return proName;
    }

    public void setProName(String proName) {
        this.proName = proName;
    }

    public String getProDesc() {
        return proDesc;
    }

    public void setProDesc(String proDesc) {
        this.proDesc = proDesc;
    }

    public String getProContact() {
        return proContact;
    }

    public void setProContact(String proContact) {
        this.proContact = proContact;
    }

    public String getProPhone() {
        return proPhone;
    }

    public void setProPhone(String proPhone) {
        this.proPhone = proPhone;
    }

    public String getProAddress() {
        return proAddress;
    }

    public void setProAddress(String proAddress) {
        this.proAddress = proAddress;
    }

    public String getProFax() {
        return proFax;
    }

    public void setProFax(String proFax) {
        this.proFax = proFax;
    }

    public int getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(int createdBy) {
        this.createdBy = createdBy;
    }

    public Date getCreationDate() {
        return creationDate;
    }

    public void setCreationDate(Date creationDate) {
        this.creationDate = creationDate;
    }

    public Date getModifyDate() {
        return modifyDate;
    }

    public void setModifyDate(Date modifyDate) {
        this.modifyDate = modifyDate;
    }

    public int getModifyBy() {
        return modifyBy;
    }

    public void setModifyBy(int modifyBy) {
        this.modifyBy = modifyBy;
    }

    public List<smbms_bill> getBillList() {
        return billList;
    }

    public void setBillList(List<smbms_bill> billList) {
        this.billList = billList;
    }

    @Override
    public String toString() {
        return "smbms_provider{" +
                "id=" + id +
                ", proCode='" + proCode + '\'' +
                ", proName='" + proName + '\'' +
                ", proDesc='" + proDesc + '\'' +
                ", proContact='" + proContact + '\'' +
                ", proPhone='" + proPhone + '\'' +
                ", proAddress='" + proAddress + '\'' +
                ", proFax='" + proFax + '\'' +
                ", createdBy=" + createdBy +
                ", creationDate=" + creationDate +
                ", modifyDate=" + modifyDate +
                ", modifyBy=" + modifyBy +
                ", billList=" + billList +
                '}';
    }
}

7.测试

 System.out.println("+++++++++(一对一)++++++++++++");
            //一对一和一对多的级联查询(一个供应商提供多个订单,一个订单对应一个供应商)
            //嵌套查询的方式
            BillMapper mapper = sqlSession.getMapper(BillMapper.class);
            smbms_bill bill = mapper.findBillByIdWithProvider(1);
            System.out.println(bill);

            System.out.println("+++++++++(一对多)++++++++++++");
            mapper = sqlSession.getMapper(BillMapper.class);
            smbms_provider provider = mapper.findProviderByIdWithBill(1);
            System.out.println(provider);

8.数据库建表

create table smbms_bill(
    id bigint(20),
    billCode varchar(20),
    productName varchar(20),
    productDesc varchar(50), 
    productUnit varchar(10),
    productCount decimal(20,2),
    totalPrice decimal(20,2),
    isPayment int(10),
    createdBy bigint(20),
    creationDate datetime,
    modifyBy bigint(20),
    modifyDate datetime,
    providerId bigint(20)   
);
 id bigint(20),
    proCode varchar(20),
    proName varchar(20),
    proDesc varchar(50), 
    proContact varchar(20),
    proPhone varchar(20),
    proAddress varchar(50),
    proFax varchar(20),
    createdBy bigint(20),
    creationDate datetime,
    modifyDate datetime, 
    modifyBy bigint(20)
运行结果及报错内容

img

我的解答思路和尝试过的方法

可能是代码不对应

我想要达到的结果

可以正常查询

img

img


这些类型如果没有设置别名的话,需要写类的全类名(带包名的)