MyBatis-Plus多表联合查询并且分页(3表)

这3张表的关系是   模型表Model  ===> 训练表Training ===》应用表Application(大概的逻辑是:选择应用,然后训练,然后成为模型)

首先我们先建立实体Model(我使用的data注解不需要get set  @TableField(exist = false) 注解下的属性 是相关联表的属性)

package cn.com.befery.dataai.po;



import java.util.Date;

import org.springframework.boot.jackson.JsonComponent;

import com.baomidou.mybatisplus.annotations.TableField;
import com.baomidou.mybatisplus.annotations.TableId;
import com.baomidou.mybatisplus.annotations.TableName;
import com.baomidou.mybatisplus.enums.IdType;

import lombok.Data;

@JsonComponent()
@Data
@TableName("ai_model")
public class Model {
	@TableId(value = "model_id", type = IdType.AUTO)
	private Long modelID;
	private Long applicationId;
	private Long trainingId;
	private String modelName;
	// 描述
	private String modelDescribe;
	private String modelType;
	private Date createDate;
	private String filePath;
	private String fileName;
	private String daimension;	//维度
	private Long status;

	@TableField(exist = false)
	private String applicationName;

	@TableField(exist = false)
	private String trainingName;

	@TableField(exist = false)
	private String order;

	@TableField(exist = false)
	private String orderdir; // 升序或降序
}

然后是第二个相关联的表 应用表application表

package cn.com.befery.dataai.po;

import java.io.Serializable;
import java.util.Date;

import com.baomidou.mybatisplus.annotations.TableField;
import com.baomidou.mybatisplus.annotations.TableId;
import com.baomidou.mybatisplus.annotations.TableName;
import com.baomidou.mybatisplus.enums.IdType;

import lombok.Data;

@Data
@TableName("ai_application")
public class Application implements Serializable{
	private static final long serialVersionUID = 1L;

	@TableId(value="application_id",type=IdType.AUTO)
	private Long applicationID;
	private String applicationName;
	private String filePath;
	private String fileName;
	private Long userId;
	private Date createDate;
	private Integer status;
	private String dimension;	//维度
	
	@TableField(exist= false)
	private String userName;	//关联用户表的名称字段
	
	@TableField(exist = false)
	private String order;
	
	@TableField(exist = false)
	private String modelName;
	
	@TableField(exist = false)
	private String trainingName;
	
	@TableField(exist = false)
	private String orderdir;   //升序或降序
}

然后是相关联的第3张表 训练表traning

package cn.com.befery.dataai.po;

import java.io.Serializable;
import java.util.Date;

import com.baomidou.mybatisplus.annotations.TableField;
import com.baomidou.mybatisplus.annotations.TableId;
import com.baomidou.mybatisplus.annotations.TableName;
import com.baomidou.mybatisplus.enums.IdType;

import lombok.Data;
@Data
@TableName("ai_training")
public class Training implements Serializable {
	private static final long serialVersionUID = 1L;

	@TableId(value = "training_id", type = IdType.AUTO)
	private Long trainingID;
	private Long serverId;			//服务器ID
	private Long applicationId;		//应用ID
	private String trainingModel;	//训练模型
	private String trainingName;	//训练名称
	private String dimensionInput;	//输入维度
	private String dimensionOutput;	//输出维度
	private Date createDate;
	private Integer status;
	
	@TableField(exist = false)
	private String applicationName;
	
	@TableField(exist = false)
	private String serverName;
	
	@TableField(exist = false)
	private String modelName;
	
	@TableField(exist = false)
	private String order;		//排序字段
	
	@TableField(exist = false)
	private String orderdir;   //升序或降序
}

然后是DAO层:

package cn.com.befery.dataai.dao;

import java.util.List;

import org.apache.ibatis.annotations.Param;

import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.baomidou.mybatisplus.plugins.pagination.Pagination;

import cn.com.befery.dataai.po.Model;

public interface ModelDao extends BaseMapper {

	List selectModelPage(Pagination page,@Param(value = "model") Model model);
}

然后是xml(sql语句使用了别名,别名和实体中的一致,包括之后的前后台交互,都取一致的名字,规范避免出错)【我之所以使用 $ 符号是因为 如果使用#号他会当作字符串识别,他不会当作关键字识别,我使用#号不行】




	

然后就是service:

package cn.com.befery.dataai.service;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.IService;

import cn.com.befery.dataai.po.Model;
import cn.com.befery.dataai.vo.ResultCode;


public interface ModelService extends IService{
        //分页	
	Page selectModelPage(int pageNo,int pageSize,Model model);
	
}

然后就是serviceImpl:(此处将接口中的  pageNo和pageSize封装成到  分页辅助类 page中)

@Service
@Transactional
public class ModelServiceImpl extends ServiceImpl implements ModelService {

	@Autowired
	private ModelDao modelDao;

	@Override
	public Page selectModelPage(int pageNo, int pageSize, Model model) {
		// TODO Auto-generated method stub
		Page page = new Page(pageNo, pageSize);
		return page.setRecords(this.baseMapper.selectModelPage(page, model));
	}
}

 

然后就是Controller:(

简单说一下下面的参数:

  1. orderNO(排序用的):是前台传过来的,根据orderNO(类似下标)找到前台定义好的数据库字段
  2. order(排序用的):根据orderNO(类似下标)找到前台定义好的数据库字段
  3. orderdir(排序用的:是asc   还是desc)
  4. search(前台模糊查询使用的):前台传的名字,来进行模糊查询

/**
	 * @author zhangxuewei 三表查询
	 * @param param
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/modelPage")
	public ResponseData modlePage(SearchParam param, HttpServletRequest request) {
		logger.info("modlePage ...........");
		String orderNO = request.getParameter("order[0][column]");
		String order = request.getParameter("columns[" + orderNO + "][name]");
		String orderdir = request.getParameter("order[0][dir]");
		String search = request.getParameter("search[value]");

		int pageNo = param.getStart() / param.getLength() + 1;
		int pageSize = param.getLength();

		Model model = new Model();
		model.setModelName(search);
		model.setOrder(order);
		model.setOrderdir("asc".equals(orderdir) ? "asc" : "desc");
		Page pageDate = modelService.selectModelPage(pageNo, pageSize, model);

		return responseData(param.getDraw(), pageDate);
	}

这个是分页返回公共类

package cn.com.befery.dataai.controller;

import org.springframework.stereotype.Controller;

import com.baomidou.mybatisplus.plugins.Page;

import cn.com.befery.dataai.vo.ResponseData;

@Controller
public class BaseController {

    /**
     * 
     * @param draw 重构次数
     * @param page 分页数据
     * @return
     */
    public ResponseData responseData(String draw,Page page){
    	ResponseData res = new ResponseData();
    	res.setData(page.getRecords());
    	res.setDraw(draw);
    	res.setRecordsFiltered((int)page.getTotal());
    	res.setRecordsTotal((int)page.getTotal());
    	return res;
    }
}

这个是ResponseDate实体类

package cn.com.befery.dataai.vo;

import java.util.List;

//@JsonInclude(Include.NON_NULL) 
public class ResponseData {
    
	/**
	 * 
	 */
//	private static final long serialVersionUID = 1L;
	
	private String draw;
	
	private int recordsTotal;
	
	private int recordsFiltered;
	
	@SuppressWarnings("rawtypes")
	private List data;
}

 

这是前端的html

















应用列表



	
	
序号 模型服务名称 训练名称 应用名称 创建时间 操作

 

你可能感兴趣的:(MyBatis-Plus多表联合查询并且分页(3表))