<file_sep>package me.lb.service.system.impl;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import me.lb.model.system.Perm;
import me.lb.model.system.Role;
import me.lb.service.common.impl.GenericServiceImpl;
import me.lb.service.system.RoleService;

import org.springframework.stereotype.Service;

@Service
public class RoleServiceImpl extends GenericServiceImpl<Role, Integer>
		implements RoleService {

	@Override
	public void auth(int roleId, List<Integer> permIds) {
		// 由于cascade方式只级联删除操作，所以这里可以通过欺骗的方式提升效率
		Set<Perm> perms = new HashSet<Perm>();
		for (int permId : permIds) {
			// 这里只要构建数据库中存在id的对象即可，避免了查询的开销
			Perm perm = new Perm();
			perm.setId(permId);
			perms.add(perm);
		}
		// 查询角色信息，更新关联（直接更新）
		Role role = dao.findById(roleId);
		role.setPerms(perms);
		dao.update(role);
	}

}<file_sep>package me.lb.dao.system;

import me.lb.dao.common.GenericDao;
import me.lb.model.system.User;

public interface UserDao extends GenericDao<User, Integer> {

}<file_sep>package me.lb.dao.system;

import me.lb.dao.common.GenericDao;
import me.lb.model.system.Role;

public interface RoleDao extends GenericDao<Role, Integer> {

}<file_sep>package me.lb.service.system;

import java.util.List;

import me.lb.model.system.Perm;
import me.lb.service.common.GenericService;

public interface PermService extends GenericService<Perm, Integer> {

	/**
	 * 查询全部的顶级资源（没有父id）
	 */
	public List<Perm> findTopPerms();

}<file_sep>package me.lb.dao.common;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import me.lb.dao.common.impl.GenericDaoImpl;
import me.lb.model.pagination.Pagination;
import me.lb.model.system.User;

import org.springframework.stereotype.Repository;

@Repository
public class CommonDaoImpl extends GenericDaoImpl<User, Integer> implements
		CommonDao {

	@Override
	public Pagination<User> pagingQuery() {
		return getPagination("from User", null);
	}

	@Override
	public Pagination<User> pagingQuery(Map<String, Object> params) {
		StringBuffer sb = new StringBuffer("from User as o where 1=1");
		List<Object> objs = new ArrayList<Object>();
		Iterator<Map.Entry<String, Object>> it = params.entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry<String, Object> me = it.next();
			// 需要额外处理模糊查询的参数
			// if ("orLogic".equals(me.getKey())) {
			// sb.append(" or o." + me.getKey() + " like ?");
			// objs.add("%" + me.getValue() + "%");
			// } else {
			sb.append(" and o." + me.getKey() + " = ?");
			objs.add(me.getValue());
			// }
		}
		return getPagination(sb.toString(), objs);
	}

}<file_sep>package me.lb.dao.system;

import java.util.List;

import me.lb.dao.common.GenericDao;
import me.lb.model.system.Perm;

public interface PermDao extends GenericDao<Perm, Integer> {

	/**
	 * 查询全部的顶级资源（没有父id）
	 */
	public List<Perm> findTopPerms();

}<file_sep>package me.lb.service.system.impl;

import java.util.List;

import me.lb.dao.system.PermDao;
import me.lb.model.system.Perm;
import me.lb.service.common.impl.GenericServiceImpl;
import me.lb.service.system.PermService;

import org.springframework.stereotype.Service;

@Service
public class PermServiceImpl extends GenericServiceImpl<Perm, Integer>
		implements PermService {

	@Override
	public List<Perm> findTopPerms() {
		return ((PermDao) dao).findTopPerms();
	}

}