一:用idea 创建 springboot 项目:
详情请参考:《使用IDEA创建一个springboot项目》
二:具体代码内容
1:StartAppLication
package com.alancode.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; /** * @program: Hello_springBoot-demo1 * @description: StartAppLication 项目启动器 * <p> * 注意: * 关于编写启动器需要注意的问题。 * 启动器存放位置:启动器可以和controller 位于同一个包 package下,或者位于controller的上一级包。 * 但是不能把该文件放到controller平行级的包或者其子包下。 * @author: Alan_Liu * @create: 2021-02-14 21:47 */ @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) public class StartAppLication { /** * 调用 springboot 启动 * * @return * @throws * @Param: [args] * @author Alan_Liu * @date 2021/2/14 22:16 **/ public static void main(String[] args) { /*** * * *@Param: [args] *@return *@throws *@author Alan_Liu *@date 2021/2/14 22:23 * **/ SpringApplication.run(StartAppLication.class, args); } }
2:HelloWorld
package com.alancode.springboot.controller; import org.springframework.stereotype.*; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import java.util.HashMap; import java.util.Map; /** * @program: Hello_springBoot-demo1 * @description: HelloWorld * @author: Alan_Liu * @create: 2021-02-14 21:47 */ @Controller public class HelloWorld { /** * @return * @throws * @Param: [] * @author Alan_Liu * @date 2021/2/14 21:52 **/ @RequestMapping("/hello") @ResponseBody public Map<String, Object> showHelloWolrd() { Map<String, Object> map = new HashMap<String, Object>(); map.put("msg", "HelloWorld"); return map; } }
3: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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.2</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.AlanCode.springBoot</groupId> <artifactId>demo1</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo1</name> <description>Demo project for Spring Boot</description> <properties> <!--設置 jdk版本--> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!--注入 springboot启动器 所谓springboot 启动器 其实就是 一些jar包的集合。springboot 一共提供了44款启动器 1:spring-boot-starter-web: 支持全栈式的web开发,包括 tomcat springmvc 等jar. 2:spring-boot-starter-jdbc 支持spring以jdbc方式操作数据库的jar包的集合。 3:spring-boot-starter-redis --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>