当前位置: 首页 > news >正文

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

问题分析:

在这里插入图片描述

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

在这里插入图片描述

http://www.dtcms.com/a/418629.html

相关文章:

  • 算法练习题
  • 9.数组介绍和静态初始化
  • 无重复字符的最长子串_优选算法(C++)滑动窗口
  • 提升雾化片性能,关键是精密测量盲孔尺寸
  • Flannel工作原理-Flannel故障案例-镜像拉取策略-secret对接harbor及ServiceAccount实战
  • GitLab高危漏洞可致实例崩溃(CVE-2025-10858 和 CVE-2025-8014)
  • 中铁建设投资集团有限公司网站自己公司怎样做免费的网站
  • 安卓13_ROM修改定制化-----修改rom 实现支持原生安装器 破除厂商定制限制
  • android 字符串工具类(兼容 Android 16+ / API 16,无报错版)
  • 9.28 深度学习10
  • 数据安全合规行业实战解析:金融、医疗与智能网联汽车的破局之道
  • 汽车全景天窗生产线解决方案 - SNK施努卡
  • 汽车地带AutoZone EDI需求分析及对接指南
  • 如何给自己网站做反链家在深圳罗湖
  • 云手机在电商行业中的优势都有哪些
  • 微信小程序入门学习教程,从入门到精通,微信小程序页面制作(2)
  • 漳州本地网站宝安网站开发
  • Pytest框架速成
  • C++设计模式之结构型模式:代理模式(Proxy)
  • 八股已死、场景当立(分布式ID篇)
  • C++指针笔试题1
  • 中英双语 网站 模板网站建设项目验收单
  • 【centos生产环境搭建(二)redis安装】
  • 四川移动网站建设网架报价明细表
  • 网站设计思路WordPress客户端
  • 做试玩网站网站快速排名技术
  • Day30_【NLP 自然语言处理(0)—入门】
  • springboot 配置 HikariDataSource 连接池信息
  • identity mapping kernel image mapping
  • Docker操作命令