比如一个用户对应多个角色
首先用户表
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(64) DEFAULT NULL,
`nickname` varchar(64) DEFAULT NULL,
`password` varchar(255) DEFAULT NULL,
`enabled` tinyint(1) DEFAULT 1,
`email` varchar(64) DEFAULT NULL,
`userface` varchar(255) DEFAULT NULL,
`reg_time` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=21 DEFAULT CHARSET=utf8;
然后角色表
CREATE TABLE `roles` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(32) DEFAULT NULL,
`role_code` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
用户与角色的关联表
CREATE TABLE `roles_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`rid` int(11) DEFAULT 2,
`uid` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `rid` (`rid`),
KEY `roles_user_ibfk_2` (`uid`),
CONSTRAINT `roles_user_ibfk_1` FOREIGN KEY (`rid`) REFERENCES `roles` (`id`),
CONSTRAINT `roles_user_ibfk_2` FOREIGN KEY (`uid`) REFERENCES `user` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=131 DEFAULT CHARSET=utf8;
user实体
package com.pwl.vcloud.common.entity;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* @author
* @date 2019/7/15 16:25
* @description: 接口.
*/
public class UserVO implements Serializable {
private Integer id;
private String username;
private String nickname;
private String password;
private Integer enabled;
private String email;
private String userface;
private Date regTime;
/**
* 角色列表
*/
private List roleList = new ArrayList<>();
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getEnabled() {
return enabled;
}
public void setEnabled(Integer enabled) {
this.enabled = enabled;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getUserface() {
return userface;
}
public void setUserface(String userface) {
this.userface = userface;
}
public Date getRegTime() {
return regTime;
}
public void setRegTime(Date regTime) {
this.regTime = regTime;
}
public List getRoleList() {
return roleList;
}
public void setRoleList(List roleList) {
this.roleList = roleList;
}
}
角色实体
package com.pwl.vcloud.common.entity;
import java.io.Serializable;
/**
*
*
*
*
* @author pwl
* @since 2019-07-14
*/
public class Roles implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private String name;
private String roleCode;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRoleCode() {
return roleCode;
}
public void setRoleCode(String roleCode) {
this.roleCode = roleCode;
}
}
userMapper
id, username, nickname, password, enabled, email, userface, regTime AS regTime
通过传入的username来查询对应的角色
查询到有两个角色,通过字段映射到相关的字段
上面的collection中property的roleList 对应的是user实体中的属性
ofType:这个属性必须要,指映射到哪个实体类型上
注意:
如果一对多查询时,上面的这种查询分页会有问题,分页不对。
在collection中使用select
coloumn 是传进去的值
一样也可以查询出来