SpringBoot
maven仓库管理需要在主pom.xml加入:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
</parent>
jar依赖导入:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
boot启动
启动需要一个类的main方法进入程序运行
@SpringBootApplication
public class ApplicationBoot {
public static void main(String[] args) {
SpringApplication.run(ApplicationBoot.class, args);
}
}
@SpringBootApplication
自动配置文件,自动扫描上下文件夹的java包.
热启动 idea alt+f9
jar依赖 pom.xml配置
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
<scope>true</scope>
</dependency>
maven插件必须有才会生效 pom.xml配置
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
打包运行
打包构建设置 pom.xml配置
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
springboot配置文件
在资源resources包下为
Application.yml(一般用这个)或者Application.properties
server:
port: 8081 //启动端口号
spring:
profiles:
active: prod //环境配置名字
环境配置
可以直接创建一个yml文件,名字直接用-分割 比如Application-prod.yml 在里面配置想要的环境属性
访问静态资源
导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
配置它可以访问/resources /static /templates /public下的html文件 html可以使用命名空间
<html lang="en" xmlns:th="http://www.thymeleaf.org">
访问js等资源文件
<bulid>
<resources>
<resource>
<directory>src/main/</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<!--加载模板文件-->
<include>**/*.html</include>
<include>**/*.js</include>
<include>**/*.css</include>
<include>**/*.png</include>
<include>**/*.gif</include>
<!--加载静态文件-->
<include>/static/</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</bulid>
Mybatis整合
全部依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</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>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>