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