Spring Boot 入门

2018-06-29  本文已影响0人  白沙forever

最近公司要新开一个项目, 我目前首先考虑用 spring boot
之前项目用到过感觉很方便, 这次从最基础的搭建开始系统的学习

配置java开发环境要求

Spring Boot要求JDK1.7以上,Maven3.2以上。
我用的IDEA, JDK1.8, Maven3.5

大家最好不要用eclipse来学习,intelliJ idea目前很友好的支持SpringBoot

为什么要使用Spring Boot ?

很多公司都是用的SSM框架, 当集成了很多功能后 配置变得很繁琐
可以快速的构建项目, 不需要繁琐的xml配置文件, 内置tomcat容器,无需生成war包.

一 .自动构建项目

有两种创建方式 简单说一下自动构建
1. 打开https://start.spring.io/
2. group 是包名 , Artifact 是项目名 , 右边有个文本框, 可以选择你需要的依赖

这里我选了3个: Web, mysql 和 mybatis


构建项目.png
3. 点击构建项目, 浏览器会下载一个项目压缩包

之前选择的 Web, mysql 和 mybatis 依赖关系 会自动添加到pom中

4. 解压后导入项目到开发工具IDEA或eclipse中

二 .手动构建项目

我比较喜欢手动构建

1. 在idea新建maven项目

工程名与选择保存路径
2. pom添加以下

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.hw</groupId>
    <artifactId>SpringBootDemo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!--依赖性  从Spring Boot继承默认值 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.BUILD-SNAPSHOT</version>
    </parent>

    <!-- 添加典型的依赖于Web应用程序 -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <!-- 部署为可执行的jar -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <!-- 如果您使用.RELEASE版本,则不需要此操作-->
    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots><enabled>true</enabled></snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <url>https://repo.spring.io/milestone</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <url>https://repo.spring.io/snapshot</url>
        </pluginRepository>
        <pluginRepository>
            <id>spring-milestones</id>
            <url>https://repo.spring.io/milestone</url>
        </pluginRepository>
    </pluginRepositories>

</project>

3. 接下来打开maven图形控制面板
下载jar包.png

点击install就会从配置的远程仓库下载jar包, 放在后台慢慢下载,需要30分钟

可以参考我另一篇文章配置 国内阿里云加速地址
4. 新建一个类 com.demo.app.Application 添加主方法, 作为springboot开启的入口
 因为是自定义的包 所以要配置  @ComponentScan("com.demo.controller")   来扫描控制器
 @ComponentScan 可以用来扫描@Controller、@Service、@Repository、@Component 的类


5. 创建Controller


6. 等jar包全部下载完成后,由于内置了tomcat容器, 鼠标右键运行main方法,打开浏览器就能访问了

本章节项目github地址:

https://github.com/hweeeeeei/springbootDemo

参考官方文档地址 :

详细 https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/
入门 https://spring.io/guides/gs/spring-boot/

上一篇 下一篇

猜你喜欢

热点阅读