导语
你好! 这是我第一次写 SpringBoot 的快速搭建。如果你想学习如何快速搭建SpringBoot, 可以仔细阅读这篇文章,了解一下SpringBoot的基本知识。
一.快速生成
访问链接: 快速生成SpringBoot.
步骤:Project中选择Maven Project
Language选择Java
Spring Boot版本选择,我这里选择2.26
Project Metadata根据自己的选择设置
最后点击 GENERATE CTRL 即可下载
解压到工作空间导入至idea
下面展示 SpringBootApplication
。
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(basePackages = {"(controller包地址)","(service包地址)","(utils包地址)"})
@MapperScan(basePackages = {"(dao地址)"})
public class TyApplication {
public static void main(String[] args) {
SpringApplication.run(TyApplication.class, args);
System.err.print("\033[34;4m" + "================================================Our Project-----Runing=====================================" + "\033[0m");
}
}
连接数据库 application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/库名
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
依赖 pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.30</version>
</dependency>
<!--访问静态资源图片-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
写一个Controller
测试
@Controller
public class IndexController {
@RequestMapping("/index")
@ResponseBody
public Map<String,String> index (){
Map map = new HashMap<>();
map.put("1","123");
map.put("2","222");
return map;
}
}
网址输入http://localhost:8080/index
展示数据{"1":"耿耿","2":"程序员"}
成功