项目中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 ...
随机推荐
- mysql常用命令大全 mysql常用命令总结
原文地址:http://www.jbxue.com/db/12472.html 本文介绍下,mysql中常用的一些命令,包括创建与修改数据库.数据库中的表,mysql的权限管理命令grant.revo ...
- (LeetCode 135) Candy N个孩子站成一排,给每个人设定一个权重
原文:http://www.cnblogs.com/AndyJee/p/4483043.html There are N children standing in a line. Each child ...
- SecureCRT 滚动条设置
不久前在Debian下使用kermit时发现kermit有一些优点,比如当串口上不断有信息打印时,仍然可以通过拖动滚动条来查看以前打印的信息,并且滚动条不会滚动到最下面.当按下回车键时,滚动条会自动滚 ...
- 让Laravel5支持memcache的方法
Laravel5框架在Cache和Session中不支持Memcache,看清了是Memcache而不是Memcached哦,MemCached是支持的但是这个扩展真的是装的蛋疼,只有修改部分源码让其 ...
- C# 代码生成工具 Millennials
Millennials 是一个可定制的 C# 代码生成工具,支持 MVC 和三层架构.ADO.NET.Nhibernate 和 LINQ. 项目主页:http://www.open-open.com/ ...
- CSS 之 嵌套 margin-top 处理
如下代码: <div style=" width:1000px; height:700px; margin:auto;"> <div style=" w ...
- WPF 之 实现TextBox输入文字后自动弹出数据(类似百度的输入框)
1.添加一个数据实体类 AutoCompleteEntry,如下: using System; using System.Collections.Generic; using System.Linq; ...
- Java对象初始化顺序
最近我发现了一个有趣的问题,这个问题的答案乍一看下骗过了我的眼睛.看一下这三个类: package com.ds.test; public class Upper { String upperSt ...
- [WinForm] VS2010发布、打包安装程序(超全超详细)
1. 在vs2010 选择“新建项目”→“ 其他项目类型”→“ Visual Studio Installer→“安装项目”: 命名为:Setup1 . 这是在VS2010中将有三个文件夹, 1.“应 ...
- 使用PIL处理image
获得一个Image实例 import Image im = Image.open('1.jpg') #返回一个Image对象,open只对图片的头做处理,所以open操作是非常快的 resize,裁剪 ...