mybatis 05 对一关联

概念:

一个员工对应一个部门
员工类中包含一个部门属性

案例:

配置文件
myBatis-conf.xml


    
        
        
    




        
        

deptMapper.xml




 


    
        
        
        
    
    

empMapper.xml

 





    
    
    
    
    






    







@Data
public class Dept {
    private int deptId;
    private String deptName;
    private String deptAddress;
}
@Data
public class Emp {
    private String empId;
    private String empName;
    private String empSex;
    private Dept dept;
}
public class UserDao {
    public List selectList (String name){
        List emps = null;
        SqlSession session = null;
        try {
            session = MybatisSessionFactory.getSession();
            emps = session.selectList("com.xxjqr.relation01.empMapper.selectEmpDeptList",name);
            session.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                MybatisSessionFactory.closeSession();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        
        return emps;
    }
}
public class TestUserDao {
    private UserDao userDao = new UserDao();
    
    @Test
    public void test(){
        List emps = null;
        emps = userDao.selectList("张无忌");
        System.out.println(emps);
    }
}

MybatisSessionFactory

你可能感兴趣的:(mybatis 05 对一关联)