springboot简单使用

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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值