2026/5/14 2:06:13
网站建设
项目流程
p2p网站功能模块,网页设计如何添加图片,磁力多多,电子商务网站建设调查报告本篇博客将深入探讨 MyBatis-Plus 的三个核心功能#xff1a;条件构造器、自定义 SQL 和 Service 接口的基本用法。通过对这些功能的学习和掌握#xff0c;开发者能够更加高效地使用 MyBatis-Plus 进行业务开发。 目录
前言
条件构造器
?自定义SQL
?Service接口基本用法…本篇博客将深入探讨MyBatis-Plus的三个核心功能条件构造器、自定义 SQL和Service 接口的基本用法。通过对这些功能的学习和掌握开发者能够更加高效地使用 MyBatis-Plus 进行业务开发。目录前言条件构造器?自定义SQL?Service接口基本用法总结前言在现代 Java 开发中MyBatis-Plus简称 MP作为 MyBatis 的增强工具已经成为了开发者提高开发效率的利器。它通过简化 MyBatis 的操作提供了多种便捷的功能如自动生成 SQL、内置条件构造器、分页查询等。与 MyBatis 相比MyBatis-Plus 更加简洁和高效尤其适用于快速开发场景。本篇博客将深入探讨MyBatis-Plus的三个核心功能条件构造器、自定义 SQL和Service 接口的基本用法。通过对这些功能的学习和掌握开发者能够更加高效地使用 MyBatis-Plus 进行业务开发。条件构造器MyBatisPlus支持各种复杂的where条件可以满足日常开发的所有需求。查询名字中带o的存款大于等于1000猿的人的id、username、info、balance字段select id,username,info,balance from user where username like ? and balance ? Test void testQueryUser() { //构造查询条件 QueryWrapperUser wrapper new QueryWrapperUser() .select(id,username,info,balance) .like(username,o) .ge(balance,1000); //查询 ListUser userList userMapper.selectList(wrapper); userList.forEach(System.out::println); }更新用户名为jack的用户的余额为2000update user set balance 2000 where username jack Test void testUpdateByQueryMapper() { //需要更新的数据 User user new User(); user.setBalance(2000); //更新的条件 QueryWrapperUser wrapper new QueryWrapperUser() .eq(username,jack); //执行更新 userMapper.update(user,wrapper); } Test void testUpdateByQueryMapper() { //更新的条件 UpdateWrapperUser wrapper new UpdateWrapperUser() .set(balance,20) .eq(username,Jack); //执行更新 userMapper.update(null,wrapper); }更新id为124的用户的余额扣200update user set balance balance - 200 where id in (1,2,4); Test void testUpdateWrapper(){ ListLong ids List.of(1L, 2L, 4L); UpdateWrapperUser wrapper new UpdateWrapperUser() .setSql(balance balance - 200) .in(id,ids); userMapper.update(null,wrapper); }查询表中username模糊匹配o和balance 100的user中id、username、info、balance。Test void testLambdaWrapper(){ //构造查询条件 LambdaQueryWrapperUser wrapper new LambdaQueryWrapperUser() .select(User::getId,User::getUsername,User::getInfo,User::getBalance) .like(User::getUsername,o) .ge(User::getBalance,100); ListUser users userMapper.selectList(wrapper); users.forEach(System.out::println); }条件构造器的用法:QueryWrapper和LambdaQueryWrapper通常用来构建select、delete、update的where条件部分UpdateWrapper和LambdaUpdateWrapper通常只有在set语句比较特殊才使用尽量使用LambdaQueryWrapper和LambdaUpdateWrapper避免硬编码自定义SQL我们可以利用MyBatisPlus的Wrapper来构建复杂的Where条件然后自己定义SQL语句中剩下的部分。将id在指定范围内的用户例如124的余额扣减指定值。update id updateBalanceByIds update user set balance balance - #{amount} where id in foreach collectionids separator,,itemid open( close) #{id} /foreach /update我们可以利用MvBatis Plus的包装器来构建复杂的Where条件然后自己定义SOL语句中剩下的部分。(1)基于包装器构建其中条件ListLong ids List.of(1L, 2L, 4L); //构建条件 LambdaQueryWrapperUser wrapper new LambdaQueryWrapperUser() .in(User::getId,ids); //自定义SQL方法调用 userMapper.updateBalanceByIds(wrapper,amount);(2)在mapper方法参数中用Param注解声明wrapper变量名称必须是ewvoid updateBalanceByIds(Param(ew) LambdaQueryWrapperUser wrapper,Param(amount) int amount);(3)自定义SQL并使用Wrapper条件update idupdateBalanceByIds update user set balance balance - #{amount} ${ew.customSqlSegment} /updateuserMapper类package com.itheima.mp.mapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.toolkit.Constants; import com.itheima.mp.domain.po.User; import org.apache.ibatis.annotations.Param; public interface UserMapper extends BaseMapperUser { void updateBalanceByIds(Param(Constants.WRAPPER)LambdaQueryWrapperUser wrapper, Param(amount) int amount); }测试方法Test void testCustomSqlUpdate(){ //更新条件 ListLong ids List.of(1L, 2L, 4L); int amount 200; //定义条件 LambdaQueryWrapperUser wrapper new LambdaQueryWrapperUser() .in(User::getId,ids); //自定义方法 userMapper.updateBalanceByIds(wrapper,amount); }Service接口基本用法自定义接口需要去继承IService接口实现类需要继承ServiceImplIUserService接口package com.itheima.mp.service; import com.baomidou.mybatisplus.extension.service.IService; import com.itheima.mp.domain.po.User; import org.springframework.stereotype.Service; public interface IUserService extends IServiceUser { }UserServiceImpl类package com.itheima.mp.service.impl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.itheima.mp.domain.po.User; import com.itheima.mp.mapper.UserMapper; import com.itheima.mp.service.IUserService; import org.springframework.stereotype.Service; Service public class UserServiceImpl extends ServiceImplUserMapper, User implements IUserService { }测试类package com.itheima.mp.service; import com.itheima.mp.domain.po.User; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import java.time.LocalDateTime; SpringBootTest class IUserServiceTest { Autowired private IUserService userService; Test void testsaveUser(){ User user new User(); user.setUsername(XiaoHong); user.setPassword(123456); user.setPhone(18688990982); user.setBalance(1500); user.setInfo({age: 23, intro: 英文老师, gender: female}); user.setCreateTime(LocalDateTime.now()); user.setUpdateTime(LocalDateTime.now()); userService.save(user); } }总结通过本篇博客的讲解开发者应该对 MyBatis-Plus 的三个核心功能有了一个清晰的认识条件构造器QueryWrapper使得查询条件构建更加简洁极大减少了编写 SQL 语句的复杂度。自定义 SQL使得在复杂的业务需求下能够灵活应对提供了更大的自由度。Service 接口基本用法通过继承ServiceImpl大大简化了 CRUD 操作的实现提升了开发效率。这些功能不仅能够帮助我们提高开发效率还能够减少代码冗余提升代码的可读性和维护性。在实际的开发中MyBatis-Plus 提供的这些工具将是日常工作中的好帮手。希望通过本篇博客读者能够更好地理解 MyBatis-Plus提升自己的开发技能快速构建高效、优雅的业务系统。