项目中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 ...
随机推荐
- Linux - wc统计文件行数、单词数或字节数
一 wc简单介绍 wc命令用来打印文件的文本行数.单词数.字节数等(print the number of newlines, words, and bytes in files).在Windows的 ...
- OpenRisc-67-OR的汇编
引言 之前我们写过OR的裸机程序,写过基于OR的linux设备驱动程序,也反汇编过OR的机器码. 本小节,我们将通过一个简单的实验,对OR的汇编(指令集)做一个简单的梳理和測试. 1,基本思想 要想了 ...
- centos 安装git server
1.yum install lrzsz wget git 2.安装gitosis:gitosis为Git用户权限管理系统,通过管理服务端的/home/git/.ssh/authorized_key文件 ...
- MySQL 调优基础:Linux内存管理 Linux文件系统 Linux 磁盘IO Linux网络
http://www.cnblogs.com/digdeep/category/739915.html
- Adding DTrace Probes to PHP Extensions
By cj on Dec 06, 2012 The powerful DTrace tracing facility has some PHP-specific probes that can b ...
- char *a 与char a[] 的区别
原文:http://www.cnblogs.com/kaituorensheng/archive/2012/10/23/2736069.html char *a = "hello" ...
- ubutun中安装nginx
一.安装 sudo wget http://nginx.org/download/nginx-1.4.4.tar.gz sudo tar zxvf ng....cd nginx-1.4.4sudo . ...
- 琐碎-hadoop2.2.0-hbase0.96.0-hive0.13.1整合
关于hadoop和hive.hbase的整合就不说了,这里就是在hadoop2.2.0的环境下整合hbase和hive 因为hive0.12不支持hadoop2,所以还要替换一些hadoop的jar包 ...
- Xcode 8:在 Active Compilation Conditions 中自定义环境变量
来源:没故事的卓同学 链接:http://www.jianshu.com/p/96b36360bb2d 在Xcode 7我们在 OTHER_SWIFT_FLAGS中配置环境变量.但是有一个不爽的地方就 ...
- android MotionEvent中getX()和getRawX()的区别
public class Res extends Activity implements View.OnTouchListener { Button btn = null; int x = 0; in ...