- spring容器能做到事springboot都能做到,更简易,springboot完全抛弃了繁琐的xml文件配置方式,而是替代性的用注解方式来实现;
- springboot集成的插件很多,所以使用许多服务都只是引入一个依赖,几个注解和java类就可以了;
- web应用开发一般都是打包成war包,再发布到相关服务器容器下,springboot下常见的形式是将springboot应用打包成可执行的jar包文件,因为我们可以直接将springboot应用看成是一个java application,其web应用可以没有webapp目录,因此也没有web.xml文件了;他推荐使用html页面,并将其作为静态资源使用;
idea中创建一个springboot+maven+mybatis+mysql项目:
自动初始化引入依赖我们选择web下的web以及sql下的mysql和mybatis,可以根据自己的需要选择,版本这里我选择了2.0.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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sydal</groupId>
<artifactId>springboot-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot-demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<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.3.2</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>
程序启动会自动生成一个SpringbootDemoApplication类,该类有一个@SpringbootApplication注解,该注解表明该类是程序的入口
我们新建一个controller类,写一个接口,测试我们的springboot项目
启动项目,结果报以下错误:
解决方案:加上这一句,启动正常
访问http://localhost:8080/hello:
自此,一个springboot的入门demo就算完成了;
b