忙了好久,有时间来整理下最近的知识了,首先封装LDAP的方法,让他可以已实体的方式来进行操作,思路类似于JPA 传入传出最好是实体,在实体里 使用map当作注解映射。
推荐阅读:
下面 先上2个代码
第一个封装一个baseDao;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.naming.Name;
import javax.naming.directory.SearchControls;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ldap.control.PagedResultsCookie;
import org.springframework.ldap.control.PagedResultsDirContextProcessor;
import org.springframework.ldap.core.ContextMapper;
import org.springframework.ldap.core.DirContextAdapter;
import org.springframework.ldap.core.DirContextOperations;
import org.springframework.ldap.core.DistinguishedName;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.core.simple.ParameterizedContextMapper;
import org.springframework.ldap.filter.AndFilter;
import org.springframework.ldap.filter.EqualsFilter;
import org.springframework.ldap.filter.WhitespaceWildcardsFilter;
/**
* Dao基层
* @创建人 PengBo
* @创建时间 2013-7-7 下午7:03:49
*/
@SuppressWarnings("unchecked")
public class BaseDAO< T extends BaseModel> {
@Autowired
private LdapTemplate ldapTemplate;
/**
* 创建实体
* @param t
* @创建人 PengBo
* @创建时间 2013-7-7 下午6:53:40
*/
public void create(T t) {
DirContextAdapter context = new DirContextAdapter(buildDn(t));
mapToContext(t.getMap(), context);
ldapTemplate.bind(context);
}
/**
* 更新实体
* @param t
* @创建人 PengBo
* @创建时间 2013-7-7 下午6:53:56
*/
public void update(T t) {
DirContextOperations context = ldapTemplate.lookupContext(buildDn(t));
mapToContext(t.getMap(), context);
ldapTemplate.modifyAttributes(context);
}
/**
* 创建更新时使用的属性存储构造器
* @param map
* @param context
* @创建人 PengBo
* @创建时间 2013-7-7 下午6:54:16
*/
private void mapToContext(Map map, DirContextOperations context) {
context.setAttributeva lue("objectclass", "top");
for(String mkey:map.keySet()){ //获得实体的属性和对应的值 加入属性构造器中
if(map.get(mkey) != null)
context.setAttributeva lue(mkey, map.get(mkey));
}
}
/**
* 删除属性
* @param t
* @创建人 PengBo
* @创建时间 2013-7-7 下午6:54:46
*/
public void delete(T t) {
ldapTemplate.unbind(buildDn(t));
}
/**
* 根据唯一DN进行查找
* @param t
* @return
* @创建人 PengBo
* @创建时间 2013-7-7 下午6:55:02
*/
public T findByPrimaryKey(final T t) {
return (T) ldapTemplate.lookup(buildDn(t), getContextMapper(t));
}
/**
* 根据dn直接获取实体信息
* @param t
* @param dn
* @return
* @创建人 PengBo
* @创建时间 2013-7-29 下午4:39:59
*/
public T findByDN(final T t,String dn) {
return (T) ldapTemplate.lookup(dn, getContextMapper(t));
}
/**
* 根据实体条件精确查找进行查找
* @param t
* @return 返回查找的集合
* @创建人 PengBo
* @创建时间 2013-7-7 下午6:55:38
*/
public List findByEqualFilter( final T t) {
return ldapTemplate.search(buildDn(t), getEqualFilter(t).encode(), getContextMapper(t));
}
/**
* 根据实体条件进行查找
* @param t
* @return 返回查找的集合
*