当前位置: 首页 > 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.gsyq.cn/news/1606650.html

相关文章:

  • GPT-4的2%激活真相:MoE稀疏架构原理与工程实践
  • 5步深度解析PIDtoolbox:从黑盒数据到飞行器控制优化的实战指南
  • 【Prompt Engineering 黄金法则】:20年AI架构师亲授的7个不可绕过的提示词设计铁律
  • Prompt写不好=浪费87%的AI算力,这5类模板已帮327家企业提升任务完成率至94.6%
  • OurBMC技术深潜|第1期:飞腾腾珑E2000平台上的开源BMC产品化实战指南
  • ChatGPT提示词失效的终极归因:不是模型问题,而是你忽略了这4层上下文嵌套结构(附AST可视化诊断工具)
  • NVIDIA Profile Inspector终极指南:3步掌握显卡隐藏参数调优
  • Tinke:NDS游戏文件编辑的终极指南与实战教程
  • Prompt调试失败率下降89%:用「意图-约束-示例」三元诊断模型快速定位问题根源
  • FanControl终极指南:Windows风扇智能控制实战与避坑全解
  • NoFences终极指南:免费开源让你的Windows桌面告别混乱
  • 【学术干货】Nature重磅:多智能体协作开启AI驱动科学研究新范式
  • Footprint Expert 无法加载 REF
  • Codex画图神器:无限画布Cowart本地插件完整教程(指哪改哪)
  • Elementor Pro 4.1.2 WordPress 网站页面构建器
  • 智能抠图加批量图片翻译软件助力跨境电商高效处理多语言内容
  • 深度学习神经网络架构设计中的层类型与参数调优技术探索
  • Headroom的中文文本适配方案,让CC/Codex节约30%-60%token
  • ChatGPT API账单暴涨?4个被忽视的用量黑洞,立即自查可立省$2,840/月
  • 为什么速为0时候是制高点?
  • AI智能VOCs治理系统:天津飞机涂装项目500+天稳定运行实证
  • 如何构建企业级智能运维平台:Keep开源AIOps平台完整指南
  • Selenium Grid模块化测试:基于Pytest标签实现精准调度与高效执行
  • OpenAI API接入避坑手册:12个高频报错代码+对应解决方案(附调试日志溯源)
  • 抖音视频无水印解析:5分钟学会免费下载高清原视频
  • 高速ADC评估板实战:从JESD204B接口到数据采集系统搭建
  • 暗黑破坏神2存档编辑器:终极可视化修改工具完全指南
  • 企业任务管理系统哪个好用?9款企业常用热门工具盘点
  • 3分钟掌握SRWE:突破游戏窗口分辨率限制的实战指南
  • ChatGPT API密钥泄露事件复盘(2024年Q2真实攻防日志):企业级安全加固清单(含自动轮转脚本)