(CRM项目)
复制配置文件
创建包cn.wolfcode.config
cn.wolfcode.controller
cn.wolfcode.service
cn.wolfcode.service.impl
cn.wolfcode.qo
cn.wolfcode.util
在main路径下创建webapp/WEB-INF/web.xml和views包
cn.wolfcode.config里写MyFreemarkerConfig
MyFreemarkerConfig类
public class MyFreemarkerConfig extends FreeMarkerConfigurer {
@Override
public void afterPropertiesSet() throws IOException, TemplateException {
super.afterPropertiesSet();
Configuration configuration = this.getConfiguration();
configuration.setSharedVariable("shiro",new ShiroTags());
}
}
PermissionMapper接口
public interface PermissionMapper {
int deleteByPrimaryKey(Long id);
int insert(Permission record);
Permission selectByPrimaryKey(Long id);
List<Permission> selectAll();
int updateByPrimaryKey(Permission record);
int count();
List<Permission> list(QueryObject qo);
List<String> selectExpressionByCurrentuserId(Long id);
List<String> selectAllExpression();
}
IPermissionService类
public interface IPermissionService {
int deleteByPrimaryKey(Long id);
int insert(Permission record);
Permission selectByPrimaryKey(Long id);
List<Permission> selectAll();
int updateByPrimaryKey(Permission record);
PageInfo<Permission> list(QueryObject qo);
List<String> selectExpressionByCurrentuserId(Long id);
List<String> selectAllExpression();
}
PermissionServiceImpl类
@Service
public class PermissionServiceImpl implements IPermissionService {
@Autowired
private PermissionMapper mapper;
@Override
public int deleteByPrimaryKey(Long id) {
return mapper.deleteByPrimaryKey(id);
}
@Override
public int insert(Permission record) {
return mapper.insert(record);
}
@Override
public Permission selectByPrimaryKey(Long id) {
return mapper.selectByPrimaryKey(id);
}
@Override
public List<Permission> selectAll() {
return mapper.selectAll();
}
@Override
public int updateByPrimaryKey(Permission record) {
return 0;
}
@Override
public PageInfo<Permission> list(QueryObject qo) {
//查询一页的Permission数据
PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
List<Permission> list = mapper.list(qo);
return new PageInfo<Permission>(list);
}
@Override
public List<String> selectExpressionByCurrentuserId(Long id) {
return mapper.selectExpressionByCurrentuserId(id);
}
@Override
public List<String> selectAllExpression() {
return mapper.selectAllExpression();
}
}
PermissionMapper.xml
<select id="selectAll" resultMap="BaseResultMap" >
select id, name, expression
from permission
</select>
<select id="count" resultType="java.lang.Integer">
select count(*) from permission
</select>
<select id="list" resultType="cn.wolfcode.domain.Permission">
select * from permission
</select>
<select id="selectExpressionByCurrentuserId" resultType="java.lang.String">
select p.expression
from employee_role er left join role_permission rp on er.role_id = rp.role_id
left join permission p on rp.permission_id = p.id
where er.employee_id = #{id}
</select>
<select id="selectAllExpression" resultType="java.lang.String">
select expression from permission
</select>
PermissionController
@Controller
@RequestMapping("/permission")
public class PermissionController {
@Autowired
private IPermissionService permissionService;
@Autowired
private IRolePermissionService rolePermissionService;
@Autowired
private ApplicationContext applicationContext;
@RequestMapping("/list")
public String list(QueryObject qo, Model model){
PageInfo<Permission> pr = permissionService.list(qo);
model.addAttribute("pageResult", pr);
return "permission/list";
}
@RequestMapping("/delete")
public String delete(Long id)
{
//删除权限表中的记录
permissionService.deleteByPrimaryKey(id);
//删除角色——权限表中,有该权限的记录
//通过权限id到角色——权限表中,删除相关记录
rolePermissionService.deleteByPermissionId(id);
return "redirect:/permission/list.do";
}
@RequestMapping("/reload")
@ResponseBody
public JsonResult reload(){
try {
//获取所有的权限表达式
List<String> list = permissionService.selectAllExpression();
Map<String, Object> beans = applicationContext.getBeansWithAnnotation(Controller.class);
Collection<Object> controllerCglibProxys = beans.values();
for (Object controllerCglibProxy : controllerCglibProxys) {
if (!AopUtils.isCglibProxy(controllerCglibProxy)) {
continue;
}
Class clzz = controllerCglibProxy.getClass().getSuperclass();
Method[] methods = clzz.getDeclaredMethods();
for (Method method : methods) {
RequiresPermissions requiresPermissions = method.getAnnotation(RequiresPermissions.class);
if (requiresPermissions!=null){
String[] expandname = requiresPermissions.value();
String expression = expandname[0];
String name = expandname[1];
//判断数据库中是否包含当前expression
if (!list.contains(expression)){
Permission permission = new Permission();
permission.setName(name);
permission.setExpression(expression);
permissionService.insert(permission);
}
}
}
}
return new JsonResult(true,"重新加载成功");
}catch (Exception e){
return new JsonResult(false,"重新加载失败");
}
}
permission/list.ftl
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>权限管理</title>
<#include "../common/link.ftl">
<script>
function permissionreset(){
//window.location.href = '/permission/reload.do';
$.post('/permission/reload.do',function (obj){
if (obj.success){
$.messager.alert("加载成功");
window.setTimeout(function (){
window.location.reload();
},1000)
}else {
$.messager.popup(obj.msg);
}
});
}
</script>
</head>
<body class="hold-transition skin-blue sidebar-mini">
<div class="wrapper">
<#include "../common/navbar.ftl">
<!--菜单回显-->
<#assign currentMenu="permission">
<#include "../common/menu.ftl">
<div class="content-wrapper">
<section class="content-header">
<h1>权限管理</h1>
</section>
<section class="content">
<div class="box" >
<!--高级查询--->
<form class="form-inline" id="searchForm" action="/permission/list.do" method="post">
<input type="hidden" name="currentPage" id="currentPage" value="1">
<a href="javascript:permissionreset();" class="btn btn-success btn_reload" style="margin: 10px;">
<span class="glyphicon glyphicon-repeat"></span> 重新加载
</a>
</form>
<table class="table table-striped table-hover" >
<thead>
<tr>
<th>编号</th>
<th>权限名称</th>
<th>权限表达式</th>
<th>操作</th>
</tr>
</thead>
<#list (pageResult.list)! as permission>
<tr>
<td>${(permission_index+1)!}</td>
<td>${(permission.name)!}</td>
<td>${(permission.expression)!}</td>
<td>
<a href="/permission/delete.do?id=${(permission.id)!}" class="btn btn-danger btn-xs btn_delete" >
<span class="glyphicon glyphicon-trash"></span> 删除
</a>
</td>
</tr>
</#list>
</table>
<!--分页-->
<#include "../common/page.ftl">
</div>
</section>
</div>
<#include "../common/footer.ftl">
</div>
</body>
</html>