mybatis 05 对一关联

2016-10-17  本文已影响9人  小小机器人
概念:

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

案例:

配置文件
myBatis-conf.xml

<!--别名-->
    <typeAliases>
        <typeAlias type="com.xxjqr.relation01.Emp" alias="Emp"/>
        <typeAlias type="com.xxjqr.relation01.Dept" alias="Dept"/>
    </typeAliases>

<!---关联配置文件-->
<mappers>

        <mapper resource="com/xxjqr/relation01/empMapper.xml"/>
        <mapper resource="com/xxjqr/relation01/deptMapper.xml"/>
</mappers>

deptMapper.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.xxjqr.relation01.deptMapper">

    <resultMap type="Dept" id="deptResultMap">
        <id column="dept_id" property="deptId"/>
        <result column="dept_name" property="deptName"/>
        <result column="dept_address" property="deptAddress"/>
    </resultMap>
    
</mapper>

empMapper.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.xxjqr.relation01.empMapper">

<resultMap type="Emp" id="empResultMap">

    <id column="emp_id" property="empId"/>
    
    <result column="emp_name" property="empName"/>
    <result column="emp_sex" property="empSex"/>
    <!-- 也可以使用javaType的方式,直接在当前配置文件中配置部门字段映射 -->
<!--    <association property="dept" javaType="com.xxjqr.relation01.Dept"> -->
<!--        <id column="dept_id" property="deptId"/> -->
<!--        <result column="dept_name" property="deptName"/> -->
<!--        <result column="dept_address" property="deptAddress"/> -->
<!--    </association> -->

    <association column="dept_id" property="dept" resultMap="com.xxjqr.relation01.deptMapper.deptResultMap"></association>
</resultMap>

<!-- sql查询语句;parameterType:查询参数类型;resultMap:返回结果类型 -->
<select id="selectEmpDeptList" parameterType="string" resultMap="empResultMap" >
    select e.*,d.* from emp_t e inner join dept_t d on e.dept_id = d.dept_id
    where e.emp_name = #{empName}
</select>

</mapper>

@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<Emp> selectList (String name){
        List<Emp> 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<Emp> emps = null;
        emps = userDao.selectList("张无忌");
        System.out.println(emps);
    }
}

MybatisSessionFactory

上一篇下一篇

猜你喜欢

热点阅读