一、怎样在用户表里加一个辅助字段属性,用于保存用户拥有的角色javascript
(1)首先,在User模型里加以下属性html
/** * 拥有的角色 */ private List<Role> roles; /** * 只作查询 * 用户拥有的角色列表 * @return */ public List<Role> getRoles() { if(this.roles == null) { this.roles = Role.dao.findRolesByUserId(this.getId()); } return roles; }
(2)这样加了还不够,renderJson的结果根本就没有roles这个属性,这里要使用fastJson结果才会有roles这个属性,至于为何要用fastJson,如今我还没太明白,后面会好好去了解下。fastJson配置以下:java
public void configConstant(Constants me) { me.setJsonFactory(new FastJsonFactory()); }
还要使用maven引用fastJson的jar包:json
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.1.41</version> </dependency>
二、经过以上步骤,能够得到每一个用户拥有的角色对象,如今的问题是怎么在前台显示,这里用到layui的引擎模板,使用以下:maven
{field:'roles', width:120,title: '角色',templet: '#titleTpl'}
<script type="text/html" id="titleTpl"> {{# layui.each(d.roles, function(index, item){ }} <span>{{ item.description }} </span> {{# }); }} </script>