首页 行业资讯 宠物日常 宠物养护 宠物健康 宠物故事

mybatis select时什么时候需要写多表联合查询

发布网友

我来回答

1个回答

热心网友

下面是 User 和 Role 的实体类代码:

User

[java] view plain copy
<span style="font-family:Comic Sans MS;font-size:12px;">package com.sica.domain;

import java.io.Serializable;
import java.util.List;

public class User implements Serializable {
private String id;

private String username;

private String password;

private List<Role> roles;

private static final long serialVersionUID = 1L;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id == null ? null : id.trim();
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username == null ? null : username.trim();
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password == null ? null : password.trim();
}

public List<Role> getRoles() {
return roles;
}

public void setRoles(List<Role> roles) {
this.roles = roles;
}

@Override
public boolean equals(Object that) {
if (this == that) {
return true;
}
if (that == null) {
return false;
}
if (getClass() != that.getClass()) {
return false;
}
User other = (User) that;
return (this.getId() == null ? other.getId() == null : this.getId().equals(other.getId()))
&& (this.getUsername() == null ? other.getUsername() == null : this.getUsername().equals(other.getUsername()))
&& (this.getPassword() == null ? other.getPassword() == null : this.getPassword().equals(other.getPassword()));
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((getId() == null) ? 0 : getId().hashCode());
result = prime * result + ((getUsername() == null) ? 0 : getUsername().hashCode());
result = prime * result + ((getPassword() == null) ? 0 : getPassword().hashCode());
return result;
}
}</span>

Role

[java] view plain copy
<span style="font-family:Comic Sans MS;font-size:12px;">package com.sica.domain;

import java.io.Serializable;

public class Role implements Serializable {
private String id;

private String name;

private String jsms;

private String bz;

private Integer jlzt;

private String glbm;

private String userid;

private static final long serialVersionUID = 1L;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id == null ? null : id.trim();
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name == null ? null : name.trim();
}

public String getJsms() {
return jsms;
}

public void setJsms(String jsms) {
this.jsms = jsms == null ? null : jsms.trim();
}

public String getBz() {
return bz;
}

public void setBz(String bz) {
this.bz = bz == null ? null : bz.trim();
}

public Integer getJlzt() {
return jlzt;
}

public void setJlzt(Integer jlzt) {
this.jlzt = jlzt;
}

public String getGlbm() {
return glbm;
}

public void setGlbm(String glbm) {
this.glbm = glbm == null ? null : glbm.trim();
}

public String getUserid() {
return userid;
}

public void setUserid(String userid) {
this.userid = userid == null ? null : userid.trim();
}

@Override
public boolean equals(Object that) {
if (this == that) {
return true;
}
if (that == null) {
return false;
}
if (getClass() != that.getClass()) {
return false;
}
Role other = (Role) that;
return (this.getId() == null ? other.getId() == null : this.getId().equals(other.getId()))
&& (this.getName() == null ? other.getName() == null : this.getName().equals(other.getName()))
&& (this.getJsms() == null ? other.getJsms() == null : this.getJsms().equals(other.getJsms()))
&& (this.getBz() == null ? other.getBz() == null : this.getBz().equals(other.getBz()))
&& (this.getJlzt() == null ? other.getJlzt() == null : this.getJlzt().equals(other.getJlzt()))
&& (this.getGlbm() == null ? other.getGlbm() == null : this.getGlbm().equals(other.getGlbm()))
&& (this.getUserid() == null ? other.getUserid() == null : this.getUserid().equals(other.getUserid()));
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((getId() == null) ? 0 : getId().hashCode());
result = prime * result + ((getName() == null) ? 0 : getName().hashCode());
result = prime * result + ((getJsms() == null) ? 0 : getJsms().hashCode());
result = prime * result + ((getBz() == null) ? 0 : getBz().hashCode());
result = prime * result + ((getJlzt() == null) ? 0 : getJlzt().hashCode());
result = prime * result + ((getGlbm() == null) ? 0 : getGlbm().hashCode());
result = prime * result + ((getUserid() == null) ? 0 : getUserid().hashCode());
return result;
}
}</span>

首先讲一下业务,这里用到的 User 、Role 的对应关系是,一个用户有多个角色,也就是 User : Role 是 1 : n 的关系。因此,在 User 的实体中加入一个 Role 的属性,对应一对多的关系。

然后就是 mapper 接口和 xml 文件了:

mapper接口

UserMapper

[java] view plain copy
<span style="font-family:Comic Sans MS;font-size:12px;">package com.sica.mapper;

import com.sica.domain.User;

import java.util.List;

public interface UserMapper {
int deleteByPrimaryKey(String id);

int insert(User record);

int insertSelective(User record);

User selectByPrimaryKey(String id);

int updateByPrimaryKeySelective(User record);

int updateByPrimaryKey(User record);

List<User> queryForList();
}</span>

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com