SpringBoot整合MybatisPlus。

创建个SpringBoot项目

选生所需的依赖:==

我把application的后缀改为.yml了,方便些。

==pom.xml:==

<?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.4.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.keafmd</groupId>
    <artifactId>springboot-mybatisplus</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-mybatisplus</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-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

因为我们配置了数据源,所以需要在application.yml中配置下数据源,不然会起不来,我顺便也改了下端口。

==application.yml:==

server:
  port: 80

spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/ssm-java1?useSSL=false&&characterEncoding=UTF-8
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: 18044229

写个HelloController测试下

==HelloController:==

package com.keafmd.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Keafmd
 *
 * @ClassName: HelloController
 * @Description:
 * @author: 牛哄哄的柯南
 * @Date: 2021-04-09 11:11
 * @Blog: https://keafmd.blog.csdn.net/
 */
@RestController
public class HelloController {
 
    @RequestMapping("/hello")
    public String hello(){
        return "keafmd";
    }
}

运行启动类,访问:http://127.0.0.1/hello

到此证明SpringBoot没有问题。

使用代码生成器生成代码

添加所需的依赖

pom.xml中添加以下依赖:

<dependency>
   <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-generator</artifactId>
    <scope>test</scope>
    <version>3.4.0</version>
</dependency>

<dependency>
    <groupId>org.freemarker</groupId>
    <artifactId>freemarker</artifactId>
    <scope>test</scope>
    <version>2.3.31</version>
</dependency>

由于代码生成器并不会在生产环境使用,只是在开发环境中使用了下。所以我们把代码生成器写在test包中即可,依赖的使用场景也定义成test即可。

CodeGenerator

==CodeGenerator:==

展开阅读全文

本文系作者在时代Java发表,未经许可,不得转载。

如有侵权,请联系nowjava@qq.com删除。

编辑于

关注时代Java

关注时代Java