首页 专题 文章 代码 归档
MybatisPlus快速开始
2020.02.10 12:33 2020.02.10 12:33

1. 快速开始

1.1. 创建数据库

截图-1581242773

表结构

-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user`  (
  `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `user_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
  `password` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  `email` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  `age` int(11) NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 6 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;

SET FOREIGN_KEY_CHECKS = 1;

数据

INSERT INTO `mp_test`.`user`(`id`, `user_name`, `password`, `email`, `age`) VALUES (1, '张三', '123', 'admin@misiai.com', 12);
INSERT INTO `mp_test`.`user`(`id`, `user_name`, `password`, `email`, `age`) VALUES (2, '李四', '131212', 'lisi@misiai.com', 18);
INSERT INTO `mp_test`.`user`(`id`, `user_name`, `password`, `email`, `age`) VALUES (3, '王五', '8989', 'wangwu@misiai.com', 20);
INSERT INTO `mp_test`.`user`(`id`, `user_name`, `password`, `email`, `age`) VALUES (4, '赵六', '8989', 'zhaoliu@misiai.com', 28);
INSERT INTO `mp_test`.`user`(`id`, `user_name`, `password`, `email`, `age`) VALUES (5, '孙七', '9891', 'sunqi@misiai.com', 30);

1.2. 创建工程

我们这里常见SpringBoot的工程。

截图-1581245541

创建时,导入Mybatis:

截图-1581245610

1.3. 依赖一览

<?xml version="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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.misiai</groupId>
    <artifactId>mybatis-plus</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>mybatis-plus</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.3.1</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.21</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

1.4. 添加配置

application.yml

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/mp_test?useSSL=false
    driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: root
    type: com.alibaba.druid.pool.DruidDataSource
#mybatis-plus:
#  check-config-location:

这里我们使用了阿里的druid数据源,所以需要导入相应依赖。


在 Spring Boot 启动类中添加 @MapperScan 注解,扫描 Mapper 文件夹:

package com.misiai.mybatisplus;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.misiai.mybatisplus.mapper")
public class MybatisPlusApplication {

    public static void main(String[] args) {
        SpringApplication.run(MybatisPlusApplication.class, args);
    }

}

1.5. 编码

Bean User

package com.misiai.mybatisplus.bean;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {

    private Integer id;
    private String user_name;
    private String password;
    private String email;
    private Integer age;
}

接口:

package com.misiai.mybatisplus.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.misiai.mybatisplus.bean.User;

public interface UserMapper  extends BaseMapper<User> {

}

1.6. 测试

@SpringBootTest
@RunWith(SpringRunner.class)
class MybatisPlusApplicationTests {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private DataSource dataSource;

    @Test
    void test01() throws SQLException {
        System.out.println("dataSource = " + dataSource);
        System.out.println(dataSource.getConnection());
        List<User> users = userMapper.selectList(null);
        for (User user : users) {
            System.out.println("user = " + user);
        }


    }

}

截图-1581294609

1.7. 打印日志

SpringBoot项目添加如下配置:

mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

即可打印SQL语句等

截图-1581301198

本节阅读完毕! (分享
二维码图片 扫描关注我们哟