Spring Boot初始项目搭建

首先,springboot是一个框架,帮助你”即插即用”,也就是说帮你快速的配置环境!!!

SpringBoot其实非常简单,一旦你掌握好它所提供的annotations 和 api,你就能迅速上手!so don’t worry about it。

 

 

  1. 明确我们的项目结构如下:

      

  • 接下来,我们开始新建我们的项目,首先我们打开intellij ide,然后我们新建一个maven项目。

     

  • 然后我们填写好项目名称和位置。

  • 创建好项目后,你的目录结构会如下图所示:

 

其中,main文件夹包含java和resource两个文件夹,java中包含了相关的java code,resource文件夹包含了一些配置文件和html等。

Test文件夹中包含test相关的java code

pom.xml中描述了我们项目需要的相关依赖等。

然后我们在浏览器输入https://spring.io/guides/gs/spring-boot/,这是springboot的官方网站,我们选择build with marven,选择其中的下列代码:

 

<parent>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-parent</artifactId>

        <version>2.0.5.RELEASE</version>

    </parent>

 

    <dependencies>

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-web</artifactId>

        </dependency>

    </dependencies>

 

    <properties>

        <java.version>1.8</java.version>

    </properties>

 

 

    <build>

        <plugins>

            <plugin>

                <groupId>org.springframework.boot</groupId>

                <artifactId>spring-boot-maven-plugin</artifactId>

            </plugin>

        </plugins>

    </build>

 

粘贴到我们的pom.xml文件中,然后自动生成相关的依赖

就像上图所示。

然后按照项目结构,我们在工程中创造如下文件:

 

  • Main.java放在根目录下,主要用于一些框架配置。

  • Entity主要存放实体

  • Dao存放数据访问层

  • Service存放业务类代码

  • Controller负责页面访问控制

 

我们将Main作为我们的主要配置类:

package com.raytseng;

 

 

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

 

 

/**

* @author raytseng

* @create 2019/1/4-下午8:13

* @detail:

**/

 

 

@SpringBootApplication

public class Main {

    public static void main(String[] args)

    {

        SpringApplication.run(Main.class, args);

    }

}

 

注:@SpringBootApplication在这里相当于 @Configuration, @EnableAutoConfiguration, 以及@ComponentScantogether三个注解的结合。

 

然后,可以首先在entity文件夹下创建实体类,这里我们首先创造student实体类如下:

package com.raytseng.Entity;

 

/**

* @author raytseng

* @create 2019/1/4-下午8:19

* @detail:

**/

public class Student {

    private int id;

    private String name;

    private String course;

 

    public Student(int id, String name, String course) {

        this.id = id;

        this.name = name;

        this.course = course;

    }

 

    public Student(){}

 

    public int getId() {

        return id;

    }

 

    public void setId(int id) {

        this.id = id;

    }

 

    public String getName() {

        return name;

    }

 

    public void setName(String name) {

        this.name = name;

    }

 

    public String getCourse() {

        return course;

    }

 

    public void setCourse(String course) {

        this.course = course;

    }

}

然后我们在Service文件夹下创建StudentService.java文件

package com.raytseng.Service;

 

 

import com.raytseng.Dao.StudentDao;

import com.raytseng.Entity.Student;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

 

 

import java.util.Collection;

 

 

/**

* @author raytseng

* @create 2019/1/4-下午9:05

* @detail:

**/

 

 

@Service

public class StudentService {

 

 

    @Autowired

    private StudentDao studentDao;

 

 

    public Collection<Student> getAllStudents(){

        return studentDao.getAllStudents();

    }

 

 

    public Student getStudentById(int id)

    {

        return studentDao.getStudentById(id);

    }

}

 

 

Dao文件夹下创建StudentDao.java文件,这里我们不连接数据库,只进行简单的存储。

package com.raytseng.Dao;

 

 

import com.raytseng.Entity.Student;

import org.springframework.stereotype.Repository;

 

 

import java.util.Collection;

import java.util.HashMap;

import java.util.Map;

 

 

/**

* @author raytseng

* @create 2019/1/4-下午8:17

* @detail:

**/

 

 

@Repository

public class StudentDao {

    private static Map<Integer,Student> students;

 

 

    static {

        students = new HashMap<Integer, Student>(){

 

 

 

 

         {

            put(1, new Student(1, "Ray", "Computer Science"));

            put(2, new Student(2, "Said", "Finance"));

            put(3, new Student(3, "Anna", "Maths"));

         }

 

 

        };

    }

 

 

    public Collection<Student> getAllStudents(){

        return this.students.values();

    }

 

 

    public Student getStudentById(int id)

    {

        return this.students.get(id);

    }

}

 

 

然后我们创建简单的Controller文件StudentController.java文件。

package com.raytseng.Controller;

 

 

import com.raytseng.Entity.Student;

import com.raytseng.Service.StudentService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.PathVariable;

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

import org.springframework.web.bind.annotation.RequestMethod;

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

 

 

import java.util.Collection;

 

 

/**

* @author raytseng

* @create 2019/1/4-下午9:07

* @detail:

**/

 

 

@RestController

@RequestMapping("/students")

public class StudentController {

 

 

    @Autowired

    private StudentService studentService;

 

 

    @RequestMapping(method = RequestMethod.GET)

    public Collection<Student> getAllStudents(){

        return studentService.getAllStudents();

    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)

    public Student getStudentById(@PathVariable("id") int id)

    {

        return studentService.getStudentById(id);

    }

}

 

 

启动spring boot 我们在浏览器输入http://localhost:8080/students/1

得到下图

Spring boot项目搭建成功。

最后附上 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

 

 

    <groupId>com.raytseng</groupId>

    <artifactId>springBoot</artifactId>

    <version>1.0-SNAPSHOT</version>

 

 

    <parent>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-parent</artifactId>

        <version>2.0.5.RELEASE</version>

    </parent>

 

 

    <dependencies>

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-web</artifactId>

        </dependency>

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-devtools</artifactId>

            <optional>true</optional>

            <!-- optional=true,依赖不会传递,该项目依赖devtools;

            之后依赖boot项目的项目如果想要使用devtools,需要重新引入 -->

        </dependency>

        <!-- 添加thymeleaf依赖-->

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-thymeleaf</artifactId>

        </dependency>

        <!-- 添加mysql数据库依赖-->

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-data-jpa</artifactId>

        </dependency>

        <dependency>

            <groupId>mysql</groupId>

            <artifactId>mysql-connector-java</artifactId>

        </dependency>

    </dependencies>

 

 

 

 

 

 

    <properties>

        <java.version>1.9</java.version>

    </properties>

 

 

    <build>

        <plugins>

            <plugin>

                <groupId>org.springframework.boot</groupId>

                <artifactId>spring-boot-maven-plugin</artifactId>

                <configuration>

                    <fork>true</fork>

                </configuration>

            </plugin>

        </plugins>

    </build>

 

 

</project>

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值