配置文件yml
连接数据库信息

和passwprd: XXXX
应用名称
和后期微服相关
yaml
spring:
application:
name:xxx
arduino
servlet:
multipart:
max-file-size: 10MB
max-request-size: 100MB
这段配置是 Spring Boot 中用于文件上传的设置,主要用来限制上传文件的大小和请求的总体大小
mybatis配置

- 作用 日志
- 此配置会让 MyBatis 将所有执行的 SQL 语句 、参数 和返回结果输出到控制台(标准输出),方便开发调试。
arduino
map-underscore-to-camel-case: true
作用
当设置为 true
时,MyBatis 会自动将数据库中的下划线命名 (snake_case)转换为 Java 对象的驼峰命名 (camelCase),无需手动通过 @Result
注解或 <resultMap>
逐个字段映射。
makefile
logging:
level:
org.springframework.jdbc.support.JdbcTransactionManager: debug
日志
核心作用1. 打印事务管理详细信息
diff
- 会输出事务的开启、提交、回滚等关键节点日志
- 帮助开发者确认事务是否按预期工作
阿里云的相关配置
写代码
在引导类可以加上@MapperSan后面加上包名
作用与功能
-
自动发现 Mapper 接口
无需手动逐个注册 Mapper 接口,Spring 启动时会自动扫描指定包路径下的所有 Mapper 接口。
-
生成代理实现类
为扫描到的 Mapper 接口自动创建动态代理对象,并注入到 Spring 容器中。
-
简化配置
替代 XML 配置中的
<mybatis:scan>
或手动声明@Mapper
的方式。映射的三种方式 1手动映射举例
less
@Results({
@Result(column = "create_time", property = "createTime"),
@Result(column = "update_time", property = "updateTime")
})
2 在sql语句中取别名
csharp
select id ,name ,create_time createTime,update_time updateTime from dept
3 自动映射配置文件mybatis中的驼峰映射
arduino
map-underscore-to-camel-case: true
nginx
Nginx 是一个高性能的 开源 Web 服务器 和 反向代理服务器
反向代理: (通过自己的配置文件conf文件里变化网址转发) 是一种网络架构技术,通过反向代理服务器向为后端服务器做代理(安全,灵活,负载均衡)
nignx在命令框的命令
启动start nginx
重启 nginx -s reload
关闭 nginx -s stop

带请求参数写法 1 .value或者name:表示指定请求参数的名称
2 .required() 表示参数是否为必须,默认值true.
3 .defaultValue() 如果required属性为false时,可以通过该属性为参数指定默认值
有请求体要用 @RequestBody

获取参数路径用 @PathVariable
例子

日志
1.logback日志,springboot,logback的依赖已经集成了,不需要引入,只需要配置文件(可以AI生成).
文件类型

应用

