当前位置:首页>财经>正文

如何用mybatis多对一多张表查询 mybatis多表查询问题

2023-04-14 22:52:49 互联网 未知 财经

 如何用mybatis多对一多张表查询 mybatis多表查询问题

如何用mybatis多对一多张表查询

可以用语句select * from students,不过新手我还是建议不要写关联,直接把resultmap写一个既有students的属性又有class的属性的map,然后再select s.*,c.* from student s left join classInfo c on s.classInfo_id = c.id,这样返回的resultmap就是两个对象都有的map了,自己再创建一个对象,里面和resultmap相对应就可以了。

mybatis多表查询问题

private static void handleException(Exception e)
{
String msg = null
if (e instanceof InvocationTargetException)
{
Throwable targetEx = ((InvocationTargetException) e)
.getTargetException()
if (targetEx != null)
{

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

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

User

[java] view plain copy
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 Listroles 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 getRoles() { return roles } public void setRoles(List 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 } }

Role

[java] view plain copy
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
}
}

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

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

mapper接口

UserMapper

[java] view plain copy
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)

ListqueryForList() }

随便看看