截取自项目中的一个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层的写法的更多相关文章

  1. 四、spring集成ibatis进行项目中dao层基类封装

    Apache iBatis(现已迁至Google Code下发展,更名为MyBatis)是当前IT项目中使用很广泛的一个半自动ORM框架,区别于Hibernate之类的全自动框架,iBatis对数据库 ...

  2. 02 整合IDEA+Maven+SSM框架的高并发的商品秒杀项目之Service层

    作者:nnngu 项目源代码:https://github.com/nnngu/nguSeckill 首先在编写Service层代码前,我们应该首先要知道这一层到底是干什么的. Service层主要负 ...

  3. log4j根据包名 日志输出到不同文件中 , service层无法输出日志问题

    1. service 层因为要配置事务,使用了代理 <aop:config proxy-target-calss=''true"> <aop:pointcut id=&qu ...

  4. 记一次利用AutoMapper优化项目中数据层到业务层的数据传递过程。

    目前项目中获取到DataSet数据后用下面这种方式复制数据. List<AgreementDoc> list = new List<AgreementDoc>(); ].Row ...

  5. javaweb项目中绝对路径的写法理解

    Tomcat的默认访问路径为http://localhost:8080,后需添加项目路径. 请求转发,是转发到本项目中的其他文件,所以在默认访问路径中添加了本项目的项目路径,故可以省略项目名称: re ...

  6. spring项目中service方法开启线程处理业务的事务问题

    1.前段时间在维护项目的时候碰到一个问题,具体业务就是更新已有角色的资源,数据库已更新,但是权限控制不起效果,还是保留原来的权限. 2.排查发现原有的代码在一个service方法里有进行资源权限表的更 ...

  7. Mybatis项目中不使用代理写法【我】

    首先 spring 配置文件中引入 数据源配置 <?xml version="1.0" encoding="UTF-8"?> <beans x ...

  8. Springboot Maven 多模块项目中 @Service跨模块引用失败的问题

    子模块中引用另一个子模块中的Service, @Autowired失败. 添加了模块之间的依赖没解决. 组以后在启动类上加上 @SpringBootApplication(scanBasePackag ...

  9. React项目中那些奇怪的写法

    1.在一个React组件里看到一个奇怪的写法: const {matchs} = this.props.matchs; 原来,是解构赋值,虽然听说过,但是看起来有点奇怪 下面做个实验: <scr ...

随机推荐

  1. Android 动画机制与使用技巧

    动画效果一直是人机交互中非常重要的部分,与死板.突兀的显示效果不同,动画效果的加入,让交互变得更加友好,特别是在提示.引导类的场景中,合理地使用动画能让用户获得更加愉悦的使用体验 一.Android ...

  2. iOS开发——开发必备OC篇&UITableView设置界面完整封装(四)

    设置界面完整封装(四) 简单MVC实现UITableView设置界面完善封装及拓展使用 关于使用和拓展, 其实基本上就是同UItableView,知识讲数据改一下就可以 拓展使用 1:首先定义一个数组 ...

  3. 字典树(Trie)的java实现

    一.定义 字典树又称单词查找树,Trie树,是一种树形结构,是一种哈希树的变种.典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计.它的优点是:利用 ...

  4. 【转】winform退出代码:Application.Exit和Environment.Exit(0)

    Application.Exit和Environment.Exit(0)有什么退出方面的区别吗? Application.Exit:通知winform消息循环退出.会在所有前台线程退出后,退出应用 强 ...

  5. 类string的构造函数、拷贝构造函数和析构函数

    原文:http://www.cnblogs.com/Laokong-ServiceStation/archive/2011/04/19/2020402.html   类string的构造函数.拷贝构造 ...

  6. 基于css3的3D立方体旋转特效

    今天给大家分享一款基于css3的3D立方体旋转特效.这款特效适用浏览器:360.FireFox.Chrome.Safari.Opera.傲游.搜狗.世界之窗. 不支持IE8及以下浏览器.效果图如下 : ...

  7. java中如何忽略字符串中的转义字符--转载

    原文地址:http://my.oschina.net/u/1010578/blog/366252 起因     这几天工作上需要跟另一个同事联调rest接口,我这边是java他是php,返回报文是js ...

  8. LeetCode6 ZigZag Conversion

    题意: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...

  9. 1.4.2 solr字段类型--(1.4.2.4)使用Dates(日期)

    1.4.2 solr字段类型 (1.4.2.1) 字段类型定义和字段类型属性. (1.4.2.2) solr附带的字段类型 (1.4.2.3) 使用货币和汇率 (1.4.2.4) 使用Dates(日期 ...

  10. Unity3D 使用 UI 的 Grid Layout Group 组件。

    1.首先创建一个容器,用于存放列表项的内容. 这里使用 Panel 来做为容器. 这里要注意! “Grid Layout Group”是要增加在容器的游戏对象里. 同时,只有容器对象的子对象才有排列效 ...