MybatisPlus进阶
1.MybatisPlus一对多查询
2.分页查询
1.MybatisPlus一对多查询
场景:我有一个表,里面填写的是用户的个人信息(姓名,生日,密码,用户ID)。我还有一个表填写的订单信息(订单编号,订单价格,下单用户id)。现在我想查询用户信息的时候,将用户的订单信息一并查询出来。
a.编写User类
第一步肯定是编写User类,User里应该包含用户ID,密码,生日,用户对应订单。这里需要注意,根据ORM,我们编写的User类是对应了User表的,但是User表里是不包含订单这个字段的。所以,我们在编写的时候,需要注明该字段是不存在User表中,不然会报错。
package com.example.mpdemo;
import java.util.List;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
@TableName("t_user")
public class User {
private int id;
private String username;
private String password;
private String birthday;
@TableField(exist = false)
private List<Order> orders;
public int getId() {
return id;
}
public void setId(int 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 String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public List<Order> getOrders() {
return orders;
}
public void setOrders(List<Order> orders) {
this.orders = orders;
}
}
@TableName对应表格名称,若不写,则默认是类的名称,大小写不敏感。
@TableField(exist = false)则表示该字段在表格中不存在。属于MybatisPlus中的功能。
用户表和订单表:
由于是一对多,且存在用户表中不存在的订单字段,所以需要自己写Result。
package com.example.mpdemo;
import java.util.List;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
@Mapper
public interface UserMapper extends BaseMapper<User>{
@Select("select * from t_user")
@Results(
{
@Result(column = "id", property = "id"),
@Result(column = "username", property = "username"),
@Result(column = "password", property = "password"),
@Result(column = "birthday", property = "birthday"),
@Result(column = "id", property = "orders", javaType = List.class,
many = @Many(select = "com.example.mpdemo.OrderMapper.selectByUid")
)
}
)
List<User> selectAllUserAndOrders();
}
@Result注解中,column表示数据库中的字段,property则表示User类中的字段名称。
最后一个@@Many注解中,column表示数据库中的字段,property则表示User类中的字段名称,javaType表示返回的Java类型,因为一个用户可以拥有多个订单信息,所以返回的是list。select表示要使用Mapper。
由于需要根据用户ID查询订单,所以需要编写Order类和Order对应的Mapper。
package com.example.mpdemo;
import java.sql.Date;
import com.baomidou.mybatisplus.annotation.TableName;
@TableName("t_order")
public class Order {
private int id;
private String order_time;
private String total;
private String uid;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getOrder_time() {
return order_time;
}
public void setOrder_time(String order_time) {
this.order_time = order_time;
}
public String getTotal() {
return total;
}
public void setTotal(String total) {
this.total = total;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
}
package com.example.mpdemo;
import java.util.List;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.type.JdbcType;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
@Mapper
public interface OrderMapper extends BaseMapper<Order> {
@Select("select * from t_order where uid = #{uid}")
@Results(
{
@Result(column = "id", property = "id"),
@Result(column = "order_time", jdbcType = JdbcType.DATE,property = "order_time"),
@Result(column = "total", property = "total"),
@Result(column = "uid", property = "uid"),
}
)
List<Order> selectByUid(int uid);
}
最后,我们需要编写一个Controller来实现查询:
package com.example.mpdemo;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
@RestController
public class UserContoller {
@Autowired
private UserMapper userMapper;
@GetMapping("/user/findall")
public List<User> findall() {
List<User> results = userMapper.selectAllUserAndOrders();
return results;
}
}
通过POSTman请求,查看结果:(我这里设置的端口是8081)
这样我们就实现了查询用户信息并且查询用户下单信息。
2.分页查询:
分页查询,mybatisPlus为我们提供了相应的分页插件。所以我们不需要自己编写一个分页拦截器,只需将MybatisPlus为我们提供的分页拦截器通过@configuration配置注解到Springboot当中即可。因为分页是通过拦截器实现的,拦截器拦截并修改SQL语句,返回分页查询结果,实现分页。
package com.example.mpdemo;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
@Configuration
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor paginationInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(DbType.MYSQL);
interceptor.addInnerInterceptor(paginationInnerInterceptor);
return interceptor;
}
}
根据MybitsPlus提供的分页,我们的Controller需要这样编写:
package com.example.mpdemo;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
@RestController
public class UserContoller {
@Autowired
private UserMapper userMapper;
@GetMapping("/user/findall")
public List<User> findall() {
List<User> results = userMapper.selectAllUserAndOrders();
return results;
}
@GetMapping("/user/findbypage/{page}")
public IPage findByPage(@PathVariable int page) {
Page<User> pageDemo = new Page<>(page,2);
IPage iPage = userMapper.selectPage(pageDemo, null);
return iPage;
}
}
其中参数page表示第几页,返回的是IPage。因为我们的usermapper继承了MybatisPlus的BaseMapper,BaseMapper其中有selectPage方法。
看看结果: