文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

Mybatis-plus多表查询

2023-09-09 12:33

关注
package com.suse.entity;import com.baomidou.mybatisplus.annotation.TableField;import java.util.List;public class User {    private Integer id;    private String userName;    private String password;    //你查询需要这个list order你就需要把这个属性添加上去    @TableField(exist = false)    private List<Order> orders;    public Integer getId() {        return id;    }    public void setId(Integer id) {        this.id = id;    }    public String getUserName() {        return userName;    }    public void setUserName(String userName) {        this.userName = userName;    }    public String getPassword() {        return password;    }    public void setPassword(String password) {        this.password = password;    }    public List<Order> getOrders() {        return orders;    }    public void setOrders(List<Order> orders) {        this.orders = orders;    }}
package com.suse.mapper;import com.baomidou.mybatisplus.core.mapper.BaseMapper;import com.suse.entity.User;import org.apache.ibatis.annotations.*;import java.util.List;@Mapperpublic interface UserMapper extends BaseMapper<User> {//    查询用户与所有的订单    @Select("select * from tb_user")    //结果集映射:我从数据库中拿到的数据怎么给对象赋值    @Results(            {                    @Result(column = "id",property = "id"),//属性值id赋值                    @Result(column = "user_name",property = "userName"),                    @Result(column = "password",property = "password"),                    //MP允许你调用另一个mapper的方法,将这个id传给OrderMapper的selectOrderByUid(),这样就可以得到所有的订                    @Result(column = "id",property = "orders",javaType =List.class,                    many = @Many(select = "com.suse.mapper.OrderMapper.selectByUid"))//因为是一对多的,所以使用@Many,里面写调用那个mapper的哪个方法                    //javaType指定orders的类型            }    )    List<User> selectAllUserAndOrders();}
package com.suse.entity;public class Order {    private Integer id;    private  String orderTime;    private  Integer total;    private Integer uid;    public Integer getId() {        return id;    }    public void setId(Integer id) {        this.id = id;    }    public String getOrderTime() {        return orderTime;    }    public void setOrderTime(String orderTime) {        this.orderTime = orderTime;    }    public Integer getTotal() {        return total;    }    public void setTotal(Integer total) {        this.total = total;    }    public Integer getUid() {        return uid;    }    public void setUid(Integer uid) {        this.uid = uid;    }}
package com.suse.mapper;import com.baomidou.mybatisplus.core.mapper.BaseMapper;import com.suse.entity.Order;import org.apache.ibatis.annotations.Mapper;import org.apache.ibatis.annotations.Select;import java.util.List;@Mapperpublic interface OrderMapper extends BaseMapper<Order> {    @Select("select * from tb_order where uid = #{uid}")    List<Order> selectByUid(int uid);}

order pojo

package com.suse.entity;import com.baomidou.mybatisplus.annotation.TableField;public class Order {    private Integer id;    private  String orderTime;    private  Integer total;    private Integer uid;    @TableField(exist = false)    private User user;    public Integer getId() {        return id;    }    public void setId(Integer id) {        this.id = id;    }    public String getOrderTime() {        return orderTime;    }    public void setOrderTime(String orderTime) {        this.orderTime = orderTime;    }    public Integer getTotal() {        return total;    }    public void setTotal(Integer total) {        this.total = total;    }    public Integer getUid() {        return uid;    }    public void setUid(Integer uid) {        this.uid = uid;    }    public User getUser() {        return user;    }    public void setUser(User user) {        this.user = user;    }}

order mapper

package com.suse.mapper;import com.baomidou.mybatisplus.core.mapper.BaseMapper;import com.suse.entity.Order;import com.suse.entity.User;import org.apache.ibatis.annotations.*;import java.util.List;@Mapperpublic interface OrderMapper extends BaseMapper<Order> {    @Select("select * from tb_order where uid = #{uid}")    List<Order> selectByUid(int uid);    @Select("select * from tb_order")    @Results({            @Result(column = "id",property = "id"),            @Result(column = "order_time",property = "orderTime"),            @Result(column = "total",property = "total"),            @Result(column = "uid",property = "user",javaType = User.class,one =@One(                    select = "com.suse.mapper.UserMapper.getUserById"            ))    })    List<Order> selectAllOrderAndUser();}

usermapper添加

@Mapperpublic interface UserMapper extends BaseMapper<User> {    @Select("select * from tb_user where id = #{uid}")    List<User> getUserById(int uid);    }
 @GetMapping("/find")    public List<User> findByCond(){        QueryWrapper<User> qw = new QueryWrapper<>();        qw.eq("user_name","luck");        return  userMapper.selectList(qw);    }

往往数据库中有很多条数据,不可能一次将数据都放出来,仅仅展示只定条数,剩下的用户点击下一条才会显示出来

spring-boot配置类

@Configurationpublic class MybatisPlusConfig {@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {    MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();    interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));    return interceptor;}}

controller

 @GetMapping("/findByPage")    public IPage findByPAGE(){        //起始值与每页的条数        Page<User> page = new Page<>(0,2);        IPage iPage = userMapper.selectPage(page, null);   //MP自带的分页查询        return iPage;    }

来源地址:https://blog.csdn.net/userohzy/article/details/127299220

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-数据库
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