Spring Ldap 的增删改查
- package ldap.entity;
- /**
- * 本测试类person对象来自schema文件的core.schema文件
- * objectClass为person,必填属性和可选属性也是根据该对象得到的。
- * Author:Ding Chengyun
- */
- public class Person {
- private String sn; //必填属性
- private String cn; //必填属性
- private String userPassword; //可选属性
- private String telephoneNumber; //可选属性
- private String seeAlso; //可选属性
- private String description; //可选属性
- public String getSn() {
- return sn;
- }
- public void setSn(String sn) {
- this.sn = sn;
- }
- public String getCn() {
- return cn;
- }
- public void setCn(String cn) {
- this.cn = cn;
- }
- public String getUserPassword() {
- return userPassword;
- }
- public void setUserPassword(String userPassword) {
- this.userPassword = userPassword;
- }
- public String getTelephoneNumber() {
- return telephoneNumber;
- }
- public void setTelephoneNumber(String telephoneNumber) {
- this.telephoneNumber = telephoneNumber;
- }
- public String getSeeAlso() {
- return seeAlso;
- }
- public void setSeeAlso(String seeAlso) {
- this.seeAlso = seeAlso;
- }
- public String getDescription() {
- return description;
- }
- public void setDescription(String description) {
- this.description = description;
- }
- }
Entity 类
- package ldap.mapper;
- import javax.naming.NamingException;
- import javax.naming.directory.Attributes;
- import ldap.entity.Person;
- import org.springframework.ldap.core.AttributesMapper;
- /**
- * 这个类的作用是将ldap中的属性转化为实体类的属性值,
- * 在查询信息的时候会用到
- */
- public class PersonAttributeMapper implements AttributesMapper{
- @Override
- public Object mapFromAttributes(Attributes attr) throws NamingException {
- Person person = new Person();
- person.setSn((String)attr.get("sn").get());
- person.setCn((String)attr.get("cn").get());
- if (attr.get("userPassword") != null) {
- person.setUserPassword((String)attr.get("userPassword").get());
- }
- if (attr.get("telephoneNumber") != null) {
- person.setTelephoneNumber((String)attr.get("telephoneNumber").get());
- }
- if (attr.get("seeAlso") != null) {
- person.setSeeAlso((String)attr.get("seeAlso").get());
- }
- if (attr.get("description") != null) {
- person.setDescription((String)attr.get("description").get());
- }
- return person;
- }
- }
Mapper 类
- package ldap.dao;
- import java.util.ArrayList;
- import java.util.List;
- import javax.naming.directory.Attributes;
- import javax.naming.directory.BasicAttribute;
- import javax.naming.directory.BasicAttributes;
- import javax.naming.directory.DirContext;
- import javax.naming.directory.ModificationItem;
- import ldap.entity.Person;
- import ldap.mapper.PersonAttributeMapper;
- import org.springframework.ldap.NameNotFoundException;
- import org.springframework.ldap.core.DistinguishedName;
- import org.springframework.ldap.core.LdapTemplate;
- import org.springframework.ldap.filter.AndFilter;
- import org.springframework.ldap.filter.EqualsFilter;
- import xhrd.ucenter.ldap.entity.UcenterLdapApplication;
- import xhrd.ucenter.ldap.ldapAttributeMappper.ApplicationAttributeMapper;
- /**
- * Description: 此类的作用是使用spring的 LdapTemplate完成对ldap的增删改查的操作
- * Author:Ding Chengyun
- */
- public class PersonDao {
- //注入spring的LdapTemplate,此处在spring的配置文件中需要配置
- private LdapTemplate ldapTemplate;
- public LdapTemplate getLdapTemplate() {
- return ldapTemplate;
- }
- public void setLdapTemplate(LdapTemplate ldapTemplate) {
- this.ldapTemplate = ldapTemplate;
- }
- /**
- * 添加 一条记录
- * @param person
- */
- public void createOnePerson(Person person) {
- BasicAttribute ba = new BasicAttribute("objectclass");
- ba.add("person"); //此处的person对应的是core.schema文件中的objectClass:person
- Attributes attr = new BasicAttributes();
- attr.put(ba);
- //必填属性,不能为null也不能为空字符串
- attr.put("cn", person.getCn());
- attr.put("sn", person.getSn());
- //可选字段需要判断是否为空,如果为空则不能添加
- if (person.getDescription() != null
- && person.getDescription().length() > 0) {
- attr.put("description", person.getDescription());
- }
- if (person.getUserPassword() != null
- && person.getUserPassword().length() > 0) {
- attr.put("userPassword", person.getUserPassword());
- }
- if (person.getSeeAlso() != null
- && person.getSeeAlso().length() > 0) {
- attr.put("seeAlso", person.getSeeAlso());
- }
- if (person.getTelephoneNumber() != null
- && person.getTelephoneNumber().length() > 0) {
- attr.put("telephoneNumber", person.getTelephoneNumber());
- }
- //bind方法即是添加一条记录。
- ldapTemplate.bind(getDn(person.getCn()), null, attr);
- }
- /**
- /**
- * 根据dn查询详细信息
- * @param cn
- * @return
- */
- public UcenterLdapApplication getPersonDetail(String cn) {
- try {
- //ldapTeplate的lookup方法是根据dn进行查询,此查询的效率超高
- UcenterLdapApplication ua = (UcenterLdapApplication)
- ldapTemplate.lookup(getDn(cn),
- new ApplicationAttributeMapper());
- return ua;
- } catch (NameNotFoundException e) {
- return null;
- }
- }
- /**
- * 根据自定义的属性值查询person列表
- * @param person
- * @return
- */
- public List<Person> getPersonList(
- Person person) {
- List<Person> list = new ArrayList<Person>();
- //查询过滤条件
- AndFilter andFilter = new AndFilter();
- andFilter.and(new EqualsFilter("objectclass", "person"));
- if (person.getCn() != null
- && person.getCn().length() > 0) {
- andFilter.and(new EqualsFilter("cn", person.getCn()));
- }
- if (person.getSn() != null
- && person.getSn().length() > 0) {
- andFilter.and(new EqualsFilter("sn", person.getSn()));
- }
- if (person.getDescription() != null
- && person.getDescription().length() > 0) {
- andFilter.and(new EqualsFilter("description", person.getDescription()));
- }
- if (person.getUserPassword() != null
- && person.getUserPassword().length() > 0) {
- andFilter.and(new EqualsFilter("userPassword", person.getUserPassword()));
- }
- if (person.getSeeAlso() != null
- && person.getSeeAlso().length() > 0) {
- andFilter.and(new EqualsFilter("seeAlso", person.getSeeAlso()));
- }
- if (person.getTelephoneNumber() != null
- && person.getTelephoneNumber().length() > 0) {
- andFilter.and(new EqualsFilter("telephoneNumber", person.getTelephoneNumber()));
- }
- //search是根据过滤条件进行查询,第一个参数是父节点的dn,可以为空,不为空时查询效率更高
- list = ldapTemplate.search("", andFilter.encode(),
- new PersonAttributeMapper());
- return list;
- }
- /**
- * 删除一条记录,根据dn
- * @param cn
- */
- public void removeOnePerson(String cn) {
- ldapTemplate.unbind(getDn(cn));
- }
- /**
- * 修改操作
- * @param person
- */
- public void updateOnePerson(Person person) {
- if (person == null || person.getCn() == null
- || person.getCn().length() <= 0) {
- return;
- }
- List<ModificationItem> mList = new ArrayList<ModificationItem>();
- mList.add(new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
- new BasicAttribute("sn",person.getSn())));
- mList.add(new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
- new BasicAttribute("description",person.getDescription())));
- mList.add(new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
- new BasicAttribute("seeAlso",person.getSeeAlso())));
- mList.add(new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
- new BasicAttribute("telephoneNumber",person.getTelephoneNumber())));
- mList.add(new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
- new BasicAttribute("userPassword",person.getUserPassword())));
- if (mList.size() > 0) {
- ModificationItem[] mArray = new ModificationItem[mList.size()];
- for (int i = 0; i < mList.size(); i++) {
- mArray[i] = mList.get(i);
- }
- //modifyAttributes 方法是修改对象的操作,与rebind()方法需要区别开
- ldapTemplate.modifyAttributes(this.getDn(person.getCn()), mArray);
- }
- }
- /**
- * 得到dn
- * @param cn
- * @return
- */
- private DistinguishedName getDn(String cn) {
- //得到根目录,也就是配置文件中配置的ldap的根目录
- DistinguishedName newContactDN = new DistinguishedName();
- // 添加cn,即使得该条记录的dn为"cn=cn,根目录",例如"cn=abc,dc=testdc,dc=com"
- newContactDN.add("cn", cn);
- return newContactDN;
- }
- }
DAO 层
Spring Ldap 的增删改查的更多相关文章
- idea社区版+第一个spring boot项目+增删改查+yml修改端口号
参考:https://www.cnblogs.com/tanlei-sxs/p/9855071.html 中途出现问题时参考了太多 1.下载idea社区版 2.在settings -> Plug ...
- Spring JPA实现增删改查
1. 创建一个Spring工程 2.配置application文件 spring.datasource.driver-class-name= com.mysql.cj.jdbc.Driver spri ...
- Spring Data CrudRepository增删改查方法(八)
CrudRepository 的主要方法 long count(); boolean exists(Integer arg0); <S extends StudentPO> S sav ...
- spring mvc hibernate spring 整合的增删改查+后台校验+bootstrap
整合之前先知道大概的思路,首先要知道每个框架的重点要点. 1.首先我们从数据库开始 --创建数据库 create database gs --创建表 create table food ( id ,) ...
- Spring Boot WebFlux 增删改查完整实战 demo
03:WebFlux Web CRUD 实践 前言 上一篇基于功能性端点去创建一个简单服务,实现了 Hello .这一篇用 Spring Boot WebFlux 的注解控制层技术创建一个 CRUD ...
- springLdap 操作ldap示例(增删改查)
转自:http://blog.csdn.net/sundenskyqq/article/details/9002440 这部分的示例网上的确有很多,但是个人在查找的过程中还是感到不够满意,所以就自己总 ...
- EasyUI + Spring MVC + hibernate实现增删改查导入导出
(这是一个故事--) 前言 作为一个JAVA开发工程师,我觉得最基本是需要懂前端.后台以及数据库. 练习的内容很基础,包括:基本增删改查.模糊查询.分页查询.树菜单.上传下载.tab页 主管发我一个已 ...
- Struts2+Spring+Hibernate实现员工管理增删改查功能(一)之ssh框架整合
前言 转载请标明出处:http://www.cnblogs.com/smfx1314/p/7795837.html 本项目是我写的一个练习,目的是回顾ssh框架的整合以及使用.项目介绍: ...
- SSM框架搭建(Spring+SpringMVC+MyBatis)与easyui集成并实现增删改查实现
一.用myEclipse初始化Web项目 新建一个web project: 二.创建包 controller //控制类 service //服务接口 service.impl //服务 ...
随机推荐
- [置顶] Android访问控制系统测试与评估
5.1实验方案 通过以上章节,本文阐述了目前Android平台上的恶意软件以“隐私窃取”和“恶意扣费”类为主,本研究课题访问控制的目标也正是阻止恶意软件“隐私窃取”和“恶意扣费”的行为,因此,本实验方 ...
- Ubuntu安装配置Qt环境
安装 QT4.8.6库+QT Creator 2.4.1 下载地址发布 QT4.8.6库 http://mirrors.hustunique.com/qt/official_releases/qt/ ...
- Python的学习
1.psutil的安装 [root@YQY-TIAN- ~]# wget https://pypi.python.org/packages/source/p/psutil/psutil-2.0.0.t ...
- openwrt interface
orige : http://www.cnblogs.com/preorder69/p/3959187.html 这篇算是对openwrt网络接口的一个翻译吧,源地址:http://wiki.open ...
- asp.net动态设置button的Text,Enabled属性,向后台传递参数
前台代码:根据后台传递过来的参数动态设置 <asp:Button ID="Button1" runat="server" CommandArgument= ...
- Linux最小化安装后配置网络
vi /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0HWADDR=00:0C:29:DD:AD:B9TYPE=EthernetUUID=d7 ...
- cocos2dx Sprite的多种创建方法
1.通过文件创建 Sprite *bg = Sprite::create("backGround.jpg"); 2.通过图片的某个区域创建 SpriteFrame *frame = ...
- NOIP201504推销员
#include<iostream> #include<cstring> #include<algorithm> #include<cmath> #in ...
- CSS实现三角形效果
类似三角形的形状的元素在网页中可能会用到,我们可以用图片或者CSS元素达到我们想要的效果.这里讲一下是讲自己使用HTML+CSS实现三角形的方式. 为了能够熟悉的使用HTML+CSS构建三角形,我们首 ...
- HTML&CSS基础学习笔记1.27-input提交数据
提交数据 我们在表单上填的信息很多情况下需要提交到后台. <input>的[type]属性值为“submit”时,表示提交表单数据.它在页面的表现形式也是个按钮,点击该按钮,表单数据将会提 ...