MyBatis-Plus (opens new window)(简称 MP)是一个 MyBatis (opens new window)的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
DROP TABLE IF EXISTS `sys_user`; CREATE TABLE `sys_user` ( `user_id` int NOT NULL AUTO_INCREMENT COMMENT '主键ID', `username` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '用户名', `password` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '密码', `del_flag` char(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '0' COMMENT '0-正常,1-删除', `create_time` datetime(0) NULL DEFAULT NULL COMMENT '创建时间', `update_time` datetime(0) NULL DEFAULT NULL COMMENT '修改时间', `create_by` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '创建者', `update_by` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '更新人', `version` int NULL DEFAULT NULL COMMENT '乐观锁', PRIMARY KEY (`user_id`) USING BTREE, INDEX `user_idx1_username`(`username`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 7 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '用户表' ROW_FORMAT = DYNAMIC; -- ---------------------------- -- Records of sys_user -- ---------------------------- INSERT INTO `sys_user` VALUES (1, 'admin', '123456', '0', '2021-08-29 00:38:43', '2021-08-29 00:38:45', 'admin', 'admin', 1); INSERT INTO `sys_user` VALUES (2, 'loner', '123456', '0', '2021-08-29 00:38:43', '2021-08-29 00:38:45', 'admin', 'admin', 1); INSERT INTO `sys_user` VALUES (3, 'Bob', '123456', '0', '2021-08-29 00:38:43', '2021-08-29 00:38:45', 'admin', 'admin', 1); INSERT INTO `sys_user` VALUES (4, 'Jack', '123456', '0', '2021-08-29 00:38:43', '2021-08-29 00:38:45', 'admin', 'admin', 1);
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.3</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--MyBatis-Plus--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.3</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-extension</artifactId> <version>3.4.3</version> </dependency> <!--Druid数据源--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.2.6</version> </dependency> <!--MySQL驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!--Lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies>
# 数据源配置 spring: datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://192.168.1.11:3306/mybatis-plus?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&autoReconnect=true username: root password: MaH00... initialSize: 5 minIdle: 5 maxActive: 20 maxWait: 60000 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: SELECT 1 FROM DUAL testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true
// 创建用户实体表 @Data @TableName("sys_user") public class User implements Serializable { private static final long serialVersionUID = 1L; /** * 用户ID */ @TableId(value = "user_id", type = IdType.AUTO) private Long userId; /** * 用户账号 */ private String username; /** * 密码 */ private String password; /** * 删除标志(0代表存在 2代表删除) */ @TableLogic private String delFlag; /** * 创建者 */ @TableField(fill = FieldFill.INSERT) private String createBy; /** * 创建时间 */ @TableField(fill = FieldFill.INSERT) private Date createTime; /** * 更新者 */ @TableField(fill = FieldFill.INSERT_UPDATE) private String updateBy; /** * 更新时间 */ @TableField(fill = FieldFill.INSERT_UPDATE) private Date updateTime; /** * 乐观锁 */ private Integer version; } // 继承BaseMapper接口 public interface UserMapper extends BaseMapper<User> { } // 编写测试代码 @SpringBootTest public class UserTest { @Autowired private UserMapper userMapper; @Test public void test() { // 直接使用BaseMapper定义好的查询方法 List<User> userList = userMapper.selectList(null); userList.forEach(System.out::println); } }
整个MP(简称)的使用过程,就是如此的简单,当我们继承完 BaseMapper 接口后,我们就完成了 CRUD 操作,剩余的只是针对不同的业务进行封装使用罢了。