MyBatis查询结果处理

一直使用mybatis-plus和jpa,好久没有使用只集成mybatis的项目,这里记录一下,方便以后快速使用

使用上次创建的项目,地址:SpringBoot集成MyBatis

在mybatis中经常使用到1对多,多对1的查询,往往会把查询结果进行聚合,例如查询雇员和所在的公司,或者查询公司和公司雇员信息,会用到mybatis的xml关键字associationcollection,我们来看看他们的使用。

1、初始化

创建雇员表和公司表

DROP TABLE IF EXISTS `corporation`;
CREATE TABLE `corporation`  (
  `id` int(10) NOT NULL,
  `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of corporation
-- ----------------------------
INSERT INTO `corporation` VALUES (1, '阿里巴巴');
INSERT INTO `corporation` VALUES (2, '美团');

-- ----------------------------
-- Table structure for employ
-- ----------------------------
DROP TABLE IF EXISTS `employ`;
CREATE TABLE `employ`  (
  `id` int(10) NOT NULL,
  `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  `c_id` int(10) NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of employ
-- ----------------------------
INSERT INTO `employ` VALUES (1, '张三', 1);
INSERT INTO `employ` VALUES (2, '李四', 2);
INSERT INTO `employ` VALUES (3, '王五', 2);
INSERT INTO `employ` VALUES (4, '赵六', 1);
INSERT INTO `employ` VALUES (5, '孙七', 1);
INSERT INTO `employ` VALUES (6, '周八', 1);

创建corporationemploy实体类xml,代码如下

Corporation实体类

@Data
public class Corporation implements Serializable {
    /**
     * 
     */
    private Integer id;

    /**
     * 
     */
    private String name;

    private static final long serialVersionUID = 1L;
}

Employ实体类

@Data
public class Employ implements Serializable {
    /**
     * 
     */
    private Integer id;

    /**
     * 
     */
    private String name;

    /**
     * 
     */
    private Integer cId;

    private static final long serialVersionUID = 1L;
}

EmployMapper

public interface EmployMapper {

}

CorporationMapper

public interface CorporationMapper {

}

CorporationMapper.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="cn.amoqi.cloud.mapper.CorporationMapper">

    <resultMap id="BaseResultMap" type="cn.amoqi.cloud.entity.Corporation">
            <id property="id" column="id" jdbcType="INTEGER"/>
            <result property="name" column="name" jdbcType="VARCHAR"/>
    </resultMap>

</mapper>

EmployMapper.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="cn.amoqi.cloud.mapper.EmployMapper">

    <resultMap id="BaseResultMap" type="cn.amoqi.cloud.entity.Employ">
            <id property="id" column="id" jdbcType="INTEGER"/>
            <result property="name" column="name" jdbcType="VARCHAR"/>
            <result property="cId" column="c_id" jdbcType="INTEGER"/>
    </resultMap>
</mapper>

2、一对一查询、多对一查询

查询雇员列表、里面包含公司的信息

创建结果类EmployCorporation.java,内容如下

@Data
public class EmployCorporation {
    /**
     *
     */
    private Integer id;

    /**
     *
     */
    private String name;

    /**
     *
     */
    private Integer cId;

    private Corporation cName;
}

2.1 按照查询嵌套处理

非关联查询处理,EmployMapper.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="cn.amoqi.cloud.mapper.EmployMapper">

    <resultMap id="BaseResultMap" type="cn.amoqi.cloud.entity.Employ">
            <id property="id" column="id" jdbcType="INTEGER"/>
            <result property="name" column="name" jdbcType="VARCHAR"/>
            <result property="cId" column="c_id" jdbcType="INTEGER"/>
    </resultMap>

    <select id="selectEmployCorporation" resultMap="EmployCorporationResultMap">
        select * from employ
    </select>
    
    
    <resultMap id="EmployCorporationResultMap" type="cn.amoqi.cloud.pojo.EmployCorporation">
        <result property="id" column="id"></result>
        <result property="name" column="name"></result>
        <result property="cId" column="c_id"></result>
        <association property="corporation" column="c_id" javaType="Corporation" select="selectCorporationById" />
    </resultMap>

    <select id="selectCorporationById" resultType="Corporation">
        select * from corporation where id = #{cId}
    </select>
</mapper>

EmployMapper文件

public interface EmployMapper {
    List<EmployCorporation> selectEmployCorporation();
}

测试类,执行后可以正常打印信息

@SpringBootTest
class MybatisCreateApplicationTests {

    @Autowired
    private EmployMapper employMapper;

    @Test
    public void test1(){
        List<EmployCorporation> employCorporations = employMapper.selectEmployCorporation();
        employCorporations.forEach(System.out::println);
    }
}

2.2 按照结果嵌套处理

2.1 为非关联查询,也可以使用join等关联进行查询

在EmployMapper.xml加入以下关联查询

    <select id="selectEmployCorporation2" resultMap="EmployCorporationResultMap2">
        select e.id id,e.name name,e.c_id cId,c.name cName from employ e,corporation c where e.c_id = c.id
    </select>
    <resultMap id="EmployCorporationResultMap2" type="cn.amoqi.cloud.pojo.EmployCorporation">
        <result property="id" column="id"></result>
        <result property="name" column="name"></result>
        <result property="cId" column="cId"></result>
        <association property="corporation" javaType="Corporation">
            <result property="id" column="cId"></result>
            <result property="name" column="cName"></result>
        </association>
    </resultMap>

在EmployMapper.java 中加入接口方法

List<EmployCorporation> selectEmployCorporation2();

查询测试

    @Test
    public void test2(){
        List<EmployCorporation> employCorporations = employMapper.selectEmployCorporation2();
        employCorporations.forEach(System.out::println);
    }

3、一对多查询

创建CorporationEmploy类,如下

@Data
public class CorporationEmploy {
    /**
     *
     */
    private Integer id;

    /**
     *
     */
    private String name;

    private List<Employ> employs;
}

3.1 按照查询嵌套处理

CorporationMapper.xml

    <select id="selectCorporationEmploy" resultMap="CorporationEmployResultMap">
        select * from corporation
    </select>

    <resultMap id="CorporationEmployResultMap" type="cn.amoqi.cloud.pojo.CorporationEmploy">
        <result property="id" column="id"></result>
        <result property="name" column="name"></result>
        <collection property="employs" column="id" javaType="ArrayList" ofType="cn.amoqi.cloud.entity.Employ" select="selectEmployListOfCorporation"/>
    </resultMap>

    <select id="selectEmployListOfCorporation" resultType="cn.amoqi.cloud.entity.Employ">
        select id,name,c_id cId from employ where c_id = #{id}
    </select>

CorporationMapper.java

public interface CorporationMapper {
    List<CorporationEmploy> selectCorporationEmploy();
}

测试

    @Test
    public void test3(){
        List<CorporationEmploy> corporationEmploys = corporationMapper.selectCorporationEmploy();
        System.out.println(JSON.toJSONString(corporationEmploys));
    }

3.2 按照结果嵌套处理

CorporationMapper.xml

<select id="selectCorporationEmploy2" resultMap="CorporationEmployResultMap2">
        select c.id id,c.name name,e.id eId,e.name eName from corporation c left join employ e on c.id = e.c_id
    </select>
    <resultMap id="CorporationEmployResultMap2" type="cn.amoqi.cloud.pojo.CorporationEmploy">
        <result property="id" column="id"></result>
        <result property="name" column="name"></result>
        <collection property="employs" ofType="cn.amoqi.cloud.entity.Employ">
            <result property="id" column="eId"></result>
            <result property="name" column="eName"></result>
        </collection>
    </resultMap>

CorporationMapper.java

List<CorporationEmploy> selectCorporationEmploy2();

测试

    @Test
    public void test4(){
        List<CorporationEmploy> corporationEmploys = corporationMapper.selectCorporationEmploy2();
        System.out.println(JSON.toJSONString(corporationEmploys));
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我是刘奇奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值