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文件的实体类的同名成员变量当中。

相关推荐
许商2 小时前
【stm32】bash自动配置buildenv
开发语言·bash
reembarkation2 小时前
自定义分页控件,只显示当前页码的前后N页
开发语言·前端·javascript
楼田莉子2 小时前
vscode搭建C/C++配置开发环境
c语言·开发语言·c++·vscode·学习·编辑器
Roye_ack3 小时前
【项目实战 Day9】springboot + vue 苍穹外卖系统(用户端订单模块 + 商家端订单管理模块 完结)
java·vue.js·spring boot·后端·mybatis
人间有清欢3 小时前
java数据权限过滤
java·mybatis·权限控制·数据过滤
A阳俊yi3 小时前
Spring——声明式事务
java·数据库·spring
我要精通C++3 小时前
lua虚拟机的垃圾回收机制
java·开发语言
22jimmy3 小时前
MyBatis动态sql
java·开发语言·mybatis
Diligence8153 小时前
最优化方法
算法