项目中Service层的写法
截取自项目中的一个service实现类,记录一下:
base类
package com.bupt.auth.service.base; import javax.annotation.Resource; import com.bupt.auth.dao.OauthClientDao;
import com.bupt.auth.dao.PermissionDao;
import com.bupt.auth.dao.ResourceDao;
import com.bupt.auth.dao.RoleDao;
import com.bupt.auth.dao.TokenDao;
import com.bupt.auth.dao.UserDao; public class BaseManagerImpl
{
@Resource
protected UserDao userDao;
@Resource
protected RoleDao roleDao;
@Resource
protected ResourceDao resourceDao;
@Resource
protected PermissionDao permissionDao;
@Resource
protected TokenDao tokenDao;
@Resource(name="oauthDao")
protected OauthClientDao clientDao; public BaseManagerImpl(){} }
具体业务的实现类:
package com.bupt.auth.service.impl; import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet; import org.springframework.stereotype.Service; import com.bupt.auth.dao.RoleDao;
import com.bupt.auth.entity.Permission;
import com.bupt.auth.entity.Resource;
import com.bupt.auth.entity.Role;
import com.bupt.auth.entity.User;
import com.bupt.auth.exception.MyException;
import com.bupt.auth.service.RoleManager;
import com.bupt.auth.service.base.BaseManagerImpl;
@Service("roleManager")
public class RoleManagerImpl extends BaseManagerImpl implements RoleManager
{ @Override
public Role findRoleAdminByUserId(Long id) {
// TODO Auto-generated method stub
Role role = roleDao.findRoleAdminByUserId(id);
role = roleDao.loadRoleByRoleId(role.getId()); return role;
} @Override
public void update(Role role) {
// TODO Auto-generated method stub
roleDao.update(role);
} @Override
public boolean addRole(Role role) throws MyException {
// TODO Auto-generated method stub
if(roleDao.findRoleByRoleNameAndUserId(role.getOwnerUser().getId(), role.getName()) != null){
throw new MyException("This role has already exist!!", "300");
}
try{
roleDao.save(role);
}catch(Exception e){
return false;
}
return true;
} @Override
public List<Role> findRoleByUserId(Long id) {
// TODO Auto-generated method stub
return roleDao.findRoleByUserId(id);
} @Override
public boolean deleteRole(Long id) {
// TODO Auto-generated method stub
try{
roleDao.delete(id);
}catch(Exception e){
return false;
}
return true;
} @Override
public Role getById(Long id) {
// TODO Auto-generated method stub
return roleDao.loadRoleByRoleId(id);
} @Override
public Role findRoleByRoleNameAndUserId(Long id, String rolename) {
// TODO Auto-generated method stub
return roleDao.findRoleByRoleNameAndUserId(id, rolename);
} @Override
public SortedSet<Long> findRoleByVePeId(Set<String> vpId, int veorpe)
{
Set<User> user = getUserFromVPId(vpId, veorpe);
return getRoleFromUser(user); } private SortedSet<Long> getRoleFromUser(Set<User> user)
{
SortedSet<Long> result = new TreeSet<Long>();
for (User u : user)
{
//该用户的所有角色
List<Role> role = roleDao.findRoleByUserId(u.getId());
//将角色对应的ID放到result中
for (Role r : role)//如果角色没有设置规则,那么将id的负值放到result中
{
if (!r.getName().equals("administrator"))
result.add(r.getConstraintRole().equals("public") ? -r.getId() : r.getId());
}
} return result;
} private Set<User> getUserFromVPId(Set<String> vpId, int veorpe)
{
Set<User> user = new HashSet<User>();
for (String id : vpId)
{
Resource resource = resourceDao.findResourceByResId(id, veorpe);
user.add(resource.getUser());
}
return user;
} @Override
public List<Role> findRoleByPermId(Long id) {
// TODO Auto-generated method stub
return roleDao.findRoleByPermId(id);
} @Override
public boolean deleteRolesPermissionByRoleIdAndPermId(Role role,
Long permid) throws MyException {
// TODO Auto-generated method stub
return roleDao.deleteRolesPermissionByRoleIdAndPermId(role, permid);
} @Override
public boolean updateRole(Role role, Set<Long> old,
Set<Long> addperm) throws MyException {
// TODO Auto-generated method stub
//Role role = this.getById(roleid); if(old != null && old.size() != 0){
for(Long permid : old){
this.deleteRolesPermissionByRoleIdAndPermId(role, permid);
}
} if(addperm != null && addperm.size() != 0){
for(Long permid : addperm){
Permission perm = permissionDao.getById(permid);
Set<Permission> permset = role.getPermissions();
permset.add(perm);
this.update(role);
}
} return true;
} @Override
public Role generateRole(String rolename, String description,
Set<Long> permissions, User user) {
// TODO Auto-generated method stub
try{
Role role = new Role(); role.setName(rolename);
role.setConstraintRole("public");
role.setDescription(description);
role.setOwnerUser(user); Set<String> accessTokens = new HashSet<String>();
accessTokens.add(user.getAccesstoken());
role.setAccessTokens(accessTokens); roleDao.save(role);
Role adminrole = roleDao.findRoleAdminByUserId(user.getId());
Set<Permission> totalPerm = adminrole.getPermissions(); if(totalPerm == null || totalPerm.size() == 0)
return null; Set<Permission> set = new HashSet<Permission>();
for(Long perm_id : permissions){
Permission perm = permissionDao.loadPermissionById(perm_id);
if(perm != null && totalPerm.contains(perm)){
set.add(perm);
}
} role.setPermissions(set);
roleDao.update(role);
return role;
}catch(Exception e){
return null;
}
} @Override
public boolean updateRole(Long id, Set<Long> permissions) throws MyException {
// TODO Auto-generated method stub
Set<Long> old = new HashSet<Long>();
Role role = roleDao.loadRoleByRoleId(id); if(role == null){
throw new MyException("Role Not Found", "301");
} Set<Permission> pset = role.getPermissions();
if(pset != null && pset.size() != 0){
for(Permission perm:pset){
old.add(perm.getId());
}
updateRole(role, old, null);
} Role adminrole = roleDao.findRoleAdminByUserId(role.getOwnerUser().getId());
Set<Permission> totalPerm = adminrole.getPermissions();
if(totalPerm == null || totalPerm.size() == 0){
return false;
} for(Long perm_id:permissions){
if(permissionDao.getById(perm_id) == null){
permissions.remove(perm_id);
}
} if(permissions != null && permissions.size() != 0)
updateRole(role, null, permissions); return true;
}
}
项目中Service层的写法的更多相关文章
- 四、spring集成ibatis进行项目中dao层基类封装
Apache iBatis(现已迁至Google Code下发展,更名为MyBatis)是当前IT项目中使用很广泛的一个半自动ORM框架,区别于Hibernate之类的全自动框架,iBatis对数据库 ...
- 02 整合IDEA+Maven+SSM框架的高并发的商品秒杀项目之Service层
作者:nnngu 项目源代码:https://github.com/nnngu/nguSeckill 首先在编写Service层代码前,我们应该首先要知道这一层到底是干什么的. Service层主要负 ...
- log4j根据包名 日志输出到不同文件中 , service层无法输出日志问题
1. service 层因为要配置事务,使用了代理 <aop:config proxy-target-calss=''true"> <aop:pointcut id=&qu ...
- 记一次利用AutoMapper优化项目中数据层到业务层的数据传递过程。
目前项目中获取到DataSet数据后用下面这种方式复制数据. List<AgreementDoc> list = new List<AgreementDoc>(); ].Row ...
- javaweb项目中绝对路径的写法理解
Tomcat的默认访问路径为http://localhost:8080,后需添加项目路径. 请求转发,是转发到本项目中的其他文件,所以在默认访问路径中添加了本项目的项目路径,故可以省略项目名称: re ...
- spring项目中service方法开启线程处理业务的事务问题
1.前段时间在维护项目的时候碰到一个问题,具体业务就是更新已有角色的资源,数据库已更新,但是权限控制不起效果,还是保留原来的权限. 2.排查发现原有的代码在一个service方法里有进行资源权限表的更 ...
- Mybatis项目中不使用代理写法【我】
首先 spring 配置文件中引入 数据源配置 <?xml version="1.0" encoding="UTF-8"?> <beans x ...
- Springboot Maven 多模块项目中 @Service跨模块引用失败的问题
子模块中引用另一个子模块中的Service, @Autowired失败. 添加了模块之间的依赖没解决. 组以后在启动类上加上 @SpringBootApplication(scanBasePackag ...
- React项目中那些奇怪的写法
1.在一个React组件里看到一个奇怪的写法: const {matchs} = this.props.matchs; 原来,是解构赋值,虽然听说过,但是看起来有点奇怪 下面做个实验: <scr ...
随机推荐
- IPC——信号量
Linux进程间通信——使用信号量 这篇文章将讲述别一种进程间通信的机制——信号量.注意请不要把它与之前所说的信号混淆起来,信号与信号量是不同的两种事物.有关信号的更多内容,可以阅读我的另一篇文章:L ...
- 使用SQL*PLUS,构建完美excel或html输出
通过SQL*PLUS我们可以构建友好的输出,满足多样化用户需求.本例通过简单示例,介绍通过sql*plus输出xls,html两种格式文件.首先创建两个脚本:1.main.sql用以设置环境,调用具体 ...
- 经典排序算法总结与实现 ---python
原文:http://wuchong.me/blog/2014/02/09/algorithm-sort-summary/ 经典排序算法在面试中占有很大的比重,也是基础,为了未雨绸缪,在寒假里整理并用P ...
- 你应该知道的基础 Git 命令
我们在早先一篇文章中已经快速介绍过 Vi 速查表了.在这篇文章里,我们将会介绍开始使用 Git 时所需要的基础命令. Git Git 是一个分布式版本控制系统,它被用在大量开源项目中.它是在 2005 ...
- 分布式缓存技术redis学习(二)——详细讲解redis数据结构(内存模型)以及常用命令
Redis数据类型 与Memcached仅支持简单的key-value结构的数据记录不同,Redis支持的数据类型要丰富得多,常用的数据类型主要有五种:String.List.Hash.Set和Sor ...
- 1.4.7 Schema API
Schema API Schema API允许使用REST API每个集合(collection)(或者单机solr的核(core)).包含了定义字段类型,字段,动态字段,复制字段等.在solr4.2 ...
- leetcode题解:Tree Level Order Traversal II (二叉树的层序遍历 2)
题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...
- Unslider--使用手册系列(一)
Unslider--入门篇 背景:因工作需求,需要完成一个图片轮播效果,因博主不是专业的前端开发人员,so google之,经过挑选最终选择使用Unslider插件完成工作. 一.Unslider插件 ...
- 转载:Character data is represented incorrectly when the code page of the client computer differs from the code page of the database in SQL Server 2005
https://support.microsoft.com/en-us/kb/904803 Character data is represented incorrectly when the cod ...
- angularJs中图表功能(有集成框架)-angular-flot
1.柱状图和折线图的数据格式: $scope.Chart.data = [ { label: "离线", data: [[0, 2]] }, { label: "在线&q ...