使用SpringBoot搭建項(xiàng)目的理由
1.簡(jiǎn)化配置陨闹,避免了在搭建項(xiàng)目環(huán)境時(shí)寫一堆沒有技術(shù)含量卻又不可缺少的xml流妻。SpringBoot采取“約定優(yōu)于配置”的策略堪遂,對(duì)于傳統(tǒng)項(xiàng)目所必需的屬性使用默認(rèn)配置娩怎,有特定需求時(shí)再用配置文件對(duì)默認(rèn)配置進(jìn)行覆蓋潜腻。
2.優(yōu)化依賴,spring項(xiàng)目通常需要數(shù)十甚至上百的jar包咖祭,傳統(tǒng)配置除了需要到處尋找掩宜、添加依賴,還要處理潛在的沖突么翰,調(diào)整版本牺汤,而使用spring-boot-starter可以極大程度上避免這些問題。
建立SpringBoot項(xiàng)目
創(chuàng)建完成后將.mvn浩嫌、mvnw檐迟、mvnw.cmd三個(gè)文件刪除
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.njfu</groupId>
<artifactId>bstabletest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>bstabletest</name>
<description>bootstrap table test.</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.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</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<!-- 熱部署工具 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- 使用freemarker作為模板引擎 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<!-- 集成mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- 使用druid數(shù)據(jù)源 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.29</version>
</dependency>
<!-- 分頁(yè)插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
<build>
<finalName>${project.name}</finalName>
<plugins>
<!-- spring boot maven插件补胚,用于將應(yīng)用打成可執(zhí)行jar包 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
</project>
推薦工程結(jié)構(gòu)
由于本例中集成了mybatis,選擇新建一個(gè)mapper包
啟動(dòng)工程
將程序的main類放在其他類所在包的頂層
使用類注解@SpringBootApplication或@Configuration @EnableAutoConfiguration @Component組合注解
以Java Application或在項(xiàng)目根目錄下用mvn spring-boot:run的方式啟動(dòng)項(xiàng)目
@SpringBootApplication
public class BsTableTestApplication {
public static void main(String[] args) {
SpringApplication.run(BsTableTestApplication.class, args);
}
}
熱部署配置
classpath下的文件有變動(dòng)追迟,就會(huì)自動(dòng)重啟應(yīng)用(更新方式依賴于不同IDE)
位于/META-INF/maven溶其、/META-INF/resources、/resources敦间、/static瓶逃、/public、/templates下的文件的更新不會(huì)重啟應(yīng)用廓块,但會(huì)觸發(fā)實(shí)時(shí)加載
SpringBoot從classpath下的/META-INF/resources厢绝、/resources、/static带猴、/public或根目錄提供靜態(tài)內(nèi)容
<!-- 熱部署工具 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<!-- spring boot maven插件昔汉,用于將應(yīng)用打成可執(zhí)行jar包 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
在pom文件中使用以上兩項(xiàng)配置,可以實(shí)現(xiàn)熱部署拴清。在classpath下的文件變動(dòng)時(shí)靶病,應(yīng)用重啟
在eclipse中通過 ctrl+s保存觸發(fā),IDEA中通過ctrl+F9 Build Project觸發(fā)
屬性文件配置
將application.properties或application.yml放在相應(yīng)目錄下作為屬性文件口予。
可以放置的位置(按優(yōu)先級(jí)排序):
1.當(dāng)前目錄下的/config子目錄
2.當(dāng)前目錄
3.classpath下的/config包
4.classpath根路徑
# server
server:
port: 8010
context-path: /bstabletest
# spring
spring:
# datasource
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/bs_table_test?useUnicode=true&characterEncoding=utf-8
username: root
password:
type: com.alibaba.druid.pool.DruidDataSource
# freemarker
freemarker:
cache: false
charset: UTF-8
content-type: text/html
suffix: .html
# favicon
mvc:
favicon:
enabled: false
# ecoding
http:
encoding:
force: true
# mybatis
mybatis:
config-location: classpath:config/mybatis-config.xml
type-aliases-package: com.njfu.bstabletest.domain
mapper-locations: classpath:mapper/*.xml
# log
logging:
level:
# show sql
com:
njfu:
bstabletest:
mapper: trace
# log path
file: log/spring.log
日志
默認(rèn)日志(Logback)輸出節(jié)點(diǎn)的含義:
1.日期和時(shí)間娄周,精確到毫秒
2.日志級(jí)別:ERROR、WARN苹威、INFO昆咽、DEBUG和TRACE
3.Process ID
4.---分隔符,分隔日志頭信息
5.線程名
6.日志名(源class類名)
7.日志信息
項(xiàng)目打包
在項(xiàng)目根目錄下使用maven指令
mvn package或 mvn package -DskipTests(跳過測(cè)試)進(jìn)行打包
打包成可執(zhí)行jar文件在target目錄下
服務(wù)器部署
在linux服務(wù)器上使用nohup java -jar /path/to/xx.jar &啟動(dòng)
完整項(xiàng)目下載
一個(gè)結(jié)合bootstrap table進(jìn)行單頁(yè)面增刪查改的小例子:Bootstrap-Table-test
Bootstrap Table實(shí)用配置:Bootstrap Table實(shí)用配置