zoukankan      html  css  js  c++  java
  • MyBatis-Plus通过注解的方式绑定一对多查询

    有一个查询活动的方法需要把所属组织放到实体里面,通过绑定自定义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="ActivitiesResultMap" type="com.school.demo.model.auto.Activities">
            <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 id="findOrganization" resultType="com.school.demo.model.auto.Organization">
            SELECT * FROM organization WHERE id = #{origanizationId}
        </select>
    
        <select id="findDate" resultType="com.school.demo.model.auto.PeopleNumManagement">
            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的各种方法可以正常使用,例如分页之类

  • 相关阅读:
    Python爬虫基础(四)--Scrapy框架的安装及介绍
    Python爬虫基础(三)--将爬虫获取到的数据写入到csv
    Python爬虫基础(二)--beautifulsoup-美丽汤框架介绍
    Python爬虫基础(一)
    Django 学习笔记
    Shell学习笔记...持续更新
    RobotFramework系统关键字解决导入报错robot framework Importing test library "CustomLibrary" failed
    Jmeter5.1入门--添加JsonPath断言
    Jmeter+Python2.7
    RobotFramework安装(基于python3.7+pycharm)
  • 原文地址:https://www.cnblogs.com/charlottepl/p/14699141.html
Copyright © 2011-2022 走看看