美文网首页
mybatis 07 双向关联

mybatis 07 双向关联

作者: 小小机器人 | 来源:发表于2016-10-17 15:10 被阅读60次
    概念:

    A类中包含B字段
    B类中包含A字段

    注意:

    双向关联会造成循环,我们要破解死循环
    如empMapper.xml如下所示

    <resultMap type="Emp" id="empResultMap">
    
        <id column="emp_id" property="empId"/>
        
        <result column="emp_name" property="empName"/>
        <result column="emp_sex" property="empSex"/>
        <association column="dept_id" property="dept" resultMap="com.xxjqr.relation01.deptMapper.deptResultMap"></association>
    </resultMap>
    

    deptMapper.xml如下所示

        <resultMap type="Dept" id="deptResultMap">
            <id column="dept_id" property="deptId"/>
            <result column="dept_name" property="deptName"/>
            <result column="dept_address" property="deptAddress"/>
            <!--collection用来配置对多的关联  -->
            <collection property="emps" resultMap="com.xxjqr.relation02.empMapper.empResultMap"></collection>
        </resultMap>
    

    可想而知我们在查询部门会同时去查询该部门的员工信息,那么查询该员工信息时又会去查该员工所属性部门的信息;查询员工时同理,双向的交叉查询,无修无止

    案例

    myBatis-conf.xml

    <typeAliases>
        <typeAlias type="com.xxjqr.relation03.Emp" alias="Emp"/>
        <typeAlias type="com.xxjqr.relation03.Dept" alias="Dept"/>
    </typeAliases>
    
    
    <mappers>
            <mapper resource="com/xxjqr/relation03/empMapper.xml"/>
            <mapper resource="com/xxjqr/relation03/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.relation03.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>
        
        <resultMap type="Dept" id="deptExtResultMap" extends="deptResultMap">
            <!--collection用来配置对多的关联  -->
            <collection property="emps" resultMap="com.xxjqr.relation03.empMapper.empResultMap"></collection>
        </resultMap>
        <!-- 注意:
            双方不要同时写xxExtResultMap,这样又会回到原始状态,造成循环
         -->
        
        <!-- 根据部门名称查询部门信息(包括部门员工信息) -->
        <select id="selectDeptEmpList" parameterType="string" resultMap="deptExtResultMap">
            select e.*,d.* from emp_t e inner join dept_t d on e.dept_id = d.dept_id
            where d.dept_name = #{deptName} 
        </select>
    </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.relation03.empMapper">
    
        <resultMap type="Emp" id="empResultMap">
        
            <id column="emp_id" property="empId"/>
            <result column="emp_name" property="empName"/>
            <result column="emp_sex" property="empSex"/>
        
        </resultMap>
        
        <resultMap type="Emp" id="empExtResultMap" extends="empResultMap">
                <association column="dept_id" property="dept" resultMap="com.xxjqr.relation03.deptMapper.deptResultMap"></association>
        </resultMap>
        
        <select id="selectEmpDeptList" parameterType="string" resultMap="empExtResultMap" >
            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;
        private List<Emp> emps;
    }
    
    @Data
    public class Emp {
        private String empId;
        private String empName;
        private String empSex;
        private Dept dept;
    }
    
    
    public class UserDao {
        /**
         * 根据部门名称来查询部门*/
        public List<Dept> selectDeptList (String name){
            List<Dept> depts = null;
            SqlSession session = null;
            try {
                session = MybatisSessionFactory.getSession();
                depts = session.selectList("com.xxjqr.relation03.deptMapper.selectDeptEmpList",name);            
                session.commit();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    MybatisSessionFactory.closeSession();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            
            return depts;
        }
        
        
        /**
         * 根据用户名查询用户*/
        public List<Emp> selectEmpList (String name){
            List<Emp> emps = null;
            SqlSession session = null;
            try {
                session = MybatisSessionFactory.getSession();
                emps = session.selectList("com.xxjqr.relation03.empMapper.selectEmpDeptList",name);
            } 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<Dept> depts = null;
            depts = userDao.selectDeptList("开发部");
            System.out.println(depts);
        }
        
        @Test
        public void test2(){
            List<Emp> emps = null;
            emps = userDao.selectEmpList("杨过");
            System.out.println(emps);
        }
    }
    

    MyBatisSessionFactory

    相关文章

      网友评论

          本文标题:mybatis 07 双向关联

          本文链接:https://www.haomeiwen.com/subject/vutzyttx.html