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对象属性中。


猜你喜欢

转载自blog.csdn.net/wanderlustlee/article/details/81006716