Java Spring 生态系统

2021-08-11  本文已影响0人  bowen_wu

概述

  1. 在 Spring 容器中引入 Bean
  2. Spring + MyBatis(H2 | MySQL | Postgres)
  3. 模板引擎 => 后端渲染 HTML => 后端直接返回带有数据的 HTML
  4. 前后端分离 vs 后端渲染
    • 前端渲染 => 前端通过 JavaScript 进行数据的获取,动态的渲染到页面中 =>

后端开发

应用逻辑更加清晰 + 代码复用

  1. Controller => 做和 HTTP 请求相关 => 接收和解析参数,调用 Service 方法,获得响应之后封装成结果返回
  2. Service => 业务代码
  3. Dao => 做数据库增删改查

Spring + MyBatis

Configuration

// resources/application.properties
spring.datasource.url=
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=org.h2.Driver

mybatis.config-location=classpath:db/mybatis/config.xml // 告知 Spring mybatis 的配置文件在哪里

// resources/db/mybatis/config.xml => 主要配置 mapper

模板引擎

Freemaker
FreeMaker 对 Spring 继承约定 => resources/templates 目录下存储模板引擎文件 => index.ftlh
Spring Boot ModuleAndView == index.ftlh + 数据 => ModuleAndView == 模板引擎 + 数据 => ModuleAndView class => new ModuleAndView(<模板引擎 file>, model);

项目文件

main/java/example

知识点

  1. spring-boot-starter-xxx => SpringBoot 模块
  2. flyway => 数据库迁移插件
  3. 在 Spring 中所有的需要当做 Bean 去管理的可以加 @Service | @Component => 使用注解告知 Spring 这个类是一个 Bean,是一个 Service,是一个未来要把这个类变成对象,这个对象可以被依赖,可以依赖别人,可以进行依赖注入,可以去完成非常复杂的操作
  4. 在 Spring 中使用 @Autowired 进行依赖的装配和运行
  5. Spring 约定,放在 static 目录下文件可以在浏览器中直接访问 => resources/static/index.html => http://localhost:8080/index.html
上一篇 下一篇

猜你喜欢

热点阅读