mybatis使用之association(二)

477 阅读1分钟

续(一):https://www.jianshu.com/p/3bced51ee642

建立一个简单的表

DROP TABLE IF EXISTS `emp`;
CREATE TABLE `emp` (
  `empno` int(10) NOT NULL AUTO_INCREMENT,
  `ename` varchar(30) NOT NULL,
  `deptno` int(10) DEFAULT NULL,
  PRIMARY KEY (`empno`)
) ENGINE=InnoDB AUTO_INCREMENT=7936 DEFAULT CHARSET=utf8mb4;

-- ----------------------------
-- Records of emp
-- ----------------------------
INSERT INTO `emp` VALUES ('1', 'a', '1');
INSERT INTO `emp` VALUES ('2', 'b', '1');
INSERT INTO `emp` VALUES ('3', 'c', '2');
INSERT INTO `emp` VALUES ('4', 'd', '2');
INSERT INTO `emp` VALUES ('5', 'e', '2');
INSERT INTO `emp` VALUES ('6', 'f', '2');
INSERT INTO `emp` VALUES ('7', 'g', '4');
INSERT INTO `emp` VALUES ('8', 'h', '3');

image.png
建立对应的pojo,这个pojo中加入了Dept,每一个Emp有一个对应的Dept,是一一对应关系。

package com.mybatis.pojo;

public class Emps {
    private int empno;
    private String ename;
    private int deptno;
    private Dept dept;
   省略了getter和setter

  @Override
    public String toString() {
        StringBuilder sb = new StringBuilder("" + empno + " " + ename + " " + deptno);
        sb.append(dept.toString());
        return sb.toString();
    }
}

在EmpMapper接口中添加方法

 Emp getEmpByEmpno(int empno);

修改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.mybatis.mapper.EmpMapper">
    <resultMap id="Emp_result_map" type="com.mybatis.pojo.Emp">
        <id column="empno" property="empno"/>
        <result column="ename" property="ename"/>
        <result column="deptno" property="deptno"/>
        <association property="dept" javaType="com.mybatis.pojo.Dept">
            <id column="deptno" property="deptno"/>
            <result column="dname" property="dname"/>
            <result column="location" property="location"/>
        </association>
    </resultMap>
    <select id="getEmpsByDeptno" resultType="com.mybatis.pojo.Emp" parameterType="_int">
       select * from emp where deptno=#{deptno}
   </select>

    <select id="getEmpByEmpno" resultMap="Emp_result_map" parameterType="_int">
           select
       e.empno,
       e.ename,
       e.deptno,
       d.deptno,
       d.dname,
       d.location
        from emp e join dept d
        on e.deptno=d.deptno
        where e.empno=#{empno};
    </select>
</mapper>

测试类

import com.mybatis.mapper.EmpMapper;
import com.mybatis.pojo.Emp;
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.BeforeClass;
import org.junit.Test;

import java.io.IOException;
import java.io.Reader;
import java.util.List;

public class EmpTest {
    private static SqlSessionFactory sqlSessionFactory=null;

    @BeforeClass
    public static void setUpClass() {
        try {
            String resource = "sqlMapConfig.xml";
            Reader reader = Resources.getResourceAsReader(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @Test
    public void testGetEmpByEmpno() {
        SqlSession session=sqlSessionFactory.openSession();
        EmpMapper mapper = session.getMapper(EmpMapper.class);
        Emp emp = mapper.getEmpByEmpno(1);
        System.out.println(emp.toString());
    }
}

测试结果

image.png