美文网首页
Spring Boot学习笔记

Spring Boot学习笔记

作者: wch853 | 来源:发表于2017-07-22 11:07 被阅读191次
    使用SpringBoot搭建项目的理由

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

    建立SpringBoot项目
    使用IDEA创建SpringBoot项目

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

    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数据源 -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>1.0.29</version>
            </dependency>
    
            <!-- 分页插件 -->
            <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插件,用于将应用打成可执行jar包 -->
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <fork>true</fork>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    
    </project>
    
    推荐工程结构
    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或根目录提供静态内容

    <!-- 热部署工具 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional>
    </dependency>
    
    <!-- spring boot maven插件,用于将应用打成可执行jar包 -->
    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
            <fork>true</fork>
        </configuration>
    </plugin>
    

    在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学习笔记

          本文链接:https://www.haomeiwen.com/subject/avlekxtx.html