6、Idea之没有网络的情况下创建SpringBoot项目

一丶创建SpringBoot有两个方式

1.没有网络的情况下使用Maven创建

1.1:用Idea创建maven项目 不要勾选 next
1.2:这里填写自己的groupId和artifactid 然后next到创建结束
1.3:现在这里就是我们已经创建了一个maven工程 现在我们在maven工程基础上 添加springboot基础依赖
1.4:添加以下依赖
<!--springboot基础的创建 告诉maven我们要创建的是一个springboot项目-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <dependencies>
        <!--SpringBook所需的jar包 默认不需要写版本号 继承父类的版本号-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--Mysql依赖 springboot 2.2.0 好像默认需要一个数据库连接 不写会报错-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.46</version>
        </dependency>
    </dependencies>
    <!--spirng 打包成war包-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
1.5:添加完成后的样子
1.6:然后我们在项目中创建以下目录和文件
1.7:然后我们在相应的目录下创建启动类和单元测试类
1.7.1:启动类代码
package com.aaa.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringbootApplication {

    public static void main(String[] args) {

        SpringApplication.run(SpringbootApplication.class,args);
    }
}
1.7.2:单元测试类代码
package com.aaa.springboot;

import org.junit.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class SpringbootTest {

    @Test
    public void test1(){
        System.out.println(111);
    }
}
1.7.3:application.yml中对应的数据连接
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: Wetalk1234
    url: jdbc:mysql://30.4.66.194:10090/test_tuling01?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true
1.8:创建对应的controller和templates模板 以及我们访问controller方法要跳到的页面
1.8.1:controller代码
package com.aaa.springboot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloWorldController {

    @RequestMapping("Hellow")
    public String HelloWorld(){
        return "HelloWorld";
    }
}
1.8.2:html代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    欢迎你使用SpringBoot
</body>
</html>
1.9:启动运行
2.0_idea创建Test类



注意:若该快捷键点击无反应,需要自己设置自动创建测试类的快捷键:
点击 file-> setting -> keymap 搜索:test
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容