Java笔记:SpringBoot开发常用技术整合(1)

简介: Java笔记:SpringBoot开发常用技术整合

一、构建springboot工程

参考源码地址

https://github.com/leechenxiang/imooc-springboot-starter

可选IDE

STS Spring Tool Suit

快速开始:https://spring.io/quickstart

配置文件 application.properties

############################################################
#
# 开发模式设置
#
############################################################
# 热部署生效
spring.devtools.restart.enabled=true
# 监听目录
spring.devtools.restart.additional-paths=src/main/java
#spring.devtools.restart.exclude=static/**,public/**
#spring.devtools.restart.exclude=WEB-INF/**
############################################################
#
# server 服务端配置
#
############################################################
server.port=8080
#server.servlet.context-path=/demo
#server.error.path=/error
#server.address=192.168.1.2
#server.session-timeout=60
############################################################
#
# server.tomcat 服务端配置
#
############################################################
#server.tomcat.max-threads=250
server.tomcat.uri-encoding=UTF-8
#server.tomcat.basedir=H:/springboot-tomcat-tmp
#server.tomcat.access-log-enabled=true
#server.tomcat.access-log-pattern=
#server.tomcat.accesslog.directory=
#logging.path=H:/springboot-tomcat-tmp
#logging.file=myapp.log

二、接口返回json


# 时间格式化
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8
spring.jackson.serialization.write-dates-as-timestamps=false
package com.example.demo.pojo;
/**
 * 统一的返回封装
 */
public class JsonResult {
    private Integer code;
    private String msg;
    private Object data;
    public JsonResult(Integer code, String msg, Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }
    public static JsonResult success(Object data){
        return new JsonResult(0, "success", data);
    }
    public static JsonResult error(String errorMessage) {
        return new JsonResult(-1, errorMessage, null);
    }
    public Integer getCode() {
        return code;
    }
    public void setCode(Integer code) {
        this.code = code;
    }
    public String getMsg() {
        return msg;
    }
    public void setMsg(String msg) {
        this.msg = msg;
    }
    public Object getData() {
        return data;
    }
    public void setData(Object data) {
        this.data = data;
    }
}
package com.example.demo.pojo;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import java.util.Date;
/**
 * jackson注解使用示例
 */
@Data
public class User {
    private String name;
    private Integer age;
    // 忽略显示
    @JsonIgnore
    private String password;
    // 格式化
    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss", locale="zh", timezone="GMT+8")
    private Date birthday;
    // 为空不显示
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String desc;
}

三、热部署

<!--热部署-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <optional>true</optional>
</dependency>

IDEA需要做额外的配置

四、资源属性配置

resource.properties

# 配置文件
com.demo.name=MyBlog
com.demo.language=zh
package com.example.demo.pojo;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
/**
 * 资源文件中的配置映射到实体类
 */
@Configuration
@ConfigurationProperties(prefix = "com.demo")
@PropertySource(value = "classpath:resource.properties")
public class Resource {
    private String name;
    private String language;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getLanguage() {
        return language;
    }
    public void setLanguage(String language) {
        this.language = language;
    }
}

五、模板引擎

