探讨问题的同胞们可以加QQ群:315309006
直接上代码吧,懒得写那么多了:
<!-- 定义数据源 --> <context:property-placeholder location="classpath:ldap.properties"/> <bean id="ldapDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${ldap.driver}"></property> <property name="url" value="${ldap.url}"></property> <property name="username" value="${ldap.username}"></property> <property name="password" value="${ldap.password}"></property> </bean>
ldap.properties配置
ldap.driver=com.octetstring.jdbcLdap.sql.JdbcLdapDriver ldap.url=jdbc:ldap://localhost:389/dc=maxcrc,dc=com?SEARCH_SCOPE:=subTreeScope ldap.username=cn=Manager,dc=maxcrc,dc=com ldap.password=secret
mybatis采用注释映射方式提供:
public interface TestMapper { public HashMap selectOne() throws Exception; }
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.restful.ldap.mapper.TestMapper"> <select id="selectOne" parameterType="hashmap" resultType="hashmap"> SELECT * FROM ou=people,dc=maxcrc,dc=com WHERE uid=oracle2 </select> </mapper>
测试代码:
@Inject private TestMapper mapper; private Logger log = Logger.getLogger(this.getClass()); @Test public void execute() { try { HashMap map = mapper.selectOne(); for (Object key : map.keySet()) { log.info("\t" + key + " is " + map.get(key)); } } catch (Exception e) { log.error("", e); } }
测试结果:
[INFO]Loaded JDBC driver: com.octetstring.jdbcLdap.sql.JdbcLdapDriver (DriverManagerDataSource.java:153):setDriverClassName[org.springframework.jdbc.datasource.DriverManagerDataSource] [DEBUG]Logging initialized using 'org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl' adapter. (JakartaCommonsLoggingImpl.java:27):debug[org.apache.ibatis.logging.LogFactory] [DEBUG]ooo Connection Opened (JakartaCommonsLoggingImpl.java:27):debug[java.sql.Connection] Sort by : null [DEBUG]==> Executing: SELECT * FROM ou=people,dc=maxcrc,dc=com WHERE uid=oracle2 (JakartaCommonsLoggingImpl.java:27):debug[java.sql.PreparedStatement] [DEBUG]==> Parameters: (JakartaCommonsLoggingImpl.java:27):debug[java.sql.PreparedStatement] Is No Con : false [DEBUG]<== Columns: DN, userPassword, uid_0, uid_1, sn, cn, telephoneNumber, objectClass_0, objectClass_1 (JakartaCommonsLoggingImpl.java:27):debug[java.sql.ResultSet] [DEBUG]<== Row: uid=oracle2,ou=People,dc=maxcrc,dc=com, redqueen, oracle, oracle2, Lee, Amy Lee, +1 408 555 0033, uidObject, person (JakartaCommonsLoggingImpl.java:27):debug[java.sql.ResultSet] [INFO] sn is Lee (TestSpring.java:33):execute[com.ehofy.test.ldap.TestSpring] [INFO] DN is uid=oracle2,ou=People,dc=maxcrc,dc=com (TestSpring.java:33):execute[com.ehofy.test.ldap.TestSpring] [INFO] cn is Amy Lee (TestSpring.java:33):execute[com.ehofy.test.ldap.TestSpring] [INFO] telephoneNumber is +1 408 555 0033 (TestSpring.java:33):execute[com.ehofy.test.ldap.TestSpring] [INFO] userPassword is redqueen (TestSpring.java:33):execute[com.ehofy.test.ldap.TestSpring] [INFO] objectClass_1 is person (TestSpring.java:33):execute[com.ehofy.test.ldap.TestSpring] [INFO] uid_1 is oracle2 (TestSpring.java:33):execute[com.ehofy.test.ldap.TestSpring] [INFO] objectClass_0 is uidObject (TestSpring.java:33):execute[com.ehofy.test.ldap.TestSpring] [INFO] uid_0 is oracle (TestSpring.java:33):execute[com.ehofy.test.ldap.TestSpring]
探讨问题的同胞们可以加QQ群:315309006