SpringBoot简单入门教程

前言:第一个写博客,有点紧张,不知道该写些什么,思来想去,准备把我最近学习的这个springboot入门教程给写进个人第一个博客吧,做为记念了,哈哈。好的,废话不说了,下面上教程java

1、打开eclipse建立一个maven项目,按照下方图片顺序建立。(若是你的eclipse没有集成maven工具的话,请自行百度搜索:eclipse集成maven)

到这个,一个maven工程就建立成功了。下面咱们须要引入jar包,在这里,由于咱们建立的是maven工程,所以引入jar包只须要在pom.xml文件中引入相关的依赖就好了,很是之方便mysql

2、配置咱们的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.0</modelVersion>
<groupId>dean</groupId>
<artifactId>dean-bk</artifactId>
<version>0.0.1-SNAPSHOT</version>

<!-- 继承springboot -->
<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>  
  <version>1.5.8.RELEASE</version>
</parent>
<!-- 配置咱们的编码集、JDK版本、springboot版本、数据库版本 -->
<!-- 注:还能够配置更多的版本信息,在这里就很少说了,有兴趣的朋友能够进一步研究一下 -->
<properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <java.version>1.8</java.version>
  <mybatis-spring-boot.version>1.2.0</mybatis-spring-boot.version>
  <mysql-connector.version>5.1.39</mysql-connector.version>
</properties>
<!-- 下面咱们就要引入相关的jar包了 -->
<dependencies>
  <!-- springboot 基础包 -->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
  </dependency>
  <!-- springboot web 包 -->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <!-- springboot web开发thymeleaf模板 -->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
  </dependency>
  <!-- 截止到这里,其实咱们已经能够完成一个springboot的入门案例了。 我就不往下配置更多的依赖了-->
</dependencies>web


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

在配置完pom文件以后,有时候咱们会发现咱们的工程有报错的状况。而后在Problems视图下看见以下信息:sql

 

 

 看到这不用着急:它提示咱们:Project configuration is not up-to-date with pom.xml. Select: Maven->Update Project... from the project context menu or use Quick Fix.数据库

所以咱们只要点击咱们的工程->右键,点击maven->点击update project等待jar包更新完毕,就能够了。apache

 3、写咱们程序的主类,这至关启动tomcat服务

package com.gc.dean;浏览器

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

/**
* 程序启动类
* @author dean
*
*/
@SpringBootApplication
public class App {
  public static void main(String[] args) {
    SpringApplication.run(App.class, args);  
  }
}springboot

注:在这里咱们加入了@SpringBootApplication这个注解(不能少)

4、写咱们的Cotroller

package com.gc.dean.controller;

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

@RestController
public class UserController {
  @RequestMapping("/hello")
  public String login() {
    return "hello dean";
  }
}

5、启动咱们的程序,在浏览器中输入http:localhost:8080/hello

①运行第三步咱们写的主类里的main方法。

②打开浏览器输入上方地址

OK,到这里咱们的一个springboot入门级应用程序就结束了。你也来试一试吧

相关文章
相关标签/搜索