设为首页 加入收藏

TOP

封装LDAP 增删改查 方法(一)
2014-11-24 02:58:05 来源: 作者: 【 】 浏览:0
Tags:封装 LDAP 删改 方法

忙了好久,有时间来整理下最近的知识了,首先封装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 返回查找的集合
*

首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇LDAP 与 MySQL 连用 的JPA事物问题 下一篇轻松了解LDAP的结构概念

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: