一.Mybatis和Mybatis-plus的区别
Mybatis是一个轻量级的持久层框架,它通过SQL语句与Java的方法进行映射,实现了对数据库操作的简化。Mybatis-plus则是在Mybatis的基础上进行了增强,提供了更多便捷的功能。
1.注解支持annotation
Mybatis使用XML文件进行SQL语句的映射,而Mybatis-plus支持注解方式进行SQL的映射,使得SQL语句的编写更加便捷。
2.代码生成
Mybatis需要手动编写实体类、Mapper接口及XML映射文件,而Mybatis-plus提供了代码生成器,可以自动生成实体类、Mapper接口和XML映射文件,减少了开发工作量。
3.分页查询
Mybatis需要手动编写分页查询语句,而Mybatis-plus提供了内置的分页插件,可以轻松实现分页查询,简化了分页操作的编写。
4.其他功能增强
Mybatis-plus还提供了一些其他的便捷功能,比如条件构造器、性能分析插件等,使得数据操作更加简单高效。
结合以上来说,Mybatis-plus在Mybatis的基础上提供了更多的便捷功能和增强,可以更快速、更高效的进行数据库操作
二.Mybatis-plus联表查询
1创建一个DTO类来容纳查询结果
public class OrderDetailDTO {
// 订单信息
private String orderNumber;
private Date orderDate;
// 客户信息
private String customerName;
private String customerEmail;
// 产品信息
private String productName;
private BigDecimal productPrice;
// 省略 getter 和 setter 方法
}
2.创建一个Mapper接口并定义方法
@Mapper
public interface OrderDetailMapper extends BaseMapper<Order> {
@Select("SELECT o.order_number, o.order_date, c.name AS customer_name, " +
"c.email AS customer_email, p.name AS product_name, p.price AS product_price " +
"FROM orders o " +
"JOIN customers c ON o.customer_id = c.id " +
"JOIN order_details od ON o.id = od.order_id " +
"JOIN products p ON od.product_id = p.id " +
"WHERE o.id = #{orderId}")
List<OrderDetailDTO> selectOrderDetail(@Param("orderId") Long orderId);
}
3.使用Mapper方法进行查询
@Autowired
private OrderDetailMapper orderDetailMapper;
public List<OrderDetailDTO> getOrderDetail(Long orderId) {
return orderDetailMapper.selectOrderDetail(orderId);
}
三. Mybatis-plus分页查询(简化版)
使用Mapper方法进行查询
public interface YourMapper extends BaseMapper<YourEntity> {
IPage<YourDto> selectYourDtoPage(Page<?> page, @Param("param") YourParam param);
}
<select id="selectYourDtoPage" resultType="YourDto">
SELECT
t1.field1, t1.field2, t2.field3, ...
FROM
table1 t1
JOIN
table2 t2 ON t1.id = t2.foreign_key
WHERE
t1.some_field = #{param.someField}
<!-- 更多的SQL条件可以根据需要添加 -->
LIMIT #{page.offset}, #{page.size}
</select>
@Service
public class YourService {
@Autowired
private YourMapper yourMapper;
public IPage<YourDto> selectYourDtoPage(Page<?> page, YourParam param) {
return yourMapper.selectYourDtoPage(page, param);
}
}
四. 百度参考
MyBatis-Plus
是一个功能强大的 MyBatis 增强工具,用于简化数据库操作。然而,它并不直接支持多表关联查询分页,因为多表关联查询通常涉及复杂的 SQL 语句,这超出了 MyBatis-Plus 的基本功能范围。但是,你可以通过自定义 SQL 语句和结合 MyBatis-Plus 的分页插件来实现这一功能。
以下是一个使用 MyBatis-Plus 进行多表关联查询分页的教程:
1. 定义实体类
首先,你需要定义用于存储查询结果的实体类。这个实体类可以包含来自多个表的数据。
public class UserOrderVo {
private Long userId;
private String userName;
private String orderNo;
private BigDecimal orderAmount;
// 其他字段和getter/setter方法
}
2. 编写 Mapper 接口
在 Mapper 接口中定义一个方法,用于执行多表关联查询。
@Mapper
public interface UserMapper extends BaseMapper<User> {
IPage<UserOrderVo> selectUserWithOrderPage(Page<UserOrderVo> page, @Param("params") Map<String, Object> params);
}
3. 编写 XML Mapper 文件
在 XML Mapper 文件中编写 SQL 语句。确保使用了正确的关联条件和分页逻辑。
<mapper namespace="com.example.mapper.UserMapper">
<select id="selectUserWithOrderPage" resultType="com.example.vo.UserOrderVo">
SELECT u.id AS userId, u.user_name AS userName, o.order_no AS orderNo, o.amount AS orderAmount
FROM user u
LEFT JOIN order o ON u.id = o.user_id
WHERE 1=1
<if test="params.userName != null and params.userName != ''">
AND u.user_name = #{params.userName}
</if>
<if test="params.orderNo != null and params.orderNo != ''">
AND o.order_no = #{params.orderNo}
</if>
ORDER BY u.create_time DESC
LIMIT #{page.offset}, #{page.size}
</select>
</mapper>
4. 配置分页插件
在 MyBatis 的配置文件中配置分页插件,或者在 Spring Boot 项目中通过配置类来配置。
@Configuration
@MapperScan("com.example.mapper") // 扫描 Mapper 接口所在的包
public class MybatisPlusConfig {
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
}
5. 在 Service 层调用 Mapper 方法
在 Service 层中创建一个方法来调用 Mapper 方法,并处理分页逻辑。
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public IPage<UserOrderVo> getUserWithOrderPage(int current, int size, Map<String, Object> params) {
Page<UserOrderVo> page = new Page<>(current, size);
return userMapper.selectUserWithOrderPage(page, params);
}
}
6. 在 Controller 层调用 Service 方法
在 Controller 层中调用 Service 方法,并返回分页数据给前端。
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/with-order/page")
public ResponseEntity<IPage<UserOrderVo>> getUserWithOrderPage(
@RequestParam("current") int current,
@RequestParam("size") int size,
@RequestParam Map<String, String> requestParams) {
Map<String, Object> params = new HashMap<>();
// 将请求参数转换为 Map<String, Object> 类型,根据需要处理类型转换和空值检查
params.putAll(requestParams.entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey, entry -> (Object) entry.getValue())));
IPage<UserOrderVo> page = userService.getUserWithOrderPage(current, size, params);
return ResponseEntity.ok(page);
}
}
注意事项
- 确保你的 XML Mapper 文件已经被 MyBatis 正确加载。
resultType
应该指向正确的结果类型,即你的 VO 类。- 在 XML Mapper 文件中使用动态 SQL 时,确保正确地使用了
<if>
标签来处理条件查询。
五. 参考资料
Mybatis-Plus快速入门及使用(超详细+干货满满)_mybatisplus-CSDN博客
https://blog.csdn.net/adobe754/article/details/135283115
使用MybatisPlus实现联表分页查询_mybatisplus联合分页查询-CSDN博客
https://blog.csdn.net/Klelelele/article/details/132474961
https://blog.csdn.net/qfbeqhbviq/article/details/119054323
https://baomidou.com/pages/10c804/#and