Spring Boot学习笔记

使用SpringBoot搭建项目的理由

1.简化配置,避免了在搭建项目环境时写一堆没有技术含量却又不可缺少的xml。SpringBoot采取“约定优于配置”的策略,对于传统项目所必需的属性使用默认配置,有特定需求时再用配置文件对默认配置进行覆盖。
2.优化依赖,spring项目通常需要数十甚至上百的jar包,传统配置除了需要到处寻找、添加依赖,还要处理潜在的冲突,调整版本,而使用spring-boot-starter可以极大程度上避免这些问题。

建立SpringBoot项目
Spring Boot学习笔记_第1张图片
使用IDEA创建SpringBoot项目

创建完成后将.mvn、mvnw、mvnw.cmd三个文件删除

pom.xml


    4.0.0

    com.njfu
    bstabletest
    0.0.1-SNAPSHOT
    jar

    bstabletest
    bootstrap table test.

    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.4.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
            org.springframework.boot
            spring-boot-starter-aop
        

        
        
            org.springframework.boot
            spring-boot-devtools
            true
        

        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-actuator
        

        
        
            org.springframework.boot
            spring-boot-starter-freemarker
        

        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.0
        

        
            mysql
            mysql-connector-java
        

        
        
            com.alibaba
            druid
            1.0.29
        

        
        
            com.github.pagehelper
            pagehelper-spring-boot-starter
            1.1.1
        
    

    
        ${project.name}
        
            
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    true
                
            
        
    


推荐工程结构
Spring Boot学习笔记_第2张图片
springBoot工程结构

由于本例中集成了mybatis,选择新建一个mapper包

启动工程

将程序的main类放在其他类所在包的顶层
使用类注解@SpringBootApplication或@Configuration @EnableAutoConfiguration @Component组合注解
以Java Application或在项目根目录下用mvn spring-boot:run的方式启动项目

@SpringBootApplication
public class BsTableTestApplication {

    public static void main(String[] args) {
        SpringApplication.run(BsTableTestApplication.class, args);
    }
}
热部署配置

classpath下的文件有变动,就会自动重启应用(更新方式依赖于不同IDE)
位于/META-INF/maven、/META-INF/resources、/resources、/static、/public、/templates下的文件的更新不会重启应用,但会触发实时加载
SpringBoot从classpath下的/META-INF/resources、/resources、/static、/public或根目录提供静态内容



    org.springframework.boot
    spring-boot-devtools
    true



    org.springframework.boot
    spring-boot-maven-plugin
    
        true
    

在pom文件中使用以上两项配置,可以实现热部署。在classpath下的文件变动时,应用重启
在eclipse中通过 ctrl+s保存触发,IDEA中通过ctrl+F9 Build Project触发

属性文件配置

将application.properties或application.yml放在相应目录下作为属性文件。
可以放置的位置(按优先级排序):
1.当前目录下的/config子目录
2.当前目录
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
日志

默认日志(Logback)输出节点的含义:
1.日期和时间,精确到毫秒
2.日志级别:ERROR、WARN、INFO、DEBUG和TRACE
3.Process ID
4.---分隔符,分隔日志头信息
5.线程名
6.日志名(源class类名)
7.日志信息

项目打包

在项目根目录下使用maven指令
mvn package或 mvn package -DskipTests(跳过测试)进行打包
打包成可执行jar文件在target目录下

服务器部署

在linux服务器上使用nohup java -jar /path/to/xx.jar &启动

完整项目下载

一个结合bootstrap table进行单页面增删查改的小例子:Bootstrap-Table-test

Bootstrap Table实用配置:Bootstrap Table实用配置

你可能感兴趣的:(Spring Boot学习笔记)