解读Spring LDAP 帮助中的代码案例 (二)

书接上回,上章,主要讲的操作的思想和方法,所以上一章是非常重要,如果上一章还没测底明白,最好还是先多看看上章或官方帮助.

接下来我门看下第三章.第三章的上班部是一些简单的桥梁,上来承上启下,所以感觉没必要解读,很鸡肋,跟上上章的思路看下面的代码,这个spring总结出来的完整类

package com.example.dao;import java.util.List;import javax.naming.Name;import javax.naming.NamingException;import javax.naming.directory.Attributes;import org.springframework.ldap.core.AttributesMapper;import org.springframework.ldap.core.ContextMapper;import org.springframework.ldap.core.LdapTemplate;import org.springframework.ldap.core.DirContextAdapter;import org.springframework.ldap.core.support.DistinguishedName;import org.springframework.ldap.filter.AndFilter;import org.springframework.ldap.filter.EqualsFilter;import org.springframework.ldap.filter.WhitespaceWildcardsFilter;/**用户DAO实现层*/public class PersonDaoImpl implements PersonDao { /**注入*/ private LdapTemplate ldapTemplate;public void setLdapTemplate(LdapTemplate ldapTemplate) {this.ldapTemplate = ldapTemplate;}/**这个方法封装他的重载方法*/protected Name buildDn(Person person) {return buildDn(person.getFullname(), person.getCompany(), person.getCountry());}/**该方法放入了他的cn的值,ou的值和c的值,返回组建好的dn*/protected Name buildDn(String fullname, String company, String country) {DistinguishedName dn = new DistinguishedName();dn.add(“c”, country);dn.add(“ou”, company);dn.add(“cn”, fullname);return dn;}/**类似上一章在创建时封装的属性集,将需要创建的属性设置进来使用一个封装类目录构造器结构接收并放回,以便于创建和更新使用*//**在这个目录构造器中同时要加入 ou o 等属性,他会保存到他的基本属性当中方便日后返回实体时查找*/ protected void mapToContext(Person person, DirContextOperations context) {context.setAttributeValues(“objectclass”, new String[] {“top”, “person”});context.setAttributeValue(“cn”, person.getFullName());context.setAttributeValue(“sn”, person.getLastName());context.setAttributeValue(“description”, person.getDescription());}/**代理返回下面的类,映射用户信息,类似于上一章写到的查询时的第三个参数,获得的属性集合*/protected ContextMapper getContextMapper() {return new PersonContextMapper();}/**与上一章查询时的获取对象属性类一样,只不过这里取值时使用的是目录构造器获取值的方式,这里同时可以增加泛型,帮助文档的第六章有写到*/private static class PersonContextMapper extends AbstractContextMapper {public Object doMapFromContext(DirContextOperations context) {Person person = new Person();person.setFullName(context.getStringAttribute(“cn”));person.setLastName(context.getStringAttribute(“sn”));person.setDescription(context.getStringAttribute(“description”));return person;}}/**以上就是一些工具类 下面需要的用到的. 因为都实现过所以总结时,喜欢把他们都归类写好,当作已经封装好的方法等待调用就ok了,接下来就是简化第一张的方法*//**创建用户*/public void create(Person person) {/**实例化一个目录构造器,并把buildDn()方法返回的dn放入*/DirContextAdapter context = new DirContextAdapter(buildDn(person)); //构造dn放入目录构造器中mapToContext(person, context); //将用户的属性放入目录构造器中(上面有写到的目录构造器方法.)ldapTemplate.bind(context); //进行新增,用户的信息(例如:dn,属性集等)都在目录构造器context里.}/**更新用户信息*/public void update(Person person) {Name dn = buildDn(person);DirContextOperations context = ldapTemplate.lookupContext(dn);mapToContext(person, context);ldapTemplate.modifyAttributes(context);//进行更新,用户的信息(例如:dn和需要修改的属性集等)都在目录构造器context里.}/**删除用户信息 这个就没什么好说的了 拿到dn kill掉*/public void delete(Person person) {ldapTemplate.unbind(buildDn(person));}/**根据指定的dn查找到用户信息*/public Person findByPrimaryKey(String name, String company, String country) {Name dn = buildDn(name, company, country); //获得dnreturn (Person) ldapTemplate.lookup(dn, getContextMapper());//这里只有2个参数了 因为他现在是lookup方法,理解为去掉了中间那没用的object类型的null.}/**根据用户的cn值进行模糊查找,search+三个参数方式,就不多说了*/public List findByName(String name) {AndFilter filter = new AndFilter();filter.and(new EqualsFilter(“objectclass”, “person”)).and(new WhitespaceWildcardsFilter(“cn”,name));return ldapTemplate.search(DistinguishedName.EMPTY_PATH, filter.encode(), getContextMapper());}/**查找到所有的用户信息*/public List findAll() {EqualsFilter filter = new EqualsFilter(“objectclass”, “person”);return ldapTemplate.search(DistinguishedName.EMPTY_PATH, filter.encode(), getContextMapper());}}

以上的操作足够封装一个简单的DAO,为什么说简单的呢 因为还没有分页,很头疼的万应

(未完待续……下篇预告[LDAP分页操作帮助第五章])

本文出自 “记下就对了” 博客,请务必保留此出处

,服务器空间,美国服务器,服务器空间学习会使你永远立于不败之地。

解读Spring LDAP 帮助中的代码案例 (二)

相关文章:

你感兴趣的文章:

标签云: