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
请求样例:
{
"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
}
响应样例:
{
code":1,
"msg""success",
"data":null
}
SQL
语句:
update emp
setusername = '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
语句
<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
集合:
user:name: zhangsanage: 18password: 10086
数组/List
/Set
集合:
hobby:-java-game-sport
③ 优化application.properties
文件为yml
格式
application.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
spring:#数据库连接信息datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/tliasusername: rootpassword: 1234#文件上传配置servlet:multipart:max-file-size: 10MBmax-request-size: 100MB#mybatis配置
mybatis:configuration:log-impl: org.apache.ibatis.logging.stdout.StdOutImplmap-underscore-to-camel-case: true#阿里云OSS
aliyun:oss:endpoint: https://oss-cn-hangzhou.aliyuncs.comaccessKeyId: LTAI5tNddW7JFUeFaPW6DNwHaccessKeySecret: 9BhAvB8X64d8axPyfeRVPm4j809qoHbucketName: hmleadnews1988
05 注解@ConfigurationProperties
问题分析:
答:借助注解@ConfigurationProperties
将yml
配置文件中的信息直接注入到AliOSSUtils.java
文件的实体类的同名成员变量当中。