Mybatis-Plus是简化开发的一款强大工具。学完Mybatis-Plus会使我们开发更加简单快速~
MyBatis-Plus (opens new window)(简称 MP)是一个 MyBatis (opens new window)的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
大多数主流数据库都可以使用。
MySQL,Oracle,DB2,H2,HSQL,SQLite,PostgreSQL,SQLServer,Phoenix,Gauss ,ClickHouse,Sybase,OceanBase,Firebird,Cubrid,Goldilocks,csiidb
达梦数据库,虚谷数据库,人大金仓数据库,南大通用(华库)数据库,南大通用数据库,神通数据库,瀚高数据库
想要了解更多可进官网学习哦~
代码:
- -- 建库
- create database study;
- use study;
- -- 建表
- DROP TABLE IF EXISTS `user`;
- CREATE TABLE `user` (
- `user_id` int(32) NOT NULL AUTO_INCREMENT COMMENT '用户id',
- `user_name` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '用户名称',
- `user_password` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '用户密码',
- `user_email` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '用户邮箱(用户账号)',
- `user_role` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '用户角色',
- `user_status` varchar(1) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '用户状态(0:正常,1:禁用)',
- PRIMARY KEY (`user_id`) USING BTREE
- ) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
- INSERT INTO `user` VALUES (1, '王五', '123456', 'wangwu@itcast.cn', 'ADMIN', '0');
- INSERT INTO `user` VALUES (2, '张三', '12345', 'zhangsan@itcast.cn', 'USER', '0');
- INSERT INTO `user` VALUES (3, '李四', '1234', 'lisi@itcast.cn', 'USER', '0');
- INSERT INTO `user` VALUES (4, '赵六', '666666', 'zhaoliu@itcast.cn', 'USER', '0');
查看建好的表
1.创建一个itcast-mybatis-plus的maven项目
2.导入所需的依赖
- "1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0modelVersion>
-
- <groupId>org.examplegroupId>
- <artifactId>itcast-mybatis-plusartifactId>
- <packaging>pompackaging>
- <version>1.0-SNAPSHOTversion>
- <modules>
- <module>itcast-mybatis-plus-simplemodule>
- modules>
-
- <dependencies>
- <dependency>
- <groupId>com.baomidougroupId>
- <artifactId>mybatis-plusartifactId>
- <version>3.1.1version>
- dependency>
- <dependency>
- <groupId>mysqlgroupId>
- <artifactId>mysql-connector-javaartifactId>
- <version>8.0.11version>
- dependency>
- <dependency>
- <groupId>com.alibabagroupId>
- <artifactId>druidartifactId>
- <version>1.1.12version>
- dependency>
- <dependency>
- <groupId>org.projectlombokgroupId>
- <artifactId>lombokartifactId>
- <optional>trueoptional>
- <version>1.18.20version>
- dependency>
- <dependency>
- <groupId>junitgroupId>
- <artifactId>junitartifactId>
- <version>4.12version>
- dependency>
- <dependency>
- <groupId>org.slf4jgroupId>
- <artifactId>slf4j-log4j12artifactId>
- <version>2.0.1version>
- dependency>
- dependencies>
-
- <properties>
- <maven.compiler.source>8maven.compiler.source>
- <maven.compiler.target>8maven.compiler.target>
- properties>
- <build>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.pluginsgroupId>
- <artifactId>maven-compiler-pluginartifactId>
- <configuration>
- <source>1.8source>
- <target>1.8target>
- configuration>
- plugin>
- plugins>
- build>
- project>
3.查看依赖是否导入成功
- public interface UserMapper extends BaseMapper
{ - //public interface UserMapper {
- List
findAll(); - }
继承BaseMapper,User是实体类
- package cn.itcast.study.simple.pojo;
-
- import com.baomidou.mybatisplus.annotation.TableName;
- import lombok.AllArgsConstructor;
- import lombok.Data;
- import lombok.NoArgsConstructor;
- /**
- * lombok注解 自动生成get/set 构造方法
- * */
- @Data
- @NoArgsConstructor//生成无参构造
- @AllArgsConstructor//生成有参构造
- //@TableName("user")
- public class User {
- private Integer userId;
- private String userName;
- private String userPassword;
- private String userEmail;
- private String userRole;
- private String userStatus;
-
- }
从日志可看出自动生成的sql代码
可以看出mybatis-plus自动将实体类首字母小写作为表名,字段名是将第一个大写字母自动变成‘_+小写字母’ 。如果表名不是user也可在实体类上加注解@TableName("表名")
这是MyBatis和MyBatis-Plus的运用部分,后期会发Spring+Mybatis+Mybatis-Plus的使用和SpringBoot+Mybatis+Mybatis-Plus的使用,以后简单的增删改查再也不用自己写sql了,哈哈~