SpringBoot学习-(一)如何在MyEclipse中创建SpringBoot项目

第一步:

右键,New选择创建maven项目

SpringBoot学习-(一)如何在MyEclipse中创建SpringBoot项目_第1张图片

第二步:

注意勾选create a simple project(skip archetype selection)//创建一个简单的项目跳过原型选择

SpringBoot学习-(一)如何在MyEclipse中创建SpringBoot项目_第2张图片

第三步:

groupid和artifactId被统称为“坐标”是为了保证项目唯一性而提出的,如果你要把你项目弄到maven本地仓库去,你想要找到你的项目就必须根据这两个id去查找。

  • Group Id : groupId一般分为多个段,常用的为两段,第一段为域,第二段为公司名称。域又分为org、com、cn等等许多,其中org为非营利组织,com为商业组织。举个apache公司的tomcat项目例子:这个项目的groupId是org.apache,它的域是org(因为tomcat是非营利项目),公司名称是apache,artigactId是tomcat。
  • Artifact Id : 项目的名称
  • Compiler Level : 选择jdk版本

SpringBoot学习-(一)如何在MyEclipse中创建SpringBoot项目_第3张图片

点击Finish,项目结构为:

SpringBoot学习-(一)如何在MyEclipse中创建SpringBoot项目_第4张图片

第四步:

配置pom.xml

<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>com.ahutgroupId>
    <artifactId>SpringBootDemoartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>1.5.6.RELEASEversion>
    parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
    dependencies>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-pluginartifactId>
                <configuration>
                    <source>1.8source>
                    <target>1.8target>
                configuration>
            plugin>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>
project>

以上文件我们做了三步操作:

1.设置spring boot的parent

<parent>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-parentartifactId>
    <version>1.5.6.RELEASEversion>
parent>

2.导入spring boot的web支持

<dependencies>
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-webartifactId>
    dependency>
dependencies>

3.添加Spring boot的插件

<plugin>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-maven-pluginartifactId>
plugin>

可以看到此时项目的jar依赖关系:

SpringBoot学习-(一)如何在MyEclipse中创建SpringBoot项目_第5张图片

第五步:

在src/main/java下,创建我们自定义的包和java代码

package com.ahut.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@SpringBootApplication // Spring Boot项目的核心注解,主要目的是开启自动配置
@Controller // 标明这是一个SpringMVC的Controller控制器
public class HelloApplication {

    @RequestMapping("/hello")
    @ResponseBody
    public String hello() {
        return "hello world";
    }

    // 在main方法中启动一个应用,即:这个应用的入口
    public static void main(String[] args) {
        SpringApplication.run(HelloApplication.class, args);
    }

}

第六步:

右键 > Run as > Spring Boot App,成功启动如下:

SpringBoot学习-(一)如何在MyEclipse中创建SpringBoot项目_第6张图片

第七步:

打开浏览器输入:http://localhost:8080/hello

这里写图片描述

完成

你可能感兴趣的:(SpringBoot)