第三步:追加 Spring Security、Thymeleaf 等配置项

蒲公英 提交于 周四, 06/04/2020 - 22:44
Spring Boot、MVC 、Security + Mybatis + Thymeleaf

本教程架构当前较流行的 Java 技术:
Spring Boot、MVC 、Security + Mybatis + Mysql + Thymeleaf 

 

我们使用 YAML 作为配置文件。

 

 

 

  1. 重命名 application.properties 为 application.yml
  2. 追加配置
    spring:
      datasource:
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://127.0.0.1:3306/demo?characterEncoding=utf-8&serverTimezone=UTC&useSSL=false
        username: root
        password: 
        
      thymeleaf:
        enabled: true
        encoding: utf-8
        prefix: classpath:/templates/
        cache: false
        mode: HTML
        suffix: .html
    
    mybatis:
      mapper-locations: classpath:mapper/*.xml
      type-aliases-package: com.example.demo.entity
      configuration:
        map-underscore-to-camel-case: true
    
  3. 修改 DemoApplication.java
    package com.example.demo;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @SpringBootApplication
    @RestController
    public class DemoApplication {
        
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
        
        @GetMapping("/hello")
        public String hello() {
            return String.format("Hello World!");
        }
    
    }
    
  4. 启动工程
    项目右键 -> Run As -> Spring Boot App
  5. 浏览器访问工程
    http://localhost:8080/hello
  6. 因为集成了 Spring Security,会弹出 Login 画面
    用户名:user
    密码:表示在控制台内

页面输出以下内容说明配置成功:
Hello World!

添加新评论