有一个查询活动的方法需要把所属组织放到实体里面,通过绑定自定义resultMap实现一对多查询

实现思路

首先创建一个对应的Mapper然后在Mapper中自定义resultMap和查询方法,然后通过mybatis中的TableName注解的resultMap绑定

在application.yml中配置Mapper扫描路径

mybatis-plus:
  mapper-locations: classpath*:static/mapper/*Mapper.xml

自定义ActivitiesMapper.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.school.demo.mapper.auto.ActivitiesMapper">

    <!-- 通用查询映射结果 -->
    <resultMap >
        <id column="id" property="id" />
        <result column="origanization_id" property="origanizationId" />
        <result column="up_time" property="upTime" />
        <result column="view_num" property="viewNum" />
        <result column="content" property="content" />
        <result column="label" property="label" />
        <result column="title" property="title" />
        <result column="status" property="status" />
        <result column="is_set_join_num" property="isSetJoinNum" />
        <result column="imgs" property="imgs" />
        <result column="column_11" property="column11" />
        <result column="column_12" property="column12" />
        <association property="organization" column="origanization_id" select="findOrganization" />
        <association property="peopleNumManagement" column="id" select="findDate" />
    </resultMap>

    <select >
        SELECT * FROM organization WHERE id = #{origanizationId}
    </select>

    <select >
        SELECT * FROM people_num_management WHERE a_id = #{id}
    </select>


</mapper>

配置实体类

import com.baomidou.mybatisplus.annotation.TableName;

@TableName(resultMap = "ActivitiesResultMap")
public class Activities extends Model {

之后继续使用mybatis-plus的各种方法可以正常使用,例如分页之类

相关文章:

  • 2022-12-23
  • 2022-01-01
  • 2021-08-30
  • 2022-12-23
  • 2021-10-16
  • 2021-11-12
  • 2021-12-08
  • 2021-09-05
猜你喜欢
  • 2022-02-28
  • 2023-03-20
  • 2021-07-09
  • 2021-10-19
  • 2021-08-30
  • 2021-06-17
相关资源
相似解决方案