时间:2021-05-19
本文主要介绍了mybatis Plus 多表联合查询,分享给大家,具体如下:
//实体类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")@Datapublic 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;@Servicepublic 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(); }}到此这篇关于mybatis Plus 多表联合查询的实现示例的文章就介绍到这了,更多相关mybatis Plus 多表联合查询内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
ThinkPHP中关联查询(即多表联合查询)可以使用table()方法或和join方法,具体使用如下例所示:1、原生查询示例:$Model=newModel()
前一阵子接手了一个使用SpringBoot和spring-data-jpa开发的项目,后期新加入一个小伙伴,表示jpa相比mybatis太难用,多表联合的查询写
1、使用mybatis-plus自身的查询构造去,只需要在全局配置中添加如下配置mybatis-plus:mapper-locations:classpath:
查询是数据库的核心,下面就为您介绍Mysql多表查询时如何实现的,如果您在Mysql多表查询方面遇到过问题,不妨一看。Mysql多表查询:复制代码代码如下:CR
1.多表查询在之前,我们示例的2个查询都是单表查询,但实际的业务场景肯定是需要多表查询的,比如现在有个需求:查询某个用户拥有的所有角色。这个需求要涉及到sys_