zoukankan      html  css  js  c++  java
  • mybatis文件映射之关联查询初探(一)

    现在有一数据库mybatis,两张数据表

    tbl_department

    tbl_employee

    其中tbl_employee表中的d_id关联tbl_department表中的id字段

    Employee.java

    public class Employee {
        private Integer id;
        private String lastName;
        private String gender;
        private String email;
        Department dept;
    }

    Department.java

    public class Department {
        private Integer id;
        private String deptName;
    }

    (省略了getters和setters以及toString方法)

    EmployeeMapper.java

        public Employee getEmpAndDept(Integer id);

    EmployeeMapper.xml

        <resultMap type="com.gong.mybatis.bean.Employee" id="MySimpleMap">
            <id column="id" property="id"/>
            <result column="last_name" property="lastName"/>
            <result column="gender" property="gender"/>
            <result column="email" property="email"/>
            <result column="did" property="dept.id"/>
            <result column="dept_name" property="dept.deptName"/>
        </resultMap>
        <select id="getEmpAndDept" resultMap="MySimpleMap">
            SELECT e.id,e.last_name,e.gender,e.email,d.dept_name,d.id did 
            FROM tbl_employee e,tbl_department d
            WHERE e.d_id = d.id and e.id=#{id};
        </select>

    说明:可以支持级联属性映射,由于我们在select标签中为tbl_department中的id取了别名,因此可以直接在resultMap中对did进行级联映射。

    最后进行测试:

    package com.gong.mybatis.test;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Map;
    
    import org.apache.ibatis.io.Resources;
    import org.apache.ibatis.session.SqlSession;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    import org.junit.Test;
    
    import com.gong.mybatis.bean.Employee;
    import com.gong.mybatis.dao.EmployeeMapperPlus;
    
    public class TestMybatis2 {
        
        public SqlSessionFactory getSqlSessionFactory() throws IOException {
            String resource = "mybatis-config.xml";
            InputStream is = Resources.getResourceAsStream(resource);
            return new SqlSessionFactoryBuilder().build(is);
        }
    
        @Test
        public void test() throws IOException {
            SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
            SqlSession openSession = sqlSessionFactory.openSession();
            try {
                EmployeeMapperPlus mapper = openSession.getMapper(EmployeeMapperPlus.class);
                Employee employee = mapper.getEmpAndDept(3);
                System.out.println(employee);
                System.out.println(employee.getDept());
                openSession.commit();
            } finally {
                openSession.close();
            }
            
        }
    
    }

    输出:

    DEBUG 01-20 12:15:43,557 ==> Preparing: SELECT e.id,e.last_name,e.gender,e.email,d.dept_name,d.id did FROM tbl_employee e,tbl_department d WHERE e.d_id = d.id and e.id=?; (BaseJdbcLogger.java:145)
    DEBUG 01-20 12:15:43,611 ==> Parameters: 3(Integer) (BaseJdbcLogger.java:145)
    DEBUG 01-20 12:15:43,652 <== Total: 1 (BaseJdbcLogger.java:145)
    Employee [id=3, lastName=小红, gender=0, email=xiaohong@qq.com, dept=Department [id=1, deptName=开发部]]
    Department [id=1, deptName=开发部]

    说明关联查询是成功的。

  • 相关阅读:
    对于大规模机器学习的理解和认识
    Failed to initialize NVML: GPU access blocked by the operating system
    ubuntu 当中添加开机启动服务
    洛谷P2882 [USACO07MAR]面对正确的方式Face The Right Way(贪心)
    注意注意!
    洛谷P5092 [USACO2004OPEN]Cube Stacking 方块游戏 (带权并查集)
    loj10017. 「一本通 1.2 练习 4」传送带(三分套三分)
    POJ1475 Pushing Boxes(BFS套BFS)
    CF451E Devu and Flowers(组合数)
    POJ2311 Cutting Game(博弈论)
  • 原文地址:https://www.cnblogs.com/xiximayou/p/12217470.html
Copyright © 2011-2022 走看看