JavaWeb 课堂笔记 —— 20 SpringBootWeb案例 配置文件

本文是JavaWeb学习笔记,基于黑马程序员教程,详细介绍了查询回显和修改员工的实现流程。通过GET/PUT请求实现员工信息查询与修改,包含SQL语句、MyBatis配置及前后端联调测试。

同时讲解了参数配置化方法,比较了properties与yml配置文件的差异,并演示如何将properties转为更简洁的yml格式。

最后引入@ConfigurationProperties注解解决多属性注入问题,提升开发效率。

教程涵盖SpringBoot项目配置、MyBatis日志输出等实用技巧,适合JavaWeb初学者参考。

01 查询回显

① 需求:根据主键id查询员工信息

请求路径:/emps/{id}

请求方式:GET

请求样例:

复制代码
/emps/1

② 思路

③ 牛马开发

Postman测试

⑤ 前后端联调测试

02 修改员工

① 需求:根据主键id修改员工数据

请求路径:/emps

请求方式:PUT

请求样例:

json 复制代码
{
"id":1,
"image":"https://web-framework.oss-cn-hangzhou.aliyuncs.com/2022-09-03-07-37-38222.jpg",
"username":"linpingzhi",
"name":"林平之",
"gender":1,
"job":1,
"entrydate":"2022-g9-18",
"deptId":1
}

响应样例:

json 复制代码
{
code":1,
"msg""success",
"data":null
}

SQL语句:

sql 复制代码
update emp
set
	username = 'Tom1',
	password = '123456',
	name = '汤姆1',
	gender = 1,
	image = '1.jpg',
	job = 1,
	entrydate = '2005-01-01',
	dept_id = 1,
	update_time = '2025-06-11 12:10:00'
where id = 1;

② 思路

③ 牛马开发

xml语句

xml 复制代码
<update id="update">
update emp
<set>
<if test="username != null and username != ''"> username = #{username}, </if>
<if test="password != null and password != ''"> password = #{password}, </if>
<if test="name != null and name != ''"> name = #{name}, </if>
<if test="gender != null"> gender = #{gender}, </if>
<if test="image != null and image != ''"> image = #{image}, </if>
<if test="job != null"> job = #{job}, </if>
<if test="entrydate != null"> entrydate = #{entrydate}, </if>
<if test="deptId != null"> dept_id = #{deptId}, </if>
<if test="updateTime != null"> update_time = #{updateTime} </if>
</set>
where id = #{id}
</update>

postman测试

⑤ 前后端联调测试

03 参数配置化

问题分析:

答:将其配置在Application.properties文件当中,因为这个文件是全项目唯一的文件。

注:@Value注解通常用于外部配置的属性注入,具体做法为:@Value("${配置文件中的key}")

04 yml配置文件

SpringBoot提供了多种属性配置方式,包括properties、yml、yaml,常见配置方式格式对比如下。


yml基本语法

yml数据格式

对象/Map集合:

yaml 复制代码
user:
  name: zhangsan
  age: 18
  password: 10086

数组/List/Set集合:

yaml 复制代码
hobby:
 -java
 -game
 -sport

③ 优化application.properties文件为yml格式

application.properties

properties 复制代码
#驱动类名称
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#数据库连接的url
spring.datasource.url=jdbc:mysql://localhost:3306/tlias
#连接数据库的用户名
spring.datasource.username=root
#连接数据库的密码
spring.datasource.password=1234

#配置mybatis的日志, 指定输出到控制台
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
#开启mybatis的驼峰命名自动映射开关 a_column ------> aCloumn
mybatis.configuration.map-underscore-to-camel-case=true

#配置单个文件上传大小限制
spring.servlet.multipart.max-file-size=10MB

#配置单个请求上传大小限制(一次请求,多个文件)
spring.servlet.multipart.max-request-size=100MB

#阿里云OSS配置
aliyun.oss.endpoint=https://oss-cn-hangzhou.aliyuncs.com
aliyun.oss.accessKeyId=LTAI5tNddW7JFUeFaPW6DNwH
aliyun.oss.accessKeySecret=9BhAvB8X64d8axPyfeRVPm4j809qoH
aliyun.oss.bucketName=hmleadnews1988

application.yml

yaml 复制代码
spring:
  #数据库连接信息
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/tlias
    username: root
    password: 1234

  #文件上传配置
  servlet:
    multipart:
      max-file-size: 10MB
      max-request-size: 100MB

#mybatis配置
mybatis:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    map-underscore-to-camel-case: true

#阿里云OSS
aliyun:
  oss:
    endpoint: https://oss-cn-hangzhou.aliyuncs.com
    accessKeyId: LTAI5tNddW7JFUeFaPW6DNwH
    accessKeySecret: 9BhAvB8X64d8axPyfeRVPm4j809qoH
    bucketName: hmleadnews1988

05 注解@ConfigurationProperties

问题分析:

答:借助注解@ConfigurationPropertiesyml配置文件中的信息直接注入到AliOSSUtils.java文件的实体类的同名成员变量当中。

相关推荐
spencer_tseng几秒前
org.eclipse.wst.common.project.facet.core.xml could not be read.
xml·java·eclipse
Lisonseekpan几秒前
为什么Spring 推荐使用构造器注入而非@Autowired字段注入?
java·后端·spring·log4j
Lovely Ruby1 分钟前
Cursor 迁移到 Zed 编辑器
java·缓存·编辑器
草莓熊Lotso2 分钟前
Python 流程控制完全指南:条件语句 + 循环语句 + 实战案例(零基础入门)
android·开发语言·人工智能·经验分享·笔记·后端·python
laozhoy12 分钟前
深入理解Golang中的锁机制
开发语言·后端·golang
Gu_yyqx4 分钟前
IDEA中debug的使用
java·ide·intellij-idea
zore_c5 分钟前
【数据结构】队列——超详解!!!(包含队列的实现)
c语言·网络·数据结构·c++·笔记·算法·链表
雾岛听蓝6 分钟前
C++ 模板初阶
开发语言·c++
小杰帅气6 分钟前
智能指针喵喵喵
开发语言·c++·算法
老华带你飞7 分钟前
个人网盘管理|基于springboot + vue个人网盘管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端