2.lombak也集成了日志
用@slf4j注解
编写代码名字要与api文档一样 分页查询 在mapper层如果有多个参数,要加注解@param取名字
可以用分页插件引入依赖
xml
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.4.7</version>
</dependency>
在业务层使用 PageHelper.startPage(page,pagesize)方法来分页(注意这个方法要在查询方法的上面并且中间不能有其他代码) (原理是拦截SQL,在在查询前先统计数量,然后在为查新SQL拼接limit)
请求参数过多的情况下,可以封装为实体类作为参数
条件可以用动态SQL
例子
bash
<where>
<if test="name!=null and name !=''">
e.name like concat('%',#{name},'%')
</if>
<if test="gender!=null">
and e.gender=#{gender}
</if>
<if test="begin!=null and end!=null">
and e.entry_date between #{begin} and #{end}
</if>
</where>
新增多张表(1对多)
查看api接口文档分析建立实体类
获取数据库id在xml中加上

取到id值 把值赋值给多表的相关属性

判断是否为空
可以入工具依赖

传入的是集合数据的话 最好加上@param取名字
遍历结合
lua
<insert id="insert" >
insert into emp_expr(begin, end, company, job, emp_id) values
<foreach collection="list1 " item="expr" separator=",">
(#{expr.begin},#{expr.end},#{expr.company},#{expr.job},#{expr.empId})
</foreach>
foreach标签的表头内容,含义
collection表示集合名字,
item表示元素,
seperator表示以啥分割
还有open是以什么为开头遍历,close以什么为结束遍历
事务管理
事务是一个完整的不可分割的操作单位,操作要么全部成功,要么全部失败.
在SQL中用
begin开启事务 ,commit提交事务 ,rollback回滚事务
在springboot中用 ,注解@Transactional
一般用在方法上.
可以用在类和接口上(但不建议用)
注解@Transactional的属性
rollback可以指定遇到什么异常类型会回滚事务, 一般加 =Exception.class 默认是RuntimeException propagation:事务传播行为(事务A方法调用了事务B方法,B方法如何进行事务控制)
REQUIRED:有事务,内部方法加入到外部方法的事务(增删改)
REQUIRES_NEW:新开事务,内部方法使用自己的事务
SUPPORTS:如果外部有方法有事务,内部方法加入,如果外部方法没事务,内部方法也无事务(查询)
- 事务特性
- A原子性:事务是一个不可分割的单元,里面的操作要么全部成功,要么全部失败
- C一致性:事务开始前到事务结束后,数据总量保持不变
- I隔离性:事务之间相互隔离,互不影响
- D持久性:事务一旦提交,数据永久的持久化
因为添加员工是两个表所有要加上事务
上传文件
可以用阿里云来储存
引入阿里的sdk依赖 可以改官方代码
改造上传用到的代码
ini
String dir = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy/MM"));
//生成一个新的不重复的文件名
String newFileName = UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
String objectName = dir + "/" + newFileName;
perl
ossClient.putObject(bucketName, objectName, new ByteArrayInputStream(content));
return endpoint.split("//")[0] + "//" + bucketName + "." + endpoint.split("//")[1] + "/" + objectName;
删除文件用到的
ini
String urlPrefix = endpoint.split("//")[0] + "//" + bucketName + "." + endpoint.split("//")[1] + "/";
keys = keys.stream()
.map(item -> item.replaceAll(urlPrefix, ""))
.toList();
获取工具类
文件类型一般是 multipart 类型
提示 file.getOriginalFilename()`,这通常用于获取上传文件的原始文件名 判断文件非空
优化配置 方法一
方法二
前缀注解@ConfiguiationProperties(prefix=xxxxxx)在配置文件里
删除
批量删除
参数可以为数组或集合
业务层判断非空 多表连表不要忘记加事务(@Transactinal)
动态SQL例子
perl
<delete id="deleteByIds">
delete from emp
where id in
<foreach collection="ids" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</delete>
修改
数据回显
根据id查询
当查询的表是1对多并且需要都要回显较复杂的话需要手动映射
选resultMap类型
实现代码案例
ini
<resultMap id="EmpAndExprListRusultMap" type="com.itheima.boottilas.domian.pojo.Emp">
<id column="id" property="id" />
<result column="username" property= "username" />
<result column="password" property= "password" />
<result column="name" property= "name" />
<result column="gender" property= "gender" />
<result column="phone" property= "phone" />
<result column="job" property= "job" />
<result column="salary" property= "salary" />
<result column="image" property= "image" />
<result column="entry_date" property= "entryDate" />
<result column="dept_id" property= "deptId" />
<result column="ecreate_time" property= "createTime" />
<result column="update_time" property= "updateTime" />
<collection property="exprList" ofType="com.itheima.boottilas.domian.pojo.EmpExpr">
<id column="ee_id" property="id"/>
<result column="ee_begin" property="begin"/>
<result column="ee_end" property="end"/>
<result column="ee_company" property="company"/>
<result column="ee_job" property="job"/>
</collection>
</resultMap>
<select id="findById" resultMap="EmpAndExprListRusultMap">
select e.*, ee.id ee_id
,ee.begin ee_begin
,ee.end ee_end
,ee.company ee_company
,ee.job ee_job
from emp e left join emp_expr ee
on e.id=ee.emp_id
where e.id=#{id}
</select>
collection :映射集合 property:java实体集合的属性名 ofType:集合的泛型
修改用
思路可以先删除多表再覆盖 业务层判断集合非空
SQL动态用标签用 set标签
ini
<update id="updateById">
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="phone!=null and phone!=''">
phone=#{phone},
</if>
<if test="job!=null ">
job=#{job},
</if>
<if test=" salary!=null">
salary=#{salary},
</if>
<if test="image!=null and image!=''">
image=#{image},
</if>
<if test="entryDate!=null">
entry_date=#{entryDate},
</if>
<if test="deptId!=null">
dept_id=#{deptId},
</if>
<if test="createTime!=null">
create_time=#{createTime},
</if>
<if test="updateTime!=null">
update_time=#{updateTime}
</if>
</set>
where id=#{id}
</update>
业务层判断名字是否重复

配置全局异常RestControllerAdvice加在一个新建的 类里面 注解@ExceptionHandler
例子
typescript
@ExceptionHandler
public Result customException(CustomException customException){
log.info("customException:{}",customException);
return Result.error(customException.getMessage());
}
统计
case的用法
vbnet
<select id="getEmpJobDate" resultType="com.itheima.boottilas.entry.JobInfo">
select (case job
when 1 then '班主任'
when 2 then '讲师'
when 3 then '学工主管'
when 4 then '教研主管'
when 5 then '咨询师'
else '其他'
end) as pos,
count(*) as num
from emp group by job order by num
</select>
重点名字要和前端相应数据一样才会显示


2个值可以用if
