diff --git a/尹顺宇学习笔记/尹顺宇11.08作业/尹顺宇11.08学习总结.docx b/尹顺宇学习笔记/尹顺宇11.08作业/尹顺宇11.08学习总结.docx new file mode 100644 index 0000000..238a3cc Binary files /dev/null and b/尹顺宇学习笔记/尹顺宇11.08作业/尹顺宇11.08学习总结.docx differ diff --git a/尹顺宇学习笔记/尹顺宇11.08作业/尹顺宇11.08学习笔记.md b/尹顺宇学习笔记/尹顺宇11.08作业/尹顺宇11.08学习笔记.md new file mode 100644 index 0000000..1ae589a --- /dev/null +++ b/尹顺宇学习笔记/尹顺宇11.08作业/尹顺宇11.08学习笔记.md @@ -0,0 +1,379 @@ +# Mybatis初步复习 + +## 1.SpringBoot整合Mybatis + +## 1.1第一步添加依赖 + +``` + + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + 3.0.5 + + +``` + + + +### 1.2进行配置 + +``` +#配置Mybatis +mybatis: + configuration: + #在映射为java对象,将表中的下划线命名自动转换成驼峰式命名 + map-underscore-to-camel-case: true + #日志前缀 可选 + log-prefix: mybatis. + #日志实现类 可选 + log-impl: org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl + #动态sql文件存储位置 + mapper-locations: classpath:/mapper/**/*.xml + +#配置日志显示sql +logging: + level: + #指定日志前缀 + mybatis: debug + + +``` + +### 1.3 在Dao层编写的mapper接口,添加@Mapper注解 + +![1762578862653](C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\1762578862653.png) + +### 1.4 编写动态sql文件(xx.xml文件) + +![1762578878514](C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\1762578878514.png) + +### 1.5 安装mybatisx插件 + +![1762578934144](C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\1762578934144.png) + +安装之后在mapper和动态SQL文件对应代码左边有一个小鸟图标,方便直接跳转 + +![1762578917583](C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\1762578917583.png) + +## 2.自动映射 + +当开启驼峰命名自动映射 + +```yaml +mybatis: + configuration: + map-underscore-to-camel-case: true +``` + +## 3.手动映射 + +当自动映射无法满足需求时,可以使用 `` 进行精确的手动映射。 + +```xml + + + + + + + + + + + +``` + +举例: + +### 3.1基础字段映射 + +```java +实体类 +public class User { + private Long userId; // 与数据库字段名不同 + private String userName; // 需要手动映射 + private String email; + private Integer status; + private LocalDateTime createTime; + private LocalDateTime updateTime; + + // 构造函数、getter、setter... +} +``` + +```sql +数据库表结构: +CREATE TABLE t_user ( + id BIGINT PRIMARY KEY, + name VARCHAR(50), + email VARCHAR(100), + status TINYINT, + created_at DATETIME, + updated_at DATETIME +); + +``` + +```xml + + + + + + + + + + + + + + + +``` + +说明: + +![1762581420222](C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\1762581420222.png) + +### 3.2一对一关联映射 + +#### 3.2.1单次查询嵌套结果映射 + +举例: + +```java +实体类 +public class User { + private Long userId; + private String userName; + private UserProfile userProfile; // 一对一关联 + // getter/setter... +} + +public class UserProfile { + private Long profileId; + private Long userId; + private String realName; + private Integer age; + private String address; + private String phone; + // getter/setter... +} +``` + +```xml +手动映射配置: + + + + + + + + + + + + + + + + + + +``` + +#### 3.2.2 多次查询嵌套查询映射 + +```xml + + + + + + + + + + + + + + +``` + + + +### 3.3一对多集合映射 + +```java +实体类: +public class User { + private Long userId; + private String userName; + private List orders; // 一对多关联 + // getter/setter... +} + +public class Order { + private Long orderId; + private Long userId; + private String orderNumber; + private BigDecimal amount; + private LocalDateTime orderTime; + // getter/setter... +} +``` + +```xml + + + + + + + + + + + + + + + + + +``` + +### 3.4复杂的多层嵌套 + +```java +public class User { + private Long userId; + private String userName; + private List orders; +} + +public class Order { + private Long orderId; + private String orderNumber; + private List orderItems; +} + +public class OrderItem { + private Long itemId; + private Long productId; + private Integer quantity; + private Product product; // 关联商品 +} + +public class Product { + private Long productId; + private String productName; + private BigDecimal price; +} +``` + +```xml + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +``` + +### 3.5.注意 + +1.可以 自动映射与手动映射结合 + +```xml + + + + + + + + + + + + + + +``` + +## + diff --git a/尹顺宇学习笔记/尹顺宇11.08作业/尹顺宇11.08学习笔记.pdf b/尹顺宇学习笔记/尹顺宇11.08作业/尹顺宇11.08学习笔记.pdf new file mode 100644 index 0000000..94a0190 Binary files /dev/null and b/尹顺宇学习笔记/尹顺宇11.08作业/尹顺宇11.08学习笔记.pdf differ diff --git a/尹顺宇学习笔记/尹顺宇11.08作业/尹顺宇11.08股票知识学习.docx b/尹顺宇学习笔记/尹顺宇11.08作业/尹顺宇11.08股票知识学习.docx new file mode 100644 index 0000000..ac68ffd Binary files /dev/null and b/尹顺宇学习笔记/尹顺宇11.08作业/尹顺宇11.08股票知识学习.docx differ