<!--模板引擎 freemarker-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<!--模板引擎 thymeleaf-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
############################################################
#
# freemarker
#
############################################################
# 上线改为true
spring.freemarker.cache=false
#spring.freemarker.template-loader-path=classpath:/templates
#spring.freemarker.charset=UTF-8
#spring.freemarker.check-template-location=true
#spring.freemarker.content-type=text/html
#spring.freemarker.expose-request-attributes=true
#spring.freemarker.expose-session-attributes=true
#spring.freemarker.request-context-attribute=request
#spring.freemarker.suffix=.ftl
############################################################
#
# thymeleaf
#
#############################################################
# 上线改为true
spring.thymeleaf.cache=false
#spring.thymeleaf.prefix=classpath:/templates/
#spring.thymeleaf.suffix=.html
#spring.thymeleaf.mode=HTML5
#spring.thymeleaf.encoding=UTF-8
#spring.thymeleaf.servlet.content-type=text/html
# 静态文件
spring.mvc.static-path-pattern=/static/**

freemarker

<h2>freemarker</h2>
<p>${resource.name}</p>
<p>${resource.language}</p>


thymeleaf

基本使用方式

对象引用方式

时间类型转换

text与utext

URL

引入静态资源文件js/css

条件判断th:if与th:unless

循环th:each

分支th:switch与th:case

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script th:src="@{/static/js/index.js}"></script>
</head>
<body>
<h2>基本语法</h2>
<p><input type="text" th:value="${user.name}"  th:id="${user.name}" th:name="${user.name}"></p>
<p><input type="text" th:value="${user.age}"></p>
<p><input type="text" th:value="${user.birthday}"></p>
<h2>日期格式化</h2>
<p><input type="text" th:value="${#dates.format(user.birthday, 'yyyy-MM-dd')}"></p>
<h2>简便写法</h2>
<div th:object="${user}">
    <p><input type="text" th:value="*{name}"  th:id="*{name}" th:name="*{name}"></p>
    <p><input type="text" th:value="*{age}"></p>
    <p><input type="text" th:value="*{birthday}"></p>
</div>
<h2>text与utext</h2>
<p>text: <span th:text="${user.desc}"></span></p>
<p>utext: <span th:utext="${user.desc}"></span></p>
<h2>网址</h2>
<a th:href="@{https://www.baidu.com/}">www.baidu.com</a>
<h2>表单</h2>
<form th:action="@{/thymeleaf/user}" method="post" th:object="${user}">
    <!-- field == id, name, value -->
    <input type="text" th:field="*{name}">
    <input type="submit">
</form>
<h2>判断</h2>
<p th:if="${user.age} == 20"> age == 20</p>
<p th:if="${user.age} gt 20"> age > 20</p>
<p th:if="${user.age} lt 20"> age < 20</p>
<p th:if="${user.age} ge 20"> age >= 20</p>
<p th:if="${user.age} le 20"> age <= 20</p>
<h2>选择框</h2>
<select name="" id="">
    <option th:selected="${user.age} == 20">20</option>
    <option th:selected="${user.age} == 18">18</option>
</select>
<h2>循环</h2>
<p th:each="person:${userList}">
    <span th:text="${person.name}"></span>
</p>
<h2>分支</h2>
<p th:switch="${user.name}">
    <span th:case="#{roles.superadmin}">超级管理员</span>
    <span th:case="#{roles.manager}">管理员</span>
    <span th:case="*">普通会员</span>
</p>
</body>
</html>
相关文章
|
4天前
|
JavaScript 安全 Java
智慧产科一体化管理平台源码,基于Java,Vue,ElementUI技术开发,二开快捷
智慧产科一体化管理平台覆盖从备孕到产后42天的全流程管理,构建科室协同、医患沟通及智能设备互联平台。通过移动端扫码建卡、自助报道、智能采集数据等手段优化就诊流程,提升孕妇就诊体验,并实现高危孕产妇五色管理和孕妇学校三位一体化管理,全面提升妇幼健康宣教质量。
33 12
|
26天前
|
JavaScript Java 测试技术
基于Java+SpringBoot+Vue实现的车辆充电桩系统设计与实现(系统源码+文档+部署讲解等)
面向大学生毕业选题、开题、任务书、程序设计开发、论文辅导提供一站式服务。主要服务:程序设计开发、代码修改、成品部署、支持定制、论文辅导,助力毕设!
57 6
|
27天前
|
前端开发 Java 程序员
菜鸟之路day02-04拼图小游戏开发一一JAVA基础综合项目
本项目基于黑马程序员教程,涵盖面向对象进阶、继承、多态等知识,历时约24小时完成。项目去除了登录和注册模块,专注于单机游戏体验。使用Git进行版本管理,代码托管于Gitee。项目包含窗体搭建、事件监听、图片加载与打乱、交互逻辑实现、菜单功能及美化界面等内容。通过此项目,巩固了Java基础并提升了实际开发能力。 仓库地址:[https://gitee.com/zhang-tenglan/puzzlegame.git](https://gitee.com/zhang-tenglan/puzzlegame.git)
42 6
|
30天前
|
Java 应用服务中间件 API
【潜意识Java】javaee中的SpringBoot在Java 开发中的应用与详细分析
本文介绍了 Spring Boot 的核心概念和使用场景,并通过一个实战项目演示了如何构建一个简单的 RESTful API。
41 5
|
30天前
|
前端开发 Java 数据库连接
【潜意识Java】深度解读JavaWeb开发在Java学习中的重要性
深度解读JavaWeb开发在Java学习中的重要性
30 4
|
4月前
|
JavaScript 安全 Java
如何使用 Spring Boot 和 Ant Design Pro Vue 实现动态路由和菜单功能,快速搭建前后端分离的应用框架
本文介绍了如何使用 Spring Boot 和 Ant Design Pro Vue 实现动态路由和菜单功能,快速搭建前后端分离的应用框架。首先,确保开发环境已安装必要的工具,然后创建并配置 Spring Boot 项目,包括添加依赖和配置 Spring Security。接着,创建后端 API 和前端项目,配置动态路由和菜单。最后,运行项目并分享实践心得,包括版本兼容性、安全性、性能调优等方面。
258 1
|
26天前
|
JavaScript Java 测试技术
基于SpringBoot+Vue实现的留守儿童爱心网站设计与实现(计算机毕设项目实战+源码+文档)
博主是一位全网粉丝超过100万的CSDN特邀作者、博客专家,专注于Java、Python、PHP等技术领域。提供SpringBoot、Vue、HTML、Uniapp、PHP、Python、NodeJS、爬虫、数据可视化等技术服务,涵盖免费选题、功能设计、开题报告、论文辅导、答辩PPT等。系统采用SpringBoot后端框架和Vue前端框架,确保高效开发与良好用户体验。所有代码由博主亲自开发,并提供全程录音录屏讲解服务,保障学习效果。欢迎点赞、收藏、关注、评论,获取更多精品案例源码。
61 10
|
26天前
|
JavaScript Java 测试技术
基于SpringBoot+Vue实现的家政服务管理平台设计与实现(计算机毕设项目实战+源码+文档)
面向大学生毕业选题、开题、任务书、程序设计开发、论文辅导提供一站式服务。主要服务:程序设计开发、代码修改、成品部署、支持定制、论文辅导,助力毕设!
46 8
|
26天前
|
JavaScript 搜索推荐 Java
基于SpringBoot+Vue实现的家乡特色推荐系统设计与实现(源码+文档+部署)
面向大学生毕业选题、开题、任务书、程序设计开发、论文辅导提供一站式服务。主要服务:程序设计开发、代码修改、成品部署、支持定制、论文辅导,助力毕设!
57 8
|
26天前
|
JavaScript NoSQL Java
基于SpringBoot+Vue实现的大学生就业服务平台设计与实现(系统源码+文档+数据库+部署等)
面向大学生毕业选题、开题、任务书、程序设计开发、论文辅导提供一站式服务。主要服务:程序设计开发、代码修改、成品部署、支持定制、论文辅导,助力毕设!
67 6