1. package ldap.entity;
  2.  
  3. /**
  4. * 本测试类person对象来自schema文件的core.schema文件
  5. * objectClass为person,必填属性和可选属性也是根据该对象得到的。
  6. * Author:Ding Chengyun
  7. */
  8. public class Person {
  9.  
  10. private String sn; //必填属性
  11. private String cn; //必填属性
  12.  
  13. private String userPassword; //可选属性
  14. private String telephoneNumber; //可选属性
  15. private String seeAlso; //可选属性
  16. private String description; //可选属性
  17. public String getSn() {
  18. return sn;
  19. }
  20. public void setSn(String sn) {
  21. this.sn = sn;
  22. }
  23. public String getCn() {
  24. return cn;
  25. }
  26. public void setCn(String cn) {
  27. this.cn = cn;
  28. }
  29. public String getUserPassword() {
  30. return userPassword;
  31. }
  32. public void setUserPassword(String userPassword) {
  33. this.userPassword = userPassword;
  34. }
  35. public String getTelephoneNumber() {
  36. return telephoneNumber;
  37. }
  38. public void setTelephoneNumber(String telephoneNumber) {
  39. this.telephoneNumber = telephoneNumber;
  40. }
  41. public String getSeeAlso() {
  42. return seeAlso;
  43. }
  44. public void setSeeAlso(String seeAlso) {
  45. this.seeAlso = seeAlso;
  46. }
  47. public String getDescription() {
  48. return description;
  49. }
  50. public void setDescription(String description) {
  51. this.description = description;
  52. }
  53.  
  54. }

Entity 类

  1. package ldap.mapper;
  2.  
  3. import javax.naming.NamingException;
  4. import javax.naming.directory.Attributes;
  5.  
  6. import ldap.entity.Person;
  7.  
  8. import org.springframework.ldap.core.AttributesMapper;
  9.  
  10. /**
  11. * 这个类的作用是将ldap中的属性转化为实体类的属性值,
  12. * 在查询信息的时候会用到
  13. */
  14. public class PersonAttributeMapper implements AttributesMapper{
  15.  
  16. @Override
  17. public Object mapFromAttributes(Attributes attr) throws NamingException {
  18. Person person = new Person();
  19. person.setSn((String)attr.get("sn").get());
  20. person.setCn((String)attr.get("cn").get());
  21.  
  22. if (attr.get("userPassword") != null) {
  23. person.setUserPassword((String)attr.get("userPassword").get());
  24. }
  25. if (attr.get("telephoneNumber") != null) {
  26. person.setTelephoneNumber((String)attr.get("telephoneNumber").get());
  27. }
  28. if (attr.get("seeAlso") != null) {
  29. person.setSeeAlso((String)attr.get("seeAlso").get());
  30. }
  31. if (attr.get("description") != null) {
  32. person.setDescription((String)attr.get("description").get());
  33. }
  34. return person;
  35. }
  36.  
  37. }

