Spring Boot+Vue+Element前后端分离入门(三):后端项目创建

  • 使用Spring Initializr快速创建向导,点击 Next。


    image.png
  • 输入项目元数据,Next


    image.png

    因为JDK是8所以选项Java版本8


    image.png

    选择要添加的模块。创建向导会自动添加所需依赖。Next。
    image.png
  • 点击Finish创建项目,等待依赖导入。


    image.png
  • 在pom.xml中引入Druid数据源依赖
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.17</version>
        </dependency>
  • 在pom.xml中引入MyBatis-Plus依赖
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.1</version>
        </dependency>

(由于使用了MyBatis-Plus,可以把创建项目时引入的MyBatis注释掉或者删掉,如果使用MyBatis可以无视MyBatis-Plus引入的部分)

  • 在resources中创建一个application.yml配置文件
    image.png
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/test
    username: root
    password: 123456
    driver-class-name: com.mysql.cj.jdbc.Driver

    druid:
      aop-patterns: com.franken.boot.*  #监控这个包下面的所有组件
      filters: stat,wall     # 底层开启功能,stat(sql监控),wall(防火墙)

      stat-view-servlet:   # 配置监控页功能
        enabled: true
        login-username: admin
        login-password: admin
        resetEnable: false

      web-stat-filter:  # 监控web
        enabled: true
        urlPattern: /*
        exclusions: '*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*'


      filter:
        stat:    # 对上面filters里面的stat的详细配置
          slow-sql-millis: 1000
          logSlowSql: true
          enabled: true
        wall:
          enabled: true
          config:
            drop-table-allow: false
  • 新建一个Controller用来测试


    image.png
package com.franken.boot.Controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    @RequestMapping("/hello")
        public String handle() {
        return "Hello,Spring Boot 2";
    }
}
  • 运行项目,浏览器打开localhost:8080/hello可以看到Hello,Spring Boot 2
    image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容