本篇文章为大家展示了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") @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(); } }
上述内容就是mybatis Plus 实现多表联合查询的方法,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。