EmployeeMapper.xml
<?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.monkey1024.dao.EmployeeDao">
<!-- 递归查询 -->
<resultMap id="leaderMap" type="employee">
<id column ="id" property="id"/>
<association property="employee" javaType="employee" select="selectLeaderByPid" column="mgr"/>
</resultMap>
<select id="selectLeaderByPid" resultMap="leaderMap">
select id,name,job,mgr
from t_employee
where id=#{id}
</select>
</mapper>
EmployeeDao.java
package com.monkey1024.dao;
import java.util.List;
import com.monkey1024.bean.Employee;
public interface EmployeeDao {
Employee selectLeaderByPid(int mgr);
}
EmployeeTest01.java
package com.monkey1024.test;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.monkey1024.bean.Employee;
import com.monkey1024.dao.EmployeeDao;
import com.monkey1024.util.MyBatisUtil;
public class EmployeeTest01 {
private SqlSession sqlSession;
private EmployeeDao employeeDao;
@Before
public void init(){
sqlSession = MyBatisUtil.getSqlSession();
employeeDao = sqlSession.getMapper(EmployeeDao.class);
}
@After
public void closeSession(){
if(sqlSession!=null){
sqlSession.close();
}
}
@Test
public void selectLeaderByPid(){
Employee employee = employeeDao.selectLeaderByPid(1005);
System.out.println(employee);
}
}