1. 程式人生 > >Mybatis多表聯合查詢,多對一關聯查詢

Mybatis多表聯合查詢,多對一關聯查詢

有兩張表,一張表為業務賬戶表,一張為資費表,業務賬戶表其中有一個外來鍵為資費型別id,引用了資費表的主鍵id。

建表語句:

/*資費表*/
create  table  sys_charge(
  id int(11) auto_increment,
  name varchar(16) CHARACTER SET utf8 not null,
  type int,
  duration int,
  charge  NUMERIC not null,
  percharge  NUMERIC not null,
  createtime  datetime DEFAULT CURRENT_TIMESTAMP,
  status int DEFAULT 0,
  opentime  datetime,
  primary  key  (id)
);

/*業務帳號表*/
create  table  sys_business(
  id int(11) auto_increment,
  accountid int(11),
  osid varchar(16) not null,
  password  varchar(16) not null,
  realname varchar(16) CHARACTER SET utf8 not null,
  phone varchar(16) not null,
  cardid varchar(18) not null,
  status int DEFAULT 1 ,
  createtime  datetime DEFAULT CURRENT_TIMESTAMP,
  ip varchar(16),
  chargetypeid int(11),
  pausetime varchar(16),
  totaltime varchar(16),
  month varchar(16),
  isdelete int DEFAULT 0,
  deletetime DATETIME,
  primary  key  (id),
  foreign key(accountid) references sys_account(id),
  foreign key(chargetypeid) references sys_charge(id)
);

對應的pojo實體類為:

public class SysCharge {
    private Integer id;

    private String name;

    private Integer type;

    private Integer duration;

    private Long charge;

    private Long percharge;

    private Date createtime;

    private Integer status;

    private Date opentime;

    /*省略getter,setter*/
}
public class SysBusiness {
    private Integer id;

    private Integer accountid;

    private String osid;

    private String password;

    private String realname;

    private String phone;

    private String cardid;

    private Integer status;

    private Date createtime;

    private String ip;

    private Integer chargetypeid;

    private String pausetime;

    private String totaltime;

    private String month;

    private Integer isdelete;

    private Date deletetime;

    private SysCharge sysCharge;

}

因為是多對一,所以要在多的那方寫一個對應到的一的物件,如果是一對多,就要在一那方寫一個對應到多的物件的list集合,存放多個物件。

不過在資料庫中,哪個表裡面有外來鍵,哪個就是多方。一對多和多對一隻是在需要的查詢結果的不同上。比如我要查一個國家和其擁有的人,那就是一對多;如果我要查詢一個人和他對應的國家,那就是多對一。

mapper.xml

<resultMap id="JoinResultMap" type="com.example.demo.pojo.SysBusiness">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="accountid" jdbcType="INTEGER" property="accountid" />
    <result column="osid" jdbcType="VARCHAR" property="osid" />
    <result column="password" jdbcType="VARCHAR" property="password" />
    <result column="realname" jdbcType="VARCHAR" property="realname" />
    <result column="phone" jdbcType="VARCHAR" property="phone" />
    <result column="cardid" jdbcType="VARCHAR" property="cardid" />
    <result column="status" jdbcType="INTEGER" property="status" />
    <result column="createtime" jdbcType="TIMESTAMP" property="createtime" />
    <result column="ip" jdbcType="VARCHAR" property="ip" />
    <result column="chargetypeid" jdbcType="INTEGER" property="chargetypeid" />
    <result column="pausetime" jdbcType="VARCHAR" property="pausetime" />
    <result column="totaltime" jdbcType="VARCHAR" property="totaltime" />
    <result column="month" jdbcType="VARCHAR" property="month" />
    <result column="isdelete" jdbcType="INTEGER" property="isdelete" />
    <result column="deletetime" jdbcType="TIMESTAMP" property="deletetime" />
    <collection property="sysCharge" ofType="com.example.demo.pojo.SysCharge">
      <id column="id" jdbcType="INTEGER" property="id" />
      <result column="name" jdbcType="VARCHAR" property="name" />
      <result column="duration" jdbcType="INTEGER" property="duration" />
      <result column="charge" jdbcType="DECIMAL" property="charge" />
      <result column="percharge" jdbcType="DECIMAL" property="percharge" />
    </collection>
  </resultMap>

  <select id="selectAllBusiness" resultMap="JoinResultMap">
    select sys_business.id,sys_business.accountid,sys_business.osid ,sys_business.password,
    sys_business.realname, sys_business.phone ,sys_business.cardid ,sys_business.status,
    sys_business.createtime ,sys_business.ip, sys_business.chargetypeid, sys_business.pausetime,
    sys_business.totaltime, sys_business.month, sys_business.isdelete, sys_business.deletetime,
    sys_charge.name, sys_charge.duration, sys_charge.charge, sys_charge.percharge
    from sys_business JOIN sys_charge on sys_business.chargetypeid
  </select>

定義的resultmap中的欄位可以不必全寫,需要哪些欄位寫哪些就行。不過select中如果要返回型別為這個resultmap。那select返回的欄位要與resultmap中定義的欄位一樣。

這樣查詢之後,mybatis會將聯合另一張表查詢到的結果封裝到sys_business類中的sys_charge物件屬性中。