mybatisplus实际上只对单表查询做了增强(速度会更快),从传统的手写sql语句,自己做映射,变为封装好的QueryWrapper。
本篇文章的内容是有两张表,分别是用户表和订单表,在不直接在数据库做表连接的情况下,通过后台代码完成①查询订单的同时查到该订单所属的用户,②查询用户的同时查到该用户的订单列表的功能。
@TableName("t_order")
public class Order {
@TableId(type = IdType.AUTO)
private int id;
private String order_time;
private String total;
private int uid;
@TableField(exist = false)
private User user;
最后记得在类里面自动生成getter和setter还有toString方法。
@TableName("t_user")
public class User {
@TableId(type = IdType.AUTO)
private int id;
private String username;
private String password;
private String birthday;
//描述用户的所有订单
@TableField(exist = false)
private List orders;
@TableName同上,加与不加都无所谓。最后记得在类里面自动生成getter和setter还有toString方法。
@Mapper
public interface UserMapper extends BaseMapper {
@Update("insert into t_user values (#{id},#{username},#{password},#{birthday})")
public int insert(User 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.mybatisplusdemo.mapper.OrderMapper.selectByUid")
)
}
)
List selectAllUserAndOrders();
}
@Select("select * from t_order where uid = #{uid}")
List selectByUid(int uid);
@Select("select * from t_order")
@Results(
{
@Result(column = "id",property = "id"),
@Result(column = "ordertime",property = "ordertime"),
@Result(column = "total",property = "total"),
@Result(column = "uid",property = "user",javaType = User.class,
one = @One(select = "com.example.mybatisplusdemo.mapper.UserMapper.selectById")
)
}
)
List selectAllOrderAndUser();
注意到是one = @One,因为一个订单只对应着一个用户。
@RestController
@CrossOrigin
public class OrderController {
@Autowired
private OrderMapper orderMapper;
@GetMapping("/order/findAll")
public List findAll()
{
List orders = orderMapper.selectAllOrderAndUser();
return orders;
}
}
@RestController
@CrossOrigin
public class UserController {
@Autowired
private UserMapper userMapper;
@GetMapping("/user")
public List query(){
List list = userMapper.selectList(null);
System.out.println(list);
return list;
}
@GetMapping("/user/findAll")
public List find(){ return userMapper.selectAllUserAndOrders();}
}
mybatisplus提供了封装好的QueryWrapper类,让我们做条件查询或者更新查询。
注意如果想要使用mybatisplus,要把原来实体类里的@TableField加上
@RestController
public class UserController {
@Autowired
private UserMapper userMapper;
@GetMapping("/user/find")
public List query() {
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.eq("username","zhangsan");
// 筛选出用户名为张三的用户。
return userMapper.selectList(queryWrapper);
}
}
新建一个软件包config,编写一个配置类
@Configuration
public class MyBatisPlusConfig {
@Bean
public MybatisPlusInterceptor paginationInterceptor(){
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); // 定义一个拦截器
PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(DbType.MYSQL); //告诉它数据库类型
interceptor.addInnerInterceptor(paginationInnerInterceptor);// 拦截器注册
return interceptor;
}
}
在UserController中添加分页查询方法
@GetMapping("user/findByPage")
public IPage findByPage(){
Page page = new Page<>(0,2); // 设置起始值和每页条数
IPage iPage = userMapper.selectPage(page,null); // 返回结果集,这里如果想额外增加附属条件
//可以写在第二个参数queryWrapper中
return iPage;
}