Mybatis两表连接(一对一)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mybatis两表连接(一对一)相关的知识,希望对你有一定的参考价值。

OrderJudgeMapper.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.datangedu.cn.dao.mapper.OrderJudgeMapper">
    <resultMap type="com.datangedu.cn.model.BusinessOrder" id="OrderJudgeMap">
        <id property="id" column="bid" />
        <result column="SERVER_ID" jdbcType="VARCHAR" property="serverId" />
        <result column="ORDER_INFO" jdbcType="VARCHAR" property="orderInfo" />
        <result column="CREATE_TIME" jdbcType="TIMESTAMP" property="createTime" />
        <!-- association :配置一对一属性 -->
        <!-- property:实体类中里面的  属性名 -->
        <!-- javaType:属性类型 -->
        <association property="serviceJudge" javaType="com.datangedu.cn.model.ServiceJudge">
            <id property="id" column="sid" />
            <result column="ORDER_ID" jdbcType="VARCHAR" property="orderId" />
            <result column="CONTENT" jdbcType="VARCHAR" property="content" />
            <result column="TYPE" jdbcType="INTEGER" property="type" />
            <result column="STATUS" jdbcType="INTEGER" property="status" />
        </association>
    </resultMap>
    <select id="selectOrderJudge" parameterType="java.lang.String" resultMap="OrderJudgeMap">
        <!--SELECT b.id bid,SERVER_ID,ORDER_INFO,CREATE_TIME,s.id sid,ORDER_ID,CONTENT,TYPE,s.STATUS
            FROM business_order b
            LEFT JOIN service_judge s ON b.id=ORDER_ID
            WHERE b.id=ORDER_ID -->
        SELECT b.id bid,SERVER_ID,ORDER_INFO,CREATE_TIME,s.id sid,ORDER_ID,CONTENT,TYPE,s.STATUS
        FROM business_order b
        LEFT JOIN service_judge s ON b.id=ORDER_ID
        WHERE b.id=ORDER_ID and s.STATUS=#{status}
    </select>
</mapper>

实体类

package com.datangedu.cn.model;

import java.util.Date;

import org.springframework.stereotype.Component;
@Component
public class BusinessOrder {
    private String id;
    private String businessNo;
    private String serverId;
    private Integer buynum;
    private String orderInfo;
    private Integer status;
    private Integer totalPrice;
    private Integer payType;
    private String memberId;
    private Date createTime;

    private ServiceJudge serviceJudge;//ServiceJudge的对象

    //此处应该重写hashcode和equals方法
    @Override
    public String toString() {
        return "BusinessOrder:" + id + "-" +createTime + "serviceJudge:" + serviceJudge.getId() + "-" + serviceJudge.getContent();
    }
}

controller层

@ResponseBody
    @RequestMapping("/selectOrderJudge")
    public Map<String,Object> eCommerceEvaluate(String status) {
        Map<String,Object> map = new HashMap<String,Object>();
        //System.out.println(status);
        List<BusinessOrder> businessOrderList = orderJudgeService.selectOrderJudge(status);
        //System.out.println(businessOrderList);
        if(businessOrderList.size()>0) {
            map.put("businessOrderList", businessOrderList);
            map.put("code", 1);
        }else {
            map.put("code", 0);
        }
        return map;
    }

js

$(".content-nav li").on("click", function(event){
    $(".content-nav li").removeClass("nav-active");
    $(event.target).addClass("nav-active");
    //alert("value=" + $(".nav-active .status").val());
    var status = $(".nav-active .status").val();
    $.ajax({
        url:"/selectOrderJudge",
        type:"post",
        data:{"status":status},
        success:function (data) {
            console.log(data);
            if(data.code==1){
                var str = "";
                $.each(data.businessOrderList,function(index,item){
                    if(item.serviceJudge.status==1){
                        str=`
                            <img src="" alt="图片" />
                            <ul class="article-info">
                                <li text="` + item.orderInfo +`"> ` + item.orderInfo + `</li>
                            </ul>
                            <p th:text="` + item.createTime + `">`+ item.createTime +`</p>
                            <p class="evaluate_btn">去评价</p>
                        `;
                    }else{
                        str=`
                            <img src="" alt="图片" />
                            <ul class="article-info">
                                <li text="` + item.orderInfo +`"> ` + item.orderInfo + `</li>
                            </ul>
                            <p th:text="` + item.createTime + `">`+ item.createTime +`</p>
                            <p class="evaluate_btn">已评价</p>
                        `;
                    }
                });
                
                $(".article").html(str);
            }else{
                $(".article").html("<p style='font-size:20px'>未找到订单</p>");;
            }
        },
        error:function(){
            console.error(arguments[1]);
        }
    });
})

以上是关于Mybatis两表连接(一对一)的主要内容,如果未能解决你的问题,请参考以下文章

mybatis 关联表查询

mybatis使用全注解的方式案例(包含一对多关系映射)

Mybatis框架第二篇

Mybatis

Mybatis--02

MyBatis