当前位置: 移动技术网 > IT编程>开发语言>Java > 零基础搭建SpringBoot框架

零基础搭建SpringBoot框架

2020年04月20日  | 移动技术网IT编程  | 我要评论

  在一般互联网公司,一些技术框架无论是前端还是后台,都是有相当牛技术经验,技术经理和架构师来搭建,一般的技术人员是无法接触到这一块的。因此,这边只是满足一些小型的开发,同时主要目的还是从搭建的角度去了解springboot而已。话不多说开始搭建;

 

一、maven添加springboot相关依赖

<parent>
  <groupid>org.springframework.boot</groupid>
  <artifactid>spring-boot-starter-parent</artifactid><!-- 控制版本信息 -->
  <version>2.1.9.release</version> <!-- springboot的版本 -->
  <relativepath />
</parent>
<properties>
   <project.build.sourceencoding>utf-8</project.build.sourceencoding>
    <maven.compiler.source>1.8</maven.compiler.source><!-- 通过jdk为1.8编译 -->
    <maven.compiler.target>1.8</maven.compiler.target>
 </properties>

 <dependencies>
   <dependency>
     <groupid>org.springframework.boot</groupid>
     <!-- spring-boot-starter是springboot的核心启动器,包含了自动配置、日志和yaml -->
        <artifactid>spring-boot-starter-web</artifactid><!-- springboot web模块支持,自动帮我们引入了web模块开发需要的相关jar包-->
  </dependency>
  <dependency>
        <groupid>org.springframework.boot</groupid>
        <artifactid>spring-boot-starter-test</artifactid><!-- springboot程序测试依赖,如果是自动创建项目默认添加 -->
        <scope>test</scope>
    </dependency>
 </dependencies>

以上的依赖已经将springboot部署到项目中了。

二、配置application.yml(或application.properties)文件

  yml(或者yaml)文件相当于ssm框架的xml配置信息一样,因此,它会将你所用到的信息在此文件编辑,该文件放在src/main/resources路径下,下面以yml形式来配置:

 

quartz:
    enabled: true #开启定时任务
server:
    port: 8080 #指定启动端口号
    servlet:
        context-path: /**/**
spring:
    output:
        ansi:
            enabled: always
  #数据库相关配置
    datasource:
        name: ****
        url: jdbc:mysql://r************:3306/***?useunicode=true&characterencoding=utf-8&servertimezone=gmt%2b8
        username: ****
        password: ****
        #配置初始化大小/最小/最大
        initial-size: 1
        min-idle: 1
        max-active: 20
        #获取连接等待超时时间
        max-wait: 60000
        #间隔多久进行一次检测,检测需要关闭的空闲连接
        time-between-eviction-runs-millis: 60000
        #一个连接在池中最小生存的时间
        min-evictable-idle-time-millis: 300000
        validation-query: select 'x'
        test-while-idle: true
        test-on-borrow: false
        test-on-return: false
        #打开pscache,并指定每个连接上pscache的大小。oracle设为true,mysql设为false。分库分表较多推荐设置为false
        pool-prepared-statements: false
        max-pool-prepared-statement-per-connection-size: 20

## 该配置节点为独立的节点,有很多同学容易将这个配置放在spring的节点下,导致配置无法被识别
mybatis:
  mapper-locations: classpath:mapper/*.xml  #注意:一定要对应mapper映射xml文件的所在路径


#log日志
logging:
    config: classpath:logback-spring.xml
    path: d:/log/logs

 

三、pom添加数据库依赖(以mysql为主)

 

<dependency>
    <groupid>org.mybatis.spring.boot</groupid>
    <artifactid>mybatis-spring-boot-starter</artifactid>
    <version>2.0.0</version>
</dependency>
<dependency>
    <groupid>mysql</groupid>
    <artifactid>mysql-connector-java</artifactid>
</dependency>

 

  这样对于一个一般小型的springboot框架就搭建好了,下来就跟原来的项目一样,配置相关pom依赖,新建实体类、service接口、controller类,去继续完成一个项目。

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网