Spring Ldap 分页

LDAP.search()当查询的数据较多时,当查询的数目较大时,会导致内存溢出,因此采用Ldap的分页进行查询

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public class ChraUserService  
  2. {  
  3. @Autowired  
  4. SearchEmployeeLdapDao searchEmployeeLdapDao;  
  5. public void employeeinfo()  
  6. {  
  7.  PagedResultsCookie cookie=null;  
  8.  PagedResult list=null;   
  9.  SearchEmployeeLdapDao.Initldap();//初始化Ldap  
  10.  do {   
  11.    list=searchEmployeeLdapDao.searchlist(cookie); //获得查询分页的List  
  12.    cookie=list.getCookie();  
  13.    } while (list.getCookie().getCookie()!=null);//判断是否结束  
  14.  }  
  15. }  
[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public class SearchEmployeeLdapDao  
  2. {  
  3. @Autowired  
  4. LdapTemplate ldapTemplate;//spring LDap dataSource  
  5. private AndFilter filter = null;  
  6. private SearchControls searchControls=null;  
  7. public void initldap()  
  8. {  
  9. ldapTemplate.setIgnorePartialResultException(true);  
  10. filter=new AndFilter(); //查询的过滤条件  
  11. filter.and(new EqualsFilter("objectclass""user"));  
  12. filter.and(new LikeFilter("uid""Tim*"));  
  13. searchControls = new SearchControls();  
  14. searchControls.setSearchScope( SearchControls.SUBTREE_SCOPE );  
  15. String[] s={"employeeNumber","uid","department","mail","title","thumbnailPhoto"}; //查询返回的字段  
  16. searchControls.setReturningAttributes(s);  
  17. }  
  18. public PagedResult searchlist(PagedResultsCookie cookie)  
  19. {  
  20. PagedResultsRequestControl control = new PagedResultsRequestControl(pagesize, cookie);  
  21. List<MemberBean> list= ldapTemplate.search( "", filter.encode(),searchControls, new ChraUserAttributesMapper(),control);  
  22. return new PagedResult(list, control.getCookie());  
  23. }  
[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public class ChraUserAttributesMapper implements AttributesMapper  
  2. {  
  3. @Override  
  4. public MemberBean mapFromAttributes(Attributes attrs) throws NamingException {  
  5. NamingEnumeration<? extends Attribute> en = attrs.getAll();  
  6. while (en.hasMoreElements())  
  7. {  
  8. BasicAttribute object = (BasicAttribute) en.nextElement();  
  9. //业务逻辑  
  10. }  
  11. }  

spring LDAP的xml配置文件

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <beans>  
  2. <bean id="contextSource"  
  3. class="org.springframework.ldap.core.support.LdapContextSource">  
  4. <property name="url" value="***********************" />  
  5. <property name="base" value="*********************" />  
  6. <property name="referral" value="follow"/>  
  7. </bean>  
  8. <bean id="ldapTemplate"  
  9. class="org.springframework.ldap.core.LdapTemplate">  
  10. <constructor-arg ref="contextSource" />  
  11. </bean>  
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值