springboot是刚毕业的时候干过半年的开发,当时身为后端引擎研发工程师,半路转向大数据方向.
先写一个简单的helloword程序吧
1 先建立一个maven工程
2 在pom中引入基础依赖
<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>org.example</groupId>
<artifactId>helloSpringBoot</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>helloSpringBoot</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--MySQL驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--jdbc连接数据库-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
定义一个Controller
package com.byd.dev.zhu.Controller.Imp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;
@RestController
public class HelloController {
@Autowired
private JdbcTemplate jdbcTemplate;
@GetMapping("/")
public String index(){
return "Greetings from Spring Boot!";
}
@GetMapping("/hello")
@ResponseBody
public List<Map<String, Object>> hello(){
String sql = "select * from hr_person";
List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
return maps;
}
@GetMapping("/word")
public String word(){
return "templates/index.html";
}
@GetMapping("/miss")
public String miss(){
return "Greetings miss from Spring Boot!";
}
}
目录结构如下
、
创建一个启动类注册spring
package com.byd.dev.zhu;
import javafx.application.Application;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class APP {
public static void main(String[] args) {
ConfigurableApplicationContext configurableApplicationContext= SpringApplication.run(APP.class, args);
String listname[] = configurableApplicationContext.getBeanDefinitionNames();
for(String x:listname){
System.out.println(x);
}
}
}
启动运行
后台执行命令为:
C:\Program Files\Java\jdk1.8.0_25\bin\java.exe -jar .\helloSpringBoot-1.0-SNAPSHOT.jar
页面访问地址