当前位置: 首页 > news >正文

MyBatis-Plus(MP)是 MyBatis 的增强工具,无需编写 SQL 即可完成 CRUD 操作,极大提升开发效率。本文带你实战 Spring Boot 整合 MyBatis-Plus。

引入依赖

<!-- pom.xml --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.5</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> # application.yml spring: datasource: url: jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=utf8 username: root password: 123456 driver-class-name: com.mysql.cj.jdbc.Driver mybatis-plus: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl global-config: db-config: logic-delete-field: deleted logic-delete-value: 1 logic-not-delete-value: 0

回到顶部

二、实体类

@Data @TableName("user") public class User { @TableId(type = IdType.AUTO) private Long id; @TableField("username") private String username; private String email; private Integer age; @TableLogic // 逻辑删除 private Integer deleted; @TableField(fill = FieldFill.INSERT) private LocalDateTime createTime; @TableField(fill = FieldFill.INSERT_UPDATE) private LocalDateTime updateTime; }

回到顶部

三、Mapper 接口

@Mapper public interface UserMapper extends BaseMapper<User> { // 继承 BaseMapper 后,自动拥有以下方法: // - insert(entity) // - deleteById(id) // - updateById(entity) // - selectById(id) // - selectList(wrapper) // - selectPage(page, wrapper) // 无需编写 XML! }

回到顶部

四、条件构造器

// 查询年龄大于18且邮箱不为空的用户 List<User> users = userMapper.selectList( new LambdaQueryWrapper<User>() .gt(User::getAge, 18) .isNotNull(User::getEmail) .orderByDesc(User::getCreateTime) ); // 模糊查询 List<User> users = userMapper.selectList( new LambdaQueryWrapper<User>() .likeRight(User::getUsername, "张") .between(User::getAge, 20, 30) ); // 更新:年龄大于30的用户状态改为1 userMapper.update(null, new LambdaUpdateWrapper<User>() .set(User::getStatus, 1) .gt(User::getAge, 30) );

回到顶部

五、分页查询

// 分页配置类 @Configuration public class MyBatisPlusConfig { @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); return interceptor; } } // 分页查询 Page<User> page = new Page<>(1, 10); // 第1页,每页10条 userMapper.selectPage(page, new LambdaQueryWrapper<User>() .gt(User::getAge, 18) ); List<User> records = page.getRecords(); // 当前页数据 long total = page.getTotal(); // 总记录数 long pages = page.getPages(); // 总页数

回到顶部

六、代码生成器

// AutoGenerator 一键生成 Entity、Mapper、Service、Controller AutoGenerator generator = new AutoGenerator(); // 全局配置 GlobalConfig globalConfig = new GlobalConfig(); globalConfig.setOutputDir(System.getProperty("user.dir") + "/src/main/java"); globalConfig.setAuthor("myname"); globalConfig.setOpen(false); generator.setGlobalConfig(globalConfig); // 数据源配置 DataSourceConfig dataSourceConfig = new DataSourceConfig.Builder( "jdbc:mysql://localhost:3306/mydb", "root", "123456" ).build(); generator.setDataSource(dataSourceConfig); // 策略配置 StrategyConfig strategyConfig = new StrategyConfig.Builder() .addInclude("user", "order") // 表名 .entityBuilder().enableLombok() .controllerBuilder().enableRestStyle() .build(); generator.setStrategy(strategyConfig); generator.execute();
http://www.jsqmd.com/news/1094694/

相关文章:

  • 别再用“帮我写个排序算法”了!资深工程师私藏的12个领域专用提示词框架,今天限时开放下载
  • XSS漏洞攻防实战:从检测到绕过与防御的完整指南
  • 如何让ChatGPT写出被导师夸“逻辑严密、术语精准”的论文段落?——12个经SCI期刊编辑实测有效的Prompt结构
  • 基于TRF7960A的16通道HF RFID多路复用系统设计与实战
  • 手工排班暗藏用工合规风险,连锁企业如何规避赔偿与人力损耗
  • 2026年中国品牌进欧洲:品牌战略咨询公司对比分析与选择指南
  • GPT-4的2%激活真相:MoE稀疏架构原理与工程实践
  • 2026深度实测|Cursor优质替代品全景对比,中文Vibe Coding开发者必看
  • 魔兽世界API与宏工具:新手玩家的终极免费指南
  • 哇塞!原来论文可以这样省时间?2026降AI率平台推荐合集
  • 5步深度解析PIDtoolbox:从黑盒数据到飞行器控制优化的实战指南
  • 【2024 Prompt Engineering权威白皮书】:基于OpenAI官方文档+1272次A/B测试提炼的11类场景化模板
  • 为什么90%的工程师写不好Prompt?揭秘LLM响应偏差背后的3层认知断层,今天必须补上
  • 从提示词小白到提示工程师:零基础通关路径图(含GitHub星标15k+的Prompt Debugger工具链+实战诊断报告模板)
  • 诚信的家用神台生产厂家
  • React Hook 状态同步的常见陷阱
  • 阿里云ECS云服务器部署Vue打包静态网站:Nginx路由重定向完整配置指南
  • 递归与回溯:自己找自己,走错了就退回来再试
  • 【Prompt Engineering 黄金法则】:20年AI架构师亲授的7个不可绕过的提示词设计铁律
  • 关于软件测试统计月度报告的方案总结(更新中)
  • Prompt写不好=浪费87%的AI算力,这5类模板已帮327家企业提升任务完成率至94.6%
  • OurBMC技术深潜|第1期:飞腾腾珑E2000平台上的开源BMC产品化实战指南
  • ChatGPT写论文不被查重的底层逻辑:基于ACL 2024实证研究的4步Prompt脱敏法,Turnitin检测通过率提升至99.3%
  • NVIDIA Profile Inspector终极指南:3步掌握显卡隐藏参数调优
  • ChatGPT提示词失效的终极归因:不是模型问题,而是你忽略了这4层上下文嵌套结构(附AST可视化诊断工具)
  • 从Selenium到Playwright:现代Web自动化测试实战指南
  • MSPM0事件管理器:从硬件联动到零CPU开销数据采集实战
  • 股海扬帆 怎么操作一个股票的思路!!!!!!!!!!!!!!!!
  • NoFences:免费开源的Windows桌面分区管理终极解决方案
  • 005、DRCN递归神经网络:共享参数与监督式重建的收敛性分析