源码获取:关注文末gongzhonghao,输入010领取下载链接
开发工具:IDEA 数据库mysql5.7
技术:springboot+jpa+shiro+redis+layui
前台截图:
后台截图:
package com.qxczh.admin.service.impl;
import com.qxczh.admin.service.AdminUserService;
import com.qxczh.admin.service.PermissionService;
import com.qxczh.admin.service.RoleService;
import com.qxczh.common.base.BaseServiceImpl;
import com.qxczh.common.dao.RoleDao;
import com.qxczh.common.dto.QuarkResult;
import com.qxczh.common.entity.Permission;
import com.qxczh.common.entity.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Stream;
import static java.util.stream.Collectors.toSet;
@Service
@Transactional
public class RoleServiceImpl extends BaseServiceImpl<RoleDao, Role> implements RoleService {
@Autowired
private AdminUserService userService;
@Autowired
private PermissionService permissionService;
@Override
public QuarkResult findRolesAndSelected(Integer id) {
Set<Role> userRole = userService.findOne(id).getRoles();
List<Role> roles = findAll();
for (Role r: roles) {
if (userRole.contains(r)) r.setSelected(1);
}
return QuarkResult.ok(roles);
}
@Override
public Page<Role> findByPage(int pageNo, int length) {
PageRequest pageRequest = new PageRequest(pageNo, length);
Page<Role> page = repository.findAll(pageRequest);
return page;
}
@Override
public void saveRolePermission(Integer roleid, Permission[] pers) {
Role role = findOne(roleid);
if (pers==null){
role.setPermissions(new HashSet<>());
}
else {
role.setPermissions(Stream.of(pers).collect(toSet()));
}
save(role);
}
}
package com.qxczh.admin.service.impl;
import com.qxczh.admin.service.UserService;
import com.qxczh.common.base.BaseServiceImpl;
import com.qxczh.common.dao.UserDao;
import com.qxczh.common.entity.User;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@Service
public class UserServiceImpl extends BaseServiceImpl<UserDao,User> implements UserService {
@Override
public Page<User> findByPage(User user, int pageNo, int length) {
PageRequest pageable = new PageRequest(pageNo, length);
Specification<User> specification = new Specification<User>() {
@Override
public Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) {
Path<Integer> $id = root.get("id");
Path<String> $username = root.get("username");
Path<String> $email = root.get("email");
Path<Integer> $enable = root.get("enable");
ArrayList<Predicate> list = new ArrayList<>();
if (user.getId() != null) list.add(criteriaBuilder.equal($id, user.getId()));
if (user.getEnable() != null) list.add(criteriaBuilder.equal($enable, user.getEnable()));
if (user.getUsername() != null)
list.add(criteriaBuilder.like($username, "%" + user.getUsername() + "%"));
if (user.getEmail()!=null)
list.add(criteriaBuilder.like($email, "%" + user.getEmail() + "%"));
Predicate predicate = criteriaBuilder.and(list.toArray(new Predicate[list.size()]));
return predicate;
}
};
Page<User> page = repository.findAll(specification, pageable);
return page;
}
@Override
public void saveUserEnable(Integer[] id) {
List<User> all = findAll(Arrays.asList(id));
for (User user :all) {
if (user.getEnable() == 1) {
user.setEnable(0);
} else {
user.setEnable(1);
}
}
save(all);
}
}