SpringBoot快速入门与基本操作指南SpringBoot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。通过使用特定的配置方式,SpringBoot使开发人员不再需要定义样板化的配置,从而显著提高了开发效率。一、创建SpringBoot项目1. 使用IntelliJ IDEA创建项目打开IntelliJ IDEA,选择“New Project”。选择“Spring Initializr”,填写项目信息,包括项目名称、组ID、Java版本等。选择所需的依赖,例如Web、Thymeleaf等。点击“Finish”完成项目创建。2. 使用Spring Initializr官网创建项目访问Spring Initializr官网(填写项目信息并选择所需的依赖。点击“Generate”生成项目并下载。将下载的项目解压并导入IDEA。二、项目结构创建完成后,SpringBoot项目的基本结构如下:∣−−src∣−−main∣−−java∣−−com.example∣−−DemoApplication.java(启动类)∣−−controller∣−−HelloController.java∣−−service∣−−entity∣−−resources∣−−application.properties(配置文件)∣−−static(静态资源)∣−−templates(模板文件)∣−−test∣−−java∣−−com.example∣−−DemoApplicationTests.java(测试类)|-- src
|-- main
|-- java
|-- com.example
|-- DemoApplication.java (启动类)
|-- controller
|-- HelloController.java
|-- service
|-- entity
|-- resources
|-- application.properties (配置文件)
|-- static (静态资源)
|-- templates (模板文件)
|-- test
|-- java
|-- com.example
|-- DemoApplicationTests.java (测试类)∣−−src∣−−main∣−−java∣−−com.example∣−−DemoApplication.java(启动类)∣−−controller∣−−HelloController.java∣−−service∣−−entity∣−−resources∣−−application.properties(配置文件)∣−−static(静态资源)∣−−templates(模板文件)∣−−test∣−−java∣−−com.example∣−−DemoApplicationTests.java(测试类)三、编写Hello World示例1. 创建控制器在包下创建一个类:packagecom.example.demo.controller;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.RestController;@RestControllerpublicclassHelloController@GetMapping("/hello")publicStringhello()return"HelloWorld";package com.example.demo.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello() {
return "Hello World";
}
}packagecom.example.demo.controller;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.RestController;@RestControllerpublicclassHelloController@GetMapping("/hello")publicStringhello()return"HelloWorld";2. 启动项目运行包下的类中的方法:packagecom.example.demo;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublicclassDemoApplicationpublicstaticvoidmain(String[]args)SpringApplication.run(DemoApplication.class,args);package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}packagecom.example.demo;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublicclassDemoApplicationpublicstaticvoidmain(String[]args)SpringApplication.run(DemoApplication.class,args);3. 访问页面在浏览器中访问,可以看到页面输出“Hello World”。四、配置数据源在或文件中配置数据源:# application.yml
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/mydb?characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false
username: root
password: 123456五、创建实体类和Mapper1. 创建实体类使用Lombok插件简化实体类的编写:packagecom.example.demo.entity;importlombok.Data;importjavax.persistence.Entity;importjavax.persistence.GeneratedValue;importjavax.persistence.GenerationType;importjavax.persistence.Id;@Data@EntitypublicclassPeople@Id@GeneratedValue(strategy=GenerationType.IDENTITY)privateIntegerid;privateStringname;privateStringemail;package com.example.demo.entity;
import lombok.Data;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Data
@Entity
public class People {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
private String email;
}packagecom.example.demo.entity;importlombok.Data;importjavax.persistence.Entity;importjavax.persistence.GeneratedValue;importjavax.persistence.GenerationType;importjavax.persistence.Id;@Data@EntitypublicclassPeople@Id@GeneratedValue(strategy=GenerationType.IDENTITY)privateIntegerid;privateStringname;privateStringemail;2. 创建Mapper在包下创建接口:packagecom.example.demo.mapper;importcom.example.demo.entity.People;importorg.apache.ibatis.annotations.Mapper;importorg.apache.ibatis.annotations.Select;importjava.util.List;@MapperpublicinterfacePeopleMapper@Select("SELECT∗FROMpeople")List<People>findAllPeople();package com.example.demo.mapper;
import com.example.demo.entity.People;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface PeopleMapper {
@Select("SELECT * FROM people")
List<People> findAllPeople();
}packagecom.example.demo.mapper;importcom.example.demo.entity.People;importorg.apache.ibatis.annotations.Mapper;importorg.apache.ibatis.annotations.Select;importjava.util.List;@MapperpublicinterfacePeopleMapper@Select("SELECT∗FROMpeople")List<People>findAllPeople();六、创建Service和Controller1. 创建Service在包下创建类:packagecom.example.demo.service;importcom.example.demo.entity.People;importcom.example.demo.mapper.PeopleMapper;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Service;importjava.util.List;@ServicepublicclassPeopleService@AutowiredprivatePeopleMapperpeopleMapper;publicList<People>findAll()returnpeopleMapper.findAllPeople();package com.example.demo.service;
import com.example.demo.entity.People;
import com.example.demo.mapper.PeopleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class PeopleService {
@Autowired
private PeopleMapper peopleMapper;
public List<People> findAll() {
return peopleMapper.findAllPeople();
}
}packagecom.example.demo.service;importcom.example.demo.entity.People;importcom.example.demo.mapper.PeopleMapper;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Service;importjava.util.List;@ServicepublicclassPeopleService@AutowiredprivatePeopleMapperpeopleMapper;publicList<People>findAll()returnpeopleMapper.findAllPeople();2. 创建Controller在包下创建类:packagecom.example.demo.controller;importcom.example.demo.entity.People;importcom.example.demo.service.PeopleService;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.RestController;importjava.util.List;@RestControllerpublicclassPeopleController@AutowiredprivatePeopleServicepeopleService;@GetMapping("/people")publicList<People>getAllPeople()returnpeopleService.findAll();package com.example.demo.controller;
import com.example.demo.entity.People;
import com.example.demo.service.PeopleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class PeopleController {
@Autowired
private PeopleService peopleService;
@GetMapping("/people")
public List<People> getAllPeople() {
return peopleService.findAll();
}
}packagecom.example.demo.controller;importcom.example.demo.entity.People;importcom.example.demo.service.PeopleService;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.RestController;importjava.util.List;@RestControllerpublicclassPeopleController@AutowiredprivatePeopleServicepeopleService;@GetMapping("/people")publicList<People>getAllPeople()returnpeopleService.findAll();七、单元测试在目录下编写单元测试用例:packagecom.example.demo;importcom.example.demo.mapper.PeopleMapper;importorg.junit.jupiter.api.Test;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.boot.test.context.SpringBootTest;importjava.util.List;importstaticorg.junit.jupiter.api.Assertions.assertEquals;@SpringBootTestclassPeopleMapperTest@AutowiredprivatePeopleMapperpeopleMapper;@TestvoidtestFindAllPeople()List<People>people=peopleMapper.findAllPeople();assertEquals(3,people.size());//检查返回的数据条数是否为3package com.example.demo;
import com.example.demo.mapper.PeopleMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
@SpringBootTest
class PeopleMapperTest {
@Autowired
private PeopleMapper peopleMapper;
@Test
void testFindAllPeople() {
List<People> people = peopleMapper.findAllPeople();
assertEquals(3, people.size()); // 检查返回的数据条数是否为3
}
}packagecom.example.demo;importcom.example.demo.mapper.PeopleMapper;importorg.junit.jupiter.api.Test;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.boot.test.context.SpringBootTest;importjava.util.List;importstaticorg.junit.jupiter.api.Assertions.assertEquals;@SpringBootTestclassPeopleMapperTest@AutowiredprivatePeopleMapperpeopleMapper;@TestvoidtestFindAllPeople()List<People>people=peopleMapper.findAllPeople();assertEquals(3,people.size());//检查返回的数据条数是否为3八、部署项目SpringBoot项目可以被打包成可执行的JAR或WAR文件。在中添加插件配置:<build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring−boot−maven−plugin</artifactId></plugin></plugins></build><build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring−boot−maven−plugin</artifactId></plugin></plugins></build>使用Maven命令打包:mvncleanpackagemvn clean packagemvncleanpackage生成JAR文件后,使用以下命令启动项目:java−jartarget/myapp.jarjava -jar target/myapp.jarjava−jartarget/myapp.jar九、总结本文介绍了SpringBoot的基本概念和快速入门方法。通过创建一个简单的“Hello World”项目,展示了如何配置数据源、编写控制器、服务和实体类,以及如何进行单元测试和部署项目。SpringBoot的简化配置和快速开发特性使其成为现代Java开发的首选框架之一。