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=开发部]

    说明关联查询是成功的。

  • 相关阅读:
    Python 模块 itertools
    Python 字符串的encode与decode
    python 模块 hashlib(提供多个不同的加密算法)
    暴力尝试安卓gesture.key
    hdu 1300 Pearls(DP)
    hdu 1232 畅通工程(并查集)
    hdu 1856 More is better(并查集)
    hdu 1198 Farm Irrigation(并查集)
    hdu 3635 Dragon Balls(并查集)
    hdu 3038 How Many Answers Are Wrong(并查集)
  • 原文地址:https://www.cnblogs.com/xiximayou/p/12217470.html
Copyright © 2011-2022 走看看