欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

mybatis Plus 多表联合查询

程序员文章站 2023-09-28 20:46:14
//实体类package com.sk.skkill.entity; import com.baomidou.mybatisplus.annotation.TableField;import com.baomidou.mybatisplus.annotation.TableName;import l ......

//实体类package com.sk.skkill.entity;


import com.baomidou.mybatisplus.annotation.tablefield;
import com.baomidou.mybatisplus.annotation.tablename;
import lombok.data;

import java.io.serializable;
import java.util.date;
import java.util.list;


@tablename("orders")
@data
public class order implements serializable
{
public static final long serialversionuid =1l;
private string id;
private string ordername;
private date createtime;
private date updatetime;
private string userid;

@tablefield(exist = false)
private list<users> listusers;


public order(){

}

public order(string id, string ordername) {
this.id = id;
this.ordername = ordername;
}



}




//dao层
package com.sk.skkill.mapper;

import com.baomidou.mybatisplus.core.mapper.basemapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.page;
import com.sk.skkill.entity.order;
import com.sun.javafx.collections.mappingchange;
import org.apache.ibatis.annotations.select;

import java.util.list;
import java.util.map;

public interface ordermapper extends basemapper<order>
{
list<order> selectorder();
int addorder(order order);

//多表联合查询 按条件orderid
@select("select t1.*,t2.user_name,t2.nick_name from orders t1 left join users t2 on t1.user_id =t2.id where t1.user_id= #{id}")
list<map<string,object>> orderuserlist(page<map<string,object>> page,string id);

}


//service层
package com.sk.skkill.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.page;
import com.baomidou.mybatisplus.extension.service.iservice;
import com.sk.skkill.entity.order;

import java.util.list;
import java.util.map;

public interface orderservice extends iservice<order>
{
list<order> selectorder();
int addorder(order order);
// list<map<string,object>> orderuserlist(page<map<string,object>> page, string id);
page<map<string,object>> selectlistpage(int current,int number,string id);
}
//serviceimpl层
package com.sk.skkill.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.page;
import com.baomidou.mybatisplus.extension.service.impl.serviceimpl;
import com.sk.skkill.entity.order;
import com.sk.skkill.mapper.ordermapper;
import com.sk.skkill.service.orderservice;
import org.springframework.stereotype.service;

import java.util.list;
import java.util.map;

@service
public class orderserviceimpl extends serviceimpl<ordermapper, order> implements orderservice
{

@override
public list<order> selectorder() {
return basemapper.selectlist(null);
}

@override
public int addorder(order order) {
return basemapper.insert(order);
}



@override
public page<map<string, object>> selectlistpage(int current, int number,string id) {
//新建分页
page<map<string,object>> page =new page<map<string,object>>(current,number);
//返回结果
return page.setrecords(this.basemapper.orderuserlist(page,id));
}


}

//controller层

 

package com.sk.skkill.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.page;
import com.sk.skkill.entity.order;
import com.sk.skkill.service.impl.orderserviceimpl;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.controller;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.restcontroller;

import java.util.list;
import java.util.map;

@restcontroller
@requestmapping("order")
public class ordercontroller
{
@autowired
private orderserviceimpl service;

@requestmapping("selectorder")
public list<order> selectorder()
{
return service.selectorder();
}
@requestmapping("addorder")
public int addorder(order order){
order=new order("fggg","蒙牛milk");
return service.addorder(order);



}
@requestmapping("selectlistpage")
public list<map<string,object>> selectlistpage(string id)
{
page<map<string, object>> page = service.selectlistpage(1, 2,id);
return page.getrecords();
}




}