@Transient
private String roles; // 所拥有的角色
package com.java1234.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import org.hibernate.validator.constraints.NotEmpty;
@Entity
@Table(name="t_user")
public class User {
@Id
@GeneratedValue
private Integer id;
@NotEmpty(message="请输入用户名!")
@Column(length=50)
private String userName;
@NotEmpty(message="请输入密码!")
@Column(length=50)
private String password;
@Column(length=50)
private String trueName;
@Column(length=1000)
private String remarks;
@Transient
private String roles;
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 getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getTrueName() {
return trueName;
}
public void setTrueName(String trueName) {
this.trueName = trueName;
}
public String getRemarks() {
return remarks;
}
public void setRemarks(String remarks) {
this.remarks = remarks;
}
public String getRoles() {
return roles;
}
public void setRoles(String roles) {
this.roles = roles;
}
}
package com.java1234.controller.admin;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.java1234.entity.Role;
import com.java1234.entity.User;
import com.java1234.service.RoleService;
import com.java1234.service.UserService;
@Controller
@RequestMapping("/admin/user")
public class UserAdminController {
@Resource
private UserService userService;
@Resource
private RoleService roleService;
/**
* 分页查询用户信息
* @param user
* @param page
* @param rows
* @return
* @throws Exception
*/
@ResponseBody
@RequestMapping("/list")
public Map<String,Object> list(User user,@RequestParam(value="page",required=false)Integer page,@RequestParam(value="rows",required=false)Integer rows)throws Exception{
Map<String,Object> resultMap=new HashMap<>();
List<User> userList=userService.list(user, page, rows, Direction.ASC, "id");
for(User u:userList){
List<Role> roleList=roleService.findByUserId(u.getId());
StringBuffer sb=new StringBuffer();
for(Role r:roleList){
sb.append(","+r.getName());
}
u.setRoles(sb.toString().replaceFirst(",", ""));
}
Long total=userService.getCount(user);
resultMap.put("rows", userList);
resultMap.put("total", total);
return resultMap;
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>用户管理</title>
<link rel="stylesheet" type="text/css" href="/static/jquery-easyui-1.3.3/themes/default/easyui.css"></link>
<link rel="stylesheet" type="text/css" href="/static/jquery-easyui-1.3.3/themes/icon.css"></link>
<script type="text/javascript" src="/static/jquery-easyui-1.3.3/jquery.min.js"></script>
<script type="text/javascript" src="/static/jquery-easyui-1.3.3/jquery.easyui.min.js"></script>
<script type="text/javascript" src="/static/jquery-easyui-1.3.3/locale/easyui-lang-zh_CN.js"></script>
<script type="text/javascript">
function formatEdit(val,row){
return "<a href=''><img style='margin-top:4px' src='/static/images/edit.gif'/></a>";
}
function searchUser(){
$("#dg").datagrid('load',{
"userName":$("#s_userName").val()
});
}
</script>
</head>
<body style="margin: 1px">
<table id="dg" title="用户管理" class="easyui-datagrid"
fitColumns="true" pagination="true" rownumbers="true" singleSelect="true"
url="/admin/user/list" fit="true" toolbar="#tb">
<thead>
<th field="id" width="20" align="center">编号</th>
<th field="userName" width="50" align="center">用户名</th>
<th field="password" width="50" align="center">密码</th>
<th field="trueName" width="50" align="center">真实姓名</th>
<th field="remarks" width="80" align="center">备注</th>
<th field="roles" width="150" align="center">拥有角色</th>
<th field="aa" width="50" align="center" formatter="formatEdit">角色设置</th>
</thead>
</table>
<div id="tb">
<div>
<a href="javascript:openUserAddDialog()" class="easyui-linkbutton" iconCls="icon-add" plain="true">添加</a>
<a href="javascript:openUserModifyDialog()" class="easyui-linkbutton" iconCls="icon-edit" plain="true">修改</a>
<a href="javascript:deleteUser()" class="easyui-linkbutton" iconCls="icon-remove" plain="true">删除</a>
</div>
<div>
用户名: <input type="text" id="s_userName" size="20" onkeydown="if(event.keyCode==13) searchUser()"/>
<a href="javascript:searchUser()" class="easyui-linkbutton" iconCls="icon-search" plain="true">搜索</a>
</div>
</div>
</body>
</html>