MyBatis-Plus (opens new window)(简称 MP)是一个 MyBatis (opens new window)的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
官网给了一个简单的Demo,是H2数据库的,我这里改写成mysql的,并且简化了步骤。
步骤:
1、创建一个空的maven项目,添加spring-boot和mysql等5个依赖,
<dependencies>
<!-- springboot基本必须-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- springboot-test 测试需要-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- MP 主角 mybatis plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
<!-- lombok 简化getter,setter-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- mysql 数据库-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
2、添加application.yaml,添加数据库配置。
3、创建启动类和mapper.和唯一的对象User。
4、项目跑起来。
5、准备数据库。创建一个user表,不然报错,连不上数据库。
6、添加一个测试文件。
官方文档的范例修改为mysql了。
到这里本案完成。但你以为结束了吗?卷起来吧!https://github.com/baomidou/mybatis-plus-samples
- mybatis-plus-sample-quickstart: 快速开始示例
- mybatis-plus-sample-quickstart-springmvc: 快速开始示例(Spring MVC版本)
- mybatis-plus-sample-reduce-springmvc: 简化掉默认mapper类示例(Spring MVC版本)
- mybatis-plus-sample-generator: 代码生成器示例
- mybatis-plus-sample-crud: 完整 CRUD 示例
- mybatis-plus-sample-wrapper: 条件构造器示例
- mybatis-plus-sample-pagination: 分页功能示例
- mybatis-plus-sample-active-record: ActiveRecord示例
- mybatis-plus-sample-sequence: Sequence示例
- mybatis-plus-sample-execution-analysis: Sql执行分析示例
- mybatis-plus-sample-performance-analysis: 性能分析示例
- mybatis-plus-sample-optimistic-locker: 乐观锁示例
- mybatis-plus-sample-sql-injector: 自定义全局操作示例
- mybatis-plus-sample-auto-fill-metainfo: 公共字段填充示例
- mybatis-plus-sample-logic-delete: 逻辑删除示例
- mybatis-plus-sample-multi-datasource: 多数据源示例
- mybatis-plus-sample-enum: 枚举注入示例
- mybatis-plus-sample-dynamic-tablename: 动态表名示例
- mybatis-plus-sample-tenant: 多租户示例
- mybatis-plus-sample-typehandler: 类型处理器示例,例如 json 字段对象转换
- mybatis-plus-sample-deluxe: 完整示例(包含 分页、逻辑删除、自定义全局操作 等绝大部分常用功能的使用示例,相当于大整合的完整示例)
- mybatis-plus-sample-assembly: 分离打包示例
- mybatis-plus-sample-resultmap: 使用 resultMap 示例
- mybatis-plus-sample-id-generator: 自定义ID生成示例
- mybatis-plus-sample-id-string: 字符串ID生成示例
- mybatis-plus-sample-no-spring: 不使用spring下的示例
- mybatis-plus-sample-pagehelper: 使用pagehelper进行分页
- mybatis-plus-sample-association: 联表查询示例