相比于springmvc,spring,mybatis复杂的整合过程,springboot确实是方便了太多太多。 这里我们使用IntelliJ IDEA来完成spring项目的搭建。
这里根据自己的需要进行选择后期都是可以自行添加的。因为后面要使用mybatis连接数据库所以将mybatis也选择上了。
此时的目录结构是这样的
我们来看一下FirstspringbootApplication中的内容,@SpringBootApplication 标注的类为 Spring Boot 的主配置类,Spring Boot 会运行这个类的 main 方法来启动 Spring Boot 应用。
@SpringBootApplication
public class FirstspringbootApplication {
public static void main(String[] args) {
SpringApplication.run(FirstspringbootApplication.class, args);
}
}
pom.xml在这里可以对使用的包进行配置,需要用到哪个包可以到https://mvnrepository.com/这个网站进行搜索。
我们来查看一下此时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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>firstspringboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>firstspringboot</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</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-web-services</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
现在我们来测试一下。首先我们要将pom中的包给注释掉,不然运行起来会报错就是下面这个<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.0.1</version> </dependency>
新建一个类,注意这个类的位置与FirstspringbootApplication在同一目录下
public class HellowWorld {
@RequestMapping("/hellow")
public String Hellow(){
return "HellowWorld";
}
}
运行程序,在浏览器中输入http://localhost:8080/hellow,浏览器中就会显示
好了至此我们的springboot项目已经搭建成功了。