本教程架构当前较流行的 Java 技术:
Spring Boot、MVC 、Security + Mybatis + Mysql + Thymeleaf
我们使用 YAML 作为配置文件。
- 重命名 application.properties 为 application.yml
- 追加配置
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
- 修改 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!"); } }
- 启动工程
项目右键 -> Run As -> Spring Boot App - 浏览器访问工程
http://localhost:8080/hello - 因为集成了 Spring Security,会弹出 Login 画面
用户名:user
密码:表示在控制台内
页面输出以下内容说明配置成功:
Hello World!