Index: forgon-core/src/main/java/com/forgon/security/service/RoleManagerImpl.java =================================================================== diff -u -r15545 -r15612 --- forgon-core/src/main/java/com/forgon/security/service/RoleManagerImpl.java (.../RoleManagerImpl.java) (revision 15545) +++ forgon-core/src/main/java/com/forgon/security/service/RoleManagerImpl.java (.../RoleManagerImpl.java) (revision 15612) @@ -21,6 +21,7 @@ import com.forgon.tools.Constants; import com.forgon.tools.hibernate.ObjectDao; import com.forgon.tools.string.StringTools; +import com.forgon.tools.util.SqlUtils; import com.forgon.treenode.model.THTreeNode; /** @@ -212,17 +213,33 @@ String[] userIdArray = userIds.split(Role.ROLE_USER_SEPARATOR); Set users = new HashSet(); + //减少查询数据库的次数,优化速度。采用in语句一次查询多个,但是in语句中的元素个数最多只能有1000个,所以超过1000个要分多次查询 + List> userNames = new ArrayList>(); + List currentList = new ArrayList(); + userNames.add(currentList); for (String userId : userIdArray) { if (StringUtils.isNotBlank(userId) && userId.startsWith(Constants.PREFIX_USERID)) { userId = userId.substring(Constants.PREFIX_USERID.length()); - User user = (User) objectDao.getByProperty( - User.class.getName(), "name", userId); - if (user != null) { - users.add(user); + if(currentList.size() >= 800){ + currentList = new ArrayList(); + userNames.add(currentList); } + currentList.add(userId); } } + for(List list : userNames){ + if(CollectionUtils.isNotEmpty(list)){ + String where = SqlUtils.getStringFieldInCollectionsPredicate("name", list); + List userList = objectDao.findBySql(User.class.getName(), " where " + where); + users.addAll(userList); + } + } +// User user = (User) objectDao.getByProperty( +// User.class.getName(), "name", userId); +// if (user != null) { +// users.add(user); +// } role.setUsers(users); }