Mapper 类

  1. package ldap.dao;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. import javax.naming.directory.Attributes;
  7. import javax.naming.directory.BasicAttribute;
  8. import javax.naming.directory.BasicAttributes;
  9. import javax.naming.directory.DirContext;
  10. import javax.naming.directory.ModificationItem;
  11.  
  12. import ldap.entity.Person;
  13. import ldap.mapper.PersonAttributeMapper;
  14.  
  15. import org.springframework.ldap.NameNotFoundException;
  16. import org.springframework.ldap.core.DistinguishedName;
  17. import org.springframework.ldap.core.LdapTemplate;
  18. import org.springframework.ldap.filter.AndFilter;
  19. import org.springframework.ldap.filter.EqualsFilter;
  20.  
  21. import xhrd.ucenter.ldap.entity.UcenterLdapApplication;
  22. import xhrd.ucenter.ldap.ldapAttributeMappper.ApplicationAttributeMapper;
  23.  
  24. /**
  25. * Description: 此类的作用是使用spring的 LdapTemplate完成对ldap的增删改查的操作
  26. * Author:Ding Chengyun
  27. */
  28. public class PersonDao {
  29.  
  30. //注入spring的LdapTemplate,此处在spring的配置文件中需要配置
  31. private LdapTemplate ldapTemplate;
  32.  
  33. public LdapTemplate getLdapTemplate() {
  34. return ldapTemplate;
  35. }
  36. public void setLdapTemplate(LdapTemplate ldapTemplate) {
  37. this.ldapTemplate = ldapTemplate;
  38. }
  39.  
  40. /**
  41. * 添加 一条记录
  42. * @param person
  43. */
  44. public void createOnePerson(Person person) {
  45. BasicAttribute ba = new BasicAttribute("objectclass");
  46. ba.add("person"); //此处的person对应的是core.schema文件中的objectClass:person
  47. Attributes attr = new BasicAttributes();
  48. attr.put(ba);
  49. //必填属性,不能为null也不能为空字符串
  50. attr.put("cn", person.getCn());
  51. attr.put("sn", person.getSn());
  52.  
  53. //可选字段需要判断是否为空,如果为空则不能添加
  54. if (person.getDescription() != null
  55. && person.getDescription().length() > 0) {
  56. attr.put("description", person.getDescription());
  57. }
  58.  
  59. if (person.getUserPassword() != null
  60. && person.getUserPassword().length() > 0) {
  61. attr.put("userPassword", person.getUserPassword());
  62. }
  63. if (person.getSeeAlso() != null
  64. && person.getSeeAlso().length() > 0) {
  65. attr.put("seeAlso", person.getSeeAlso());
  66. }
  67. if (person.getTelephoneNumber() != null
  68. && person.getTelephoneNumber().length() > 0) {
  69. attr.put("telephoneNumber", person.getTelephoneNumber());
  70. }
  71.  
  72. //bind方法即是添加一条记录。
  73. ldapTemplate.bind(getDn(person.getCn()), null, attr);
  74. }
  75.  
  76. /**
  77.  
  78. /**
  79. * 根据dn查询详细信息
  80. * @param cn
  81. * @return
  82. */
  83. public UcenterLdapApplication getPersonDetail(String cn) {
  84. try {
  85. //ldapTeplate的lookup方法是根据dn进行查询,此查询的效率超高
  86. UcenterLdapApplication ua = (UcenterLdapApplication)
  87. ldapTemplate.lookup(getDn(cn),
  88. new ApplicationAttributeMapper());
  89. return ua;
  90. } catch (NameNotFoundException e) {
  91. return null;
  92. }
  93. }
  94.  
  95. /**
  96. * 根据自定义的属性值查询person列表
  97. * @param person
  98. * @return
  99. */
  100. public List<Person> getPersonList(
  101. Person person) {
  102. List<Person> list = new ArrayList<Person>();
  103. //查询过滤条件
  104. AndFilter andFilter = new AndFilter();
  105. andFilter.and(new EqualsFilter("objectclass", "person"));
  106.  
  107. if (person.getCn() != null
  108. && person.getCn().length() > 0) {
  109. andFilter.and(new EqualsFilter("cn", person.getCn()));
  110. }
  111. if (person.getSn() != null
  112. && person.getSn().length() > 0) {
  113. andFilter.and(new EqualsFilter("sn", person.getSn()));
  114. }
  115.  
  116. if (person.getDescription() != null
  117. && person.getDescription().length() > 0) {
  118. andFilter.and(new EqualsFilter("description", person.getDescription()));
  119. }
  120.  
  121. if (person.getUserPassword() != null
  122. && person.getUserPassword().length() > 0) {
  123. andFilter.and(new EqualsFilter("userPassword", person.getUserPassword()));
  124. }
  125. if (person.getSeeAlso() != null
  126. && person.getSeeAlso().length() > 0) {
  127. andFilter.and(new EqualsFilter("seeAlso", person.getSeeAlso()));
  128. }
  129. if (person.getTelephoneNumber() != null
  130. && person.getTelephoneNumber().length() > 0) {
  131. andFilter.and(new EqualsFilter("telephoneNumber", person.getTelephoneNumber()));
  132. }
  133. //search是根据过滤条件进行查询,第一个参数是父节点的dn,可以为空,不为空时查询效率更高
  134. list = ldapTemplate.search("", andFilter.encode(),
  135. new PersonAttributeMapper());
  136. return list;
  137. }
  138.  
  139. /**
  140. * 删除一条记录,根据dn
  141. * @param cn
  142. */
  143. public void removeOnePerson(String cn) {
  144. ldapTemplate.unbind(getDn(cn));
  145. }
  146.  
  147. /**
  148. * 修改操作
  149. * @param person
  150. */
  151. public void updateOnePerson(Person person) {
  152. if (person == null || person.getCn() == null
  153. || person.getCn().length() <= 0) {
  154. return;
  155. }
  156. List<ModificationItem> mList = new ArrayList<ModificationItem>();
  157.  
  158. mList.add(new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
  159. new BasicAttribute("sn",person.getSn())));
  160. mList.add(new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
  161. new BasicAttribute("description",person.getDescription())));
  162. mList.add(new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
  163. new BasicAttribute("seeAlso",person.getSeeAlso())));
  164. mList.add(new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
  165. new BasicAttribute("telephoneNumber",person.getTelephoneNumber())));
  166. mList.add(new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
  167. new BasicAttribute("userPassword",person.getUserPassword())));
  168.  
  169. if (mList.size() > 0) {
  170. ModificationItem[] mArray = new ModificationItem[mList.size()];
  171. for (int i = 0; i < mList.size(); i++) {
  172. mArray[i] = mList.get(i);
  173. }
  174. //modifyAttributes 方法是修改对象的操作,与rebind()方法需要区别开
  175. ldapTemplate.modifyAttributes(this.getDn(person.getCn()), mArray);
  176. }
  177. }
  178. /**
  179. * 得到dn
  180. * @param cn
  181. * @return
  182. */
  183. private DistinguishedName getDn(String cn) {
  184. //得到根目录,也就是配置文件中配置的ldap的根目录
  185. DistinguishedName newContactDN = new DistinguishedName();
  186. // 添加cn,即使得该条记录的dn为"cn=cn,根目录",例如"cn=abc,dc=testdc,dc=com"
  187. newContactDN.add("cn", cn);
  188. return newContactDN;
  189. }
  190. }

DAO 层

Spring Ldap 的增删改查的更多相关文章

  1. idea社区版+第一个spring boot项目+增删改查+yml修改端口号

    参考:https://www.cnblogs.com/tanlei-sxs/p/9855071.html 中途出现问题时参考了太多 1.下载idea社区版 2.在settings -> Plug ...

  2. Spring JPA实现增删改查

    1. 创建一个Spring工程 2.配置application文件 spring.datasource.driver-class-name= com.mysql.cj.jdbc.Driver spri ...

  3. Spring Data CrudRepository增删改查方法(八)

    CrudRepository   的主要方法 long count(); boolean exists(Integer arg0); <S extends StudentPO> S sav ...

  4. spring mvc hibernate spring 整合的增删改查+后台校验+bootstrap

    整合之前先知道大概的思路,首先要知道每个框架的重点要点. 1.首先我们从数据库开始 --创建数据库 create database gs --创建表 create table food ( id ,) ...

  5. Spring Boot WebFlux 增删改查完整实战 demo

    03:WebFlux Web CRUD 实践 前言 上一篇基于功能性端点去创建一个简单服务,实现了 Hello .这一篇用 Spring Boot WebFlux 的注解控制层技术创建一个 CRUD ...

  6. springLdap 操作ldap示例(增删改查)

    转自:http://blog.csdn.net/sundenskyqq/article/details/9002440 这部分的示例网上的确有很多,但是个人在查找的过程中还是感到不够满意,所以就自己总 ...

  7. EasyUI + Spring MVC + hibernate实现增删改查导入导出

    (这是一个故事--) 前言 作为一个JAVA开发工程师,我觉得最基本是需要懂前端.后台以及数据库. 练习的内容很基础,包括:基本增删改查.模糊查询.分页查询.树菜单.上传下载.tab页 主管发我一个已 ...

  8. Struts2+Spring+Hibernate实现员工管理增删改查功能(一)之ssh框架整合

    前言        转载请标明出处:http://www.cnblogs.com/smfx1314/p/7795837.html 本项目是我写的一个练习,目的是回顾ssh框架的整合以及使用.项目介绍: ...

  9. SSM框架搭建(Spring+SpringMVC+MyBatis)与easyui集成并实现增删改查实现

    一.用myEclipse初始化Web项目 新建一个web project: 二.创建包 controller        //控制类 service //服务接口 service.impl //服务 ...

随机推荐

  1. [置顶] Android访问控制系统测试与评估

    5.1实验方案 通过以上章节,本文阐述了目前Android平台上的恶意软件以“隐私窃取”和“恶意扣费”类为主,本研究课题访问控制的目标也正是阻止恶意软件“隐私窃取”和“恶意扣费”的行为,因此,本实验方 ...

  2. Ubuntu安装配置Qt环境

    安装 QT4.8.6库+QT Creator 2.4.1 下载地址发布 QT4.8.6库  http://mirrors.hustunique.com/qt/official_releases/qt/ ...

  3. Python的学习

    1.psutil的安装 [root@YQY-TIAN- ~]# wget https://pypi.python.org/packages/source/p/psutil/psutil-2.0.0.t ...

  4. openwrt interface

    orige : http://www.cnblogs.com/preorder69/p/3959187.html 这篇算是对openwrt网络接口的一个翻译吧,源地址:http://wiki.open ...

  5. asp.net动态设置button的Text,Enabled属性,向后台传递参数

    前台代码:根据后台传递过来的参数动态设置 <asp:Button ID="Button1" runat="server" CommandArgument= ...

  6. Linux最小化安装后配置网络

    vi /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0HWADDR=00:0C:29:DD:AD:B9TYPE=EthernetUUID=d7 ...

  7. cocos2dx Sprite的多种创建方法

    1.通过文件创建 Sprite *bg = Sprite::create("backGround.jpg"); 2.通过图片的某个区域创建 SpriteFrame *frame = ...

  8. NOIP201504推销员

    #include<iostream> #include<cstring> #include<algorithm> #include<cmath> #in ...

  9. CSS实现三角形效果

    类似三角形的形状的元素在网页中可能会用到,我们可以用图片或者CSS元素达到我们想要的效果.这里讲一下是讲自己使用HTML+CSS实现三角形的方式. 为了能够熟悉的使用HTML+CSS构建三角形,我们首 ...

  10. HTML&CSS基础学习笔记1.27-input提交数据

    提交数据 我们在表单上填的信息很多情况下需要提交到后台. <input>的[type]属性值为“submit”时,表示提交表单数据.它在页面的表现形式也是个按钮,点击该按钮,表单数据将会提 ...