温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何进行Mapper多表关联查询

发布时间:2021-11-25 16:56:46 来源:亿速云 阅读:819 作者:柒染 栏目:编程语言

这期内容当中小编将会给大家带来有关如何进行Mapper多表关联查询,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

1、首先写sql语句

select p.id, p.create_time, p.update_time, p.create_user, p.update_user, p.project_name, p.test_user, 
    p.dev_user, p.simple_desc, p.other_desc, p.project_code, p.belong_env_id, p.status_id,u.status_name,u.description from projectinfo_temp p LEFT JOIN usestatus u on p.status_id = u.id

2、关联pojo

@ToString
public class ProjectinfoTemp {

    private Integer id;

    private Date createTime;

    private Date updateTime;

    private String createUser;

    private String updateUser;

    private String projectName;

    private String testUser;

    private String devUser;

    private String simpleDesc;

    private String otherDesc;

    private String projectCode;

    private Integer belongEnvId;

    private Integer statusId;
    /**
     *  关联状态表 激活状态
     */
    private Usestatus usestatus;

状态表

/**
 *
 * @author liwen406
 * @date 2019-04-21 09:12
 */
@ToString
public class Usestatus {
    private Integer id;

    private String statusName;

    private String description;

3编写关联sql语句

模仿写法

<sql id="WithBase_Column_List" >
p.id, p.create_time, p.update_time, p.create_user, p.update_user, p.project_name, p.test_user,
p.dev_user, p.simple_desc, p.other_desc, p.project_code, p.belong_env_id, p.status_id,u.status_name,u.description
</sql>
<select id="selectByExample" resultMap="BaseResultMap" parameterType="com.apitest.pojo.ProjectinfoTempExample" >
    select
    <if test="distinct" >
      distinct
    </if>
    <include refid="Base_Column_List" />
    from projectinfo_temp
    <if test="_parameter != null" >
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null" >
      order by ${orderByClause}
    </if>
  </select>

  <select id="selectByExamplestart" resultMap="WithBaseResultMap"  >
    select
    <if test="distinct" >
      distinct
    </if>
    <include refid="WithBase_Column_List" />
    from projectinfo_temp p LEFT JOIN usestatus u on p.status_id = u.id
    <if test="_parameter != null" >
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null" >
      order by ${orderByClause}
    </if>
  </select>

4前端调用写法

<div class="x_content">
                                <table id="datatable" class="table table-striped table-bordered">
                                    <thead>
                                    <tr>
                                        <th class="table-title">应用编号</th>
                                        <th class="table-title">应用名称</th>
                                        <th class="table-title">测试负责人</th>
                                        <th class="table-title">开发负责人</th>
                                        <th class="table-title">模块/接口/用例</th>
                                        <th class="table-title">状态</th>
                                        <th class="table-title">操作</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    <tr th:each="pagefo:${pageInfo.list}">
                                        <td th:text="${pagefo.projectCode}"></td>
                                        <td th:text="${pagefo.projectName}"></td>
                                        <td th:text="${pagefo.testUser}"></td>
                                        <td th:text="${pagefo.devUser}"></td>
                                        <td th:text="${pagefo.belongEnvId}"></td>
                                        <td th:text="${pagefo.usestatus.statusName}"></td>
                                        <td>
                                            <a type="button" class="fa fa-bug btn  btn-round btn-success"
                                               th:href="@{/run/}+${pagefo.projectCode}">运行</a>
                                            <a type="button" class="fa fa-edit btn btn-round btn-info"
                                               th:href="@{/cache/editProjectInf/}+${pagefo.projectCode}">编辑</a>
                                            <a type="button" class="fa fa-trash-o btn  btn-round btn-danger"
                                               th:href="@{/deleteProjectinfoTemp/}+${pagefo.projectCode}">删除</a>
                                        </td>
                                    </tr>
                                    </tbody>
                                </table>
                                <div class="box-footer">
                                    <div class="pull-left">
                                        <div class="form-group form-inline">
                                            <span th:text="'共'+${pageInfo.getPages()}+'页'"></span>,
                                            <span th:text="'当前为'+${pageInfo.getPageNum()}+'页'"></span>
                                            <span th:text="'总'+${pageInfo.getTotal()}+'条记录'"></span>
                                        </div>
                                    </div>

                                    <div class="box-tools pull-right">
                                        <ul class="pagination">
                                            <li><a th:href="@{/cache/project_list_cache(start=0)}"
                                                   aria-label="Previous">[首 页]</a></li>
                                            <li><a th:href="@{/cache/project_list_cache(start=${pageInfo.pageNum-1})}">[上一页]</a>
                                            </li>
                                            <li><a th:href="@{/cache/project_list_cache(start=${pageInfo.pageNum+1})}">[下一页]</a>
                                            </li>
                                            <li><a th:href="@{/cache/project_list_cache(start=${pageInfo.pages})}"
                                                   aria-label="Next">[尾 页]</a></li>
                                        </ul>
                                    </div>
                                </div>
                            </div>

5、访问结果显示

如何进行Mapper多表关联查询

上述就是小编为大家分享的如何进行Mapper多表关联查询了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI