马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
MyBatis-Plus:特性、快速入门与 CRUD 实战
一、简介
MyBatis-Plus(简称 MP)是 MyBatis 的增强工具,在 MyBatis 基础上只做增强不做改变,为简化开发、提高效率而生。愿景是成为 MyBatis 最好的搭档。
二、核心特性
- 无侵入:只做增强不做改变,引入后对现有工程无影响。
- 损耗小:启动即自动注入基本 CRUD,性能基本无损耗,直接面向对象操作 BaseMapper。
- 强大的 CRUD 操作:内置通用 Mapper、通用 Service,少量配置即可实现单表大部分 CRUD,并带强大条件构造器。
- 支持 Lambda 形式调用:用 Lambda 表达式编写查询条件,无需担心字段名写错。
- 支持主键自动生成:支持多种主键策略(含分布式 ID 生成器 Sequence)。
- 支持 ActiveRecord 模式:实体类继承 Model 即可进行 CRUD。
- 内置代码生成器:可快速生成 Mapper、Model、Service、Controller 层代码。
- 内置分页插件:基于 MyBatis 物理分页,写分页等同于普通 List 查询。
- 内置性能分析插件:可输出 SQL 及其执行时间,便于揪出慢查询。
- 内置全局拦截插件:对全表 delete/update 提供智能分析阻断,预防误操作。
三、快速入门
使用第三方组件的一般流程:导入依赖 → 研究配置 → 编写代码 → 扩展提升。具体:建表并搭建 Spring Boot 工程,引入依赖。
- <dependency>
- <groupId>com.baomidou</groupId>
- <artifactId>mybatis-plus-boot-starter</artifactId>
- <version>3.4.2</version>
- </dependency>
复制代码
配置数据源(MySQL 8,注意时区):
- url=jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8
复制代码
传统 MyBatis 开发路径:pojo - dao(mapper.xml) - service - controller;使用 MP 后为:pojo - mapper 接口 - 使用。
Mapper 接口示例:
- package com.ithema.reggie.mapper;
- import com.baomidou.mybatisplus.core.mapper.BaseMapper;
- import com.ithema.reggie.entity.Employee;
- import org.apache.ibatis.annotations.Mapper;
- @Mapper
- public interface EmployeeMapper extends BaseMapper<Employee> {
- }
复制代码
启动类扫描 mapper 包:
- @MapperScan("com.ithema.reggie.mapper")
复制代码
查询全部示例:
- @SpringBootTest
- class ReggieApplicationTests {
- @Autowired
- private EmployeeMapper mapper;
- @Test
- void contextLoads() {
- List<Employee> employees = mapper.selectList(null);
- employees.forEach(System.out::println);
- }
- }
复制代码
四、配置日志
默认 SQL 不可见,开发时可开启 StdOutImpl 日志 ,上线关闭。
- mybatis-plus:
- configuration:
- map-underscore-to-camel-case: true
- log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
复制代码
五、CRUD 扩展
插入(insert)
- @Test
- public void insert() {
- User user = new User();
- user.setName("郝泾钊");
- user.setAge(8);
- mapper.insert(user);
- }
复制代码
发现未设置 id 时会自动插入(数据库 id 为全局唯一默认值,如雪花算法)。主键生成策略包括 uuid、自增 id、雪花算法、redis、zookeeper 等。
更新(update)
- @Test
- public void update() {
- User user = new User();
- user.setId(6L);
- user.setName("郝泾钊");
- user.setAge(8);
- mapper.updateById(user);
- }
复制代码
查询(select)
- @Test
- public void selectById() {
- User user = userMapper.selectById(1L);
- }
- @Test
- public void selectByBatchId() {
- List<User> users = userMapper.selectBatchIds(Arrays.asList(1, 2, 3));
- users.forEach(System.out::println);
- }
- @Test
- public void selectByMap() {
- HashMap<String, Object> map = new HashMap<>();
- map.put("name", "郝泾钊");
- List<User> users = userMapper.selectByMap(map);
- users.forEach(System.out::println);
- }
复制代码
分页查询
配置分页拦截器:
- @Bean
- public MybatisPlusInterceptor mybatisPlusInterceptor() {
- MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
- interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
- return interceptor;
- }
复制代码
使用内置 Page 对象:
- @Test
- public void testPage() {
- Page<User> page = new Page<>(1, 5);
- userMapper.selectPage(page, null);
- page.getRecords().forEach(System.out::println);
- }
复制代码
删除(delete)
- @Test
- public void testDelete() {
- userMapper.deleteById(1L);
- userMapper.deleteBatchIds(Arrays.asList(1, 2, 3));
- HashMap<String, Object> map = new HashMap<>();
- map.put("name", "郝泾钊");
- userMapper.deleteByMap(map);
- }
复制代码
六、逻辑删除
逻辑删除:数据库未真正移除,通过 deleted=0 变 deleted=1 标记;物理删除则是真实移除。逻辑删除可防止数据丢失,类似回收站。
- 数据库增加 deleted 字段
- 实体类添加 deleted 属性并加 @TableLogic 注解
- 配置逻辑删除值
- mybatis-plus:
- global-config:
- db-config:
- logic-delete-field: flag
- logic-delete-value: 1
- logic-not-delete-value: 0
复制代码
查询时会自动过滤已逻辑删除的记录。
qidao123.com企服评测-应用市场 编辑提示
|