书接上回 解读Spring LDAP帮助中的代码案例(一) ,上章,主要讲的操作的思想和方法,所以上一章是非常重要,如果上一章还没测底明白,最好还是先多看看上章或官方帮助.
接下来我门看下第三章.第三章的上班部是一些简单的桥梁,上来承上启下,所以感觉没必要解读,很鸡肋,跟上上章的思路看下面的代码,这个spring总结出来的完整类
01.
package
com.example.dao;
02.
import
java.util.List;
03.
import
javax.naming.Name;
04.
import
javax.naming.NamingException;
05.
import
javax.naming.directory.Attributes;
06.
import
org.springframework.ldap.core.AttributesMapper;
07.
import
org.springframework.ldap.core.ContextMapper;
08.
import
org.springframework.ldap.core.LdapTemplate;
09.
import
org.springframework.ldap.core.DirContextAdapter;
10.
import
org.springframework.ldap.core.support.DistinguishedName;
11.
import
org.springframework.ldap.filter.AndFilter;
12.
import
org.springframework.ldap.filter.EqualsFilter;
13.
import
org.springframework.ldap.filter.WhitespaceWildcardsFilter;
14.
/**用户DAO实现层*/
15.
public
class
PersonDaoImpl
implements
PersonDao {
16.
/**注入*/
17.
private
LdapTemplate ldapTemplate;
18.
public
void
setLdapTemplate(LdapTemplate ldapTemplate) {
19.
this
.ldapTemplate = ldapTemplate;
20.
}
21.
/**这个方法封装他的重载方法*/
22.
protected
Name buildDn(Person person) {
23.
return
buildDn(person.getFullname(), person.getCompany(), person.getCountry());
24.
}
25.
/**该方法放入了他的cn的值,ou的值和c的值,返回组建好的dn*/
26.
protected
Name buildDn(String fullname, String company, String country) {
27.
DistinguishedName dn =
new
DistinguishedName();
28.
dn.add(
"c"
, country);
29.
dn.add(
"ou"
, company);
30.
dn.add(
"cn"
, fullname);
31.
return
dn;
32.
}
33.
/**类似上一章在创建时封装的属性集,将需要创建的属性设置进来使用一个封装类目录构造器结构接收并放回,以便于创建和更新使用*/
34.
protected
void
mapToContext(Person person, DirContextOperations context) {
35.
context.setAttributeValues(
"objectclass"
,
new
String[] {
"top"
,
"person"
});
36.
context.setAttributeValue(
"cn"
, person.getFullName());
37.
context.setAttributeValue(
"sn"
, person.getLastName());
38.
context.setAttributeValue(
"description"
, person.getDescription());
39.
}
40.
/**代理返回下面的类,映射用户信息,类似于上一章写到的查询时的第三个参数,获得的属性集合*/
41.
protected
ContextMapper getContextMapper() {
42.
return
new
PersonContextMapper();
43.
}
44.
/**与上一章查询时的获取对象属性类一样,只不过这里取值时使用的是目录构造器获取值的方式,这里同时可以增加泛型,帮助文档的第六章有写到*/
45.
private
static
class
PersonContextMapper
extends
AbstractContextMapper {
46.
public
Object doMapFromContext(DirContextOperations context) {
47.
Person person =
new
Person();
48.
person.setFullName(context.getStringAttribute(
"cn"
));
49.
person.setLastName(context.getStringAttribute(
"sn"
));
50.
person.setDescription(context.getStringAttribute(
"description"
));
51.
return
person;
52.
}
53.
}
54.
/**以上就是一些工具类 下面需要的用到的. 因为都实现过所以总结时,喜欢把他们都归类写好,当作已经封装好的方法等待调用就ok了,接下来就是简化第一张的方法*/
55.
/**创建用户*/
56.
public
void
create(Person person) {
57.
/**实例化一个目录构造器,并把buildDn()方法返回的dn放入*/
58.
DirContextAdapter context =
new
DirContextAdapter(buildDn(person));
//构造dn放入目录构造器中
59.
mapToContext(person, context);
//将用户的属性放入目录构造器中(上面有写到的目录构造器方法.)
60.
ldapTemplate.bind(context);
//进行新增,用户的信息(例如:dn,属性集等)都在目录构造器context里.
61.
}
62.
/**更新用户信息*/
63.
public
void
update(Person person) {
64.
Name dn = buildDn(person);
65.
DirContextOperations context = ldapTemplate.lookupContext(dn);
66.
mapToContext(person, context);
67.
ldapTemplate.modifyAttributes(context);
//进行更新,用户的信息(例如:dn和需要修改的属性集等)都在目录构造器context里.
68.
}
69.
/**删除用户信息 这个就没什么好说的了 拿到dn kill掉*/
70.
public
void
delete(Person person) {
71.
ldapTemplate.unbind(buildDn(person));
72.
}
73.
/**根据指定的dn查找到用户信息*/
74.
public
Person findByPrimaryKey(String name, String company, String country) {
75.
Name dn = buildDn(name, company, country);
//获得dn
76.
return
(Person) ldapTemplate.lookup(dn, getContextMapper());
//这里只有2个参数了 因为他现在是lookup方法,理解为去掉了中间那没用的object类型的null.
77.
}
78.
/**根据用户的cn值进行模糊查找,search+三个参数方式,就不多说了*/
79.
public
List findByName(String name) {
80.
AndFilter filter =
new
AndFilter();
81.
filter.and(
new
EqualsFilter(
"objectclass"
,
"person"
)).and(
new
WhitespaceWildcardsFilter(
"cn"
,name));
82.
return
ldapTemplate.search(DistinguishedName.EMPTY_PATH, filter.encode(), getContextMapper());
83.
}
84.
/**查找到所有的用户信息*/
85.
public
List findAll() {
86.
EqualsFilter filter =
new
EqualsFilter(
"objectclass"
,
"person"
);
87.
return
ldapTemplate.search(DistinguishedName.EMPTY_PATH, filter.encode(), getContextMapper());
88.
}
89.
}