mybaitis 通过Mapping 实现多表查询

1.实体类

 1.1 用于做多表查询的类

public class CustomerCard {
    private Integer id;//主键

    private String cardNumber;

    private Integer customerId;//用户id

    private String customerName;//客户名称

    private String mobile;//手机号

    private Integer status;//状态(1.可用,2.不可用)

    private Integer creatorId;//创建人id

    private String creator;//创建人

    private Date createTime;//创建时间

    private Integer updatorId;//更新人id

    private String updator;//更新人

    private Date updateTime;//更新时间

    private String remark;//备注

    private String tab1;//备用字段1

    private String tab2;//备用字段2
    
    private Customer customer;

    private Company company;
    

1.2 其中一子类

public class Customer {
    private Integer id;//客户表主键

    private String customerCode;//客户编号

    private String customerName;//客户名称

    private Integer cooperateType;//合作类型(1投售人2客户)

    private Integer customerType;//客户类型(1中转站客户2外来客户)

    private String contactName;//联系人

    private String contactPhone;//联系电话

    private Integer companyId;//所属企业id

    private String plateNumber;//车牌号(以#号隔开)

。。。。

1.3 另一个类

 

public class Company {
    private Integer id;//主键

    private String companyCode;//企业编码

    private String companyName;//企业名称

    private Integer companyType;//企业属性

    private String companyArea;//企业地区

    private String companyAddress;//企业地址
。。。。

2. Mapping:

  2.1. resultMap

  <resultMap id="BaseResultMap" type="com.model.CustomerCard" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="card_number" property="cardNumber" jdbcType="VARCHAR" />
    <result column="customer_id" property="customerId" jdbcType="INTEGER" />
    <result column="customer_name" property="customerName" jdbcType="VARCHAR" />
    <result column="mobile" property="mobile" jdbcType="VARCHAR" />
    <result column="status" property="status" jdbcType="INTEGER" />

    <association property="customer" javaType="com.model.Customer">  
        <id column="id" property="id" jdbcType="INTEGER" />
        <result column="customer_code" property="customerCode" jdbcType="VARCHAR" />
        <result column="customer_name" property="customerName" jdbcType="VARCHAR" />
        <result column="cooperate_type" property="cooperateType" jdbcType="INTEGER" />
        <result column="customer_type" property="customerType" jdbcType="INTEGER" />
    </association>
    <association property="company" javaType="com.model.Company">
         <id column="id" property="id" jdbcType="INTEGER" />
         <result column="company_code" property="companyCode" jdbcType="VARCHAR" />
        <result column="company_name" property="companyName" jdbcType="VARCHAR" />
        <result column="company_type" property="companyType" jdbcType="INTEGER" />
        <result column="company_area" property="companyArea" jdbcType="VARCHAR" />
        <result column="company_address" property="companyAddress" jdbcType="VARCHAR" />
        <result column="company_intro" property="companyIntro" jdbcType="VARCHAR" />
        <result column="contact" property="contact" jdbcType="VARCHAR" />
    </association>
  </resultMap>

说明:property 中的名字与类中一定要一样

 2.2 具体执行sql  <select id="selectSelective" resultMap="BaseResultMap" parameterType="com.sortcenter.hjy.model.CustomerCard">
      select 
       c.card_number  ,f.company_name,t.address
      from t_customer_card c LEFT JOIN t_customer t on c.customer_id = t.id 
    LEFT JOIN t_company f on t.company_id = f.id
 

转载于:https://my.oschina.net/kuchawyz/blog/1858072

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值