MyBatis的第四天学习笔记下

news2025/4/17 21:45:02

10.MyBatis参数处理

10.1 项目信息

  • 模块名:mybatis-007-param
  • 数据库表:t_student
  • 表结构:
    • id: 主键
    • name: 姓名
    • age: 年龄
    • height: 身高
    • sex: 性别
    • birth: 出生日期
  • sql文件:
create table t_student  
(  
    id     bigint auto_increment  
        primary key,  
    name   varchar(255) null,  
    age    int          null,  
    height double       null,  
    birth  date         null,  
    sex    char         null  
);

INSERT INTO `t_student` VALUES (1, '张三', 20, 1.81, '1980-10-11', '男');  
INSERT INTO `t_student` VALUES (2, '李四', 18, 1.61, '1988-10-11', '女');  
INSERT INTO `t_student` VALUES (3, '赵六', 20, 1.81, '2022-09-01', '男');  
INSERT INTO `t_student` VALUES (4, '赵六', 20, 1.81, '2022-09-01', '男');  
INSERT INTO `t_student` VALUES (5, '张飞', 50, 10, '2022-09-01', '女');  
INSERT INTO `t_student` VALUES (6, '张飞', 50, 10, '2022-09-01', '女');

10.2 POJO类

package com.example.mybatis.pojo;  

import java.time.LocalDate;  

public class Student {  
    private Long id;  
    private String name;  
    private Integer age;  
    private double height;  
    private Character sex;  
    private LocalDate birth;
    // constructor
    // setter and getter
    // toString
}

10.3 参数处理方式

10.3.1 单个简单类型参数

适用场景:查询条件只有一个简单类型参数时使用

特点

  • MyBatis可以自动推断参数类型,无需显式指定
  • #{…}中的内容可以随意写
  • 完整写法(可省略):
<select id="selectByName" resultType="student" parameterType="java.lang.String">
    select * from t_student where name = #{name, javaType=String, jdbcType=VARCHAR}
</select>

示例

// Mapper接口
public interface StudentMapper {
    List<Student> selectByName(String name);
    Student selectById(Long id);
    List<Student> selectByBirth(LocalDate birth);
    List<Student> selectBySex(Character sex);
}

// XML配置
<select id="selectByName" resultType="Student">
    select * from t_student where name = #{name}
</select>

// 测试代码
@Test
public void testSelectByName() {
    List<Student> students = mapper.selectByName("张三");
    students.forEach(System.out::println);
}
10.3.2 Map参数

适用场景:需要传递多个参数但没有对应POJO类时

特点

  • 手动封装Map集合,将每个条件以key-value形式存放
  • 通过#{map集合的key}来取值

示例

// Mapper接口
List<Student> selectByParamMap(Map<String,Object> paramMap);

// 测试代码
@Test
public void testSelectByParamMap(){
    Map<String,Object> paramMap = new HashMap<>();
    paramMap.put("nameKey", "张三");
    paramMap.put("ageKey", 20);
    
    List<Student> students = mapper.selectByParamMap(paramMap);
    students.forEach(System.out::println);
}

// XML配置
<select id="selectByParamMap" resultType="student">
    select * from t_student where name = #{nameKey} and age = #{ageKey}
</select>
10.3.3 实体类参数

适用场景:参数与实体类属性匹配时

特点

  • #{…}中写的是属性名
  • 属性名本质上是set/get方法名去掉set/get之后的名字

示例

// Mapper接口
int insert(Student student);

// 测试代码
@Test
public void testInsert(){
    Student student = new Student();
    student.setName("李四");
    student.setAge(30);
    student.setHeight(1.70);
    student.setSex('男');
    student.setBirth(LocalDate.now());
    
    int count = mapper.insert(student);
    System.out.println("插入了" + count + "条记录");
}

// XML配置
<insert id="insert">
    insert into t_student values(null,#{name},#{age},#{height},#{birth},#{sex})
</insert>
10.3.4 多参数

适用场景:方法有多个参数但没有使用@Param注解时

特点

  • MyBatis底层会创建map集合存储参数
  • 参数命名规则:
    • arg0/param1:第一个参数
    • arg1/param2:第二个参数
    • 以此类推…

示例

// Mapper接口
List<Student> selectByNameAndSex(String name, Character sex);

// 测试代码
@Test
public void testSelectByNameAndSex(){
    List<Student> students = mapper.selectByNameAndSex("张三", '男');
    students.forEach(System.out::println);
}

// XML配置
<select id="selectByNameAndSex" resultType="student">
    select * from t_student where name = #{arg0} and sex = #{arg1}
</select>
10.3.5 @Param注解(命名参数)

适用场景:方法有多个参数且需要明确参数名时

特点

  • 增强代码可读性
  • @Param中的值就是Map集合的key
  • 可以自定义参数名称

示例

// Mapper接口
List<Student> selectByNameAndAge(@Param("name") String name, @Param("age") int age);

// 测试代码
@Test
public void testSelectByNameAndAge(){
    List<Student> students = mapper.selectByNameAndAge("张三", 20);
    students.forEach(System.out::println);
}

// XML配置
<select id="selectByNameAndAge" resultType="student">
    select * from t_student where name = #{name} and age = #{age}
</select>

10.4 总结对比

参数类型适用场景优点缺点
单个简单类型单一条件查询简单直接只能处理一个参数
Map参数多条件查询且无对应POJO灵活,可自定义key需要手动封装Map
实体类参数参数与实体属性匹配自动映射属性需要创建实体类
多参数方法有多个参数无需额外封装参数名不直观(arg0/param1)
@Param注解需要明确参数名可读性好需要添加注解

10.5 常见问题

  1. #{…}和${…}的区别

    • #{…}:预编译处理,防止SQL注入
    • ${…}:字符串替换,需要手动处理引号
  2. 参数类型自动推断

    • MyBatis可以自动推断大多数简单类型的参数
    • 复杂类型建议显式指定javaType和jdbcType
  3. 参数命名冲突

    • 避免在Map参数和@Param注解中使用相同的key
    • 建议使用有意义的参数名

11. MyBatis查询语句专题

模块名:mybatis-008-select

打包方式:jar

引入依赖:mysql驱动依赖、mybatis依赖、logback依赖、junit依赖。

引入配置文件:jdbc.properties、mybatis-config.xml、logback.xml

创建pojo类:Car

创建Mapper接口:CarMapper

创建Mapper接口对应的映射文件:com/powernode/mybatis/mapper/CarMapper.xml

创建单元测试:CarMapperTest

拷贝工具类:SqlSessionUtil

11.1 返回Car

当查询的结果有对应的实体类,并且查询结果只有一条时,可以直接返回Car对象。

使用场景
  • 根据主键查询单条记录
  • 查询结果保证只有一条记录的情况
  • 查询结果有对应的实体类映射
  • 需要获取完整对象信息的场景
  • 需要直接操作对象属性的场景
实际应用
  1. 用户详情页根据用户ID查询用户信息
  2. 商品详情页根据商品ID查询商品信息
  3. 订单详情页根据订单ID查询订单信息
示例代码
// CarMapper接口
public interface CarMapper {
    /**
     * 根据id主键查询:结果最多只有一条
     * @param id 主键id
     * @return Car对象
     * @throws TooManyResultsException 当查询结果多于一条时抛出
     */
    Car selectById(Long id);
}
// Mapper XML
<!-- 
  使用resultType="Car"指定返回类型为Car实体类
  列名通过as或resultMap转换为Java属性名
-->
<select id="selectById" resultType="Car">
    select 
        id,
        car_num carNum,  -- 数据库列名car_num映射到Java属性carNum
        brand,
        guide_price guidePrice,
        produce_time produceTime,
        car_type carType 
    from t_car 
    where id = #{id}  -- 使用#{}防止SQL注入
</select>
// 测试代码
@Test
public void testSelectById(){
    // 获取SqlSession和Mapper接口实例
    SqlSession sqlSession = SqlSessionUtil.openSession();
    try {
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        
        // 执行查询并处理结果
        Car car = mapper.selectById(166L);
        
        // 验证结果
        Assert.assertNotNull("查询结果不应为null", car);
        System.out.println(car);
    } finally {
        // 确保关闭SqlSession
        sqlSession.close();
    }
}
性能优化建议
  1. 对于频繁查询的字段,考虑添加数据库索引
  2. 大数据量查询建议使用分页
  3. 考虑使用二级缓存提高查询性能
常见错误及解决方案
  1. 错误:查询结果为空返回null
    • 解决方案:使用Optional包装返回值或添加空值检查
  2. 错误:查询结果多于一条抛出TooManyResultsException
    • 解决方案:确保查询条件唯一或使用List接收结果
  3. 错误:列名与属性名不匹配导致映射失败
    • 解决方案:使用resultMap或开启驼峰命名映射
执行结果示例
Car{id=166, carNum='京A154345', brand='宝马', guidePrice=20.0, produceTime='2024-01-01', carType='豪华电车'}
注意事项
  1. 查询结果是一条记录时,也可以使用List集合接收
  2. 如果查询结果为空,返回null
  3. 如果查询结果多于一条,会抛出TooManyResultsException异常
  4. 建议在查询条件中确保结果唯一性

11.2 返回List

当查询的记录条数是多条时,必须使用集合接收。如果使用单个实体类接收会出现异常。

使用场景
  • 查询多条记录
  • 分页查询
  • 条件查询可能返回多条记录
  • 需要批量处理数据的场景
  • 需要遍历处理每条记录的场景
实际应用
  1. 商品列表页查询所有商品
  2. 用户管理页查询所有用户
  3. 订单列表页查询符合条件的订单
性能考虑
  • 大数据量查询建议使用分页
  • 考虑使用延迟加载优化性能
示例代码
// CarMapper接口
/**
 * 查询所有的Car
 * @return Car对象列表
 */
List<Car> selectAll();
// Mapper XML
<select id="selectAll" resultType="Car">
    select 
        id,
        car_num carNum,
        brand,
        guide_price guidePrice,
        produce_time produceTime,
        car_type carType 
    from t_car
</select>
// 测试代码
@Test
public void testSelectAll(){
    CarMapper mapper = SqlSessionUtil.openSession().getMapper(CarMapper.class);
    List<Car> cars = mapper.selectAll();
    cars.forEach(car -> System.out.println(car));
}
执行结果示例
[
    Car{id=33, carNum='103', brand='奔驰E300L', guidePrice=50.30, produceTime=2020-10-01, carType='燃油车'}, 
    Car{id=34, carNum='102', brand='比亚迪汉', guidePrice=30.23, produceTime=2018-09-10, carType='电车'},
    ...
]
注意事项
  1. 如果返回多条记录,采用单个实体类接收会抛出TooManyResultsException异常
  2. 查询结果为空时返回空集合,而不是null
  3. 建议在查询条件中明确限制返回条数,避免查询过多数据

11.3 返回Map

当返回的数据没有合适的实体类对应时,可以采用Map集合接收。字段名做key,字段值做value。

使用场景
  • 查询结果没有对应的实体类
  • 只需要部分字段
  • 动态查询结果
示例代码
// CarMapper接口
/**
 * 通过id查询一条记录,返回Map集合
 * @param id 主键id
 * @return Map<String, Object> 字段名和值的映射
 */
Map<String, Object> selectByIdRetMap(Long id);
// Mapper XML
<select id="selectByIdRetMap" resultType="map">
    select 
        id,
        car_num carNum,
        brand,
        guide_price guidePrice,
        produce_time produceTime,
        car_type carType 
    from t_car 
    where id = #{id}
</select>
// 测试代码
@Test
public void testSelectByIdRetMap(){
    CarMapper mapper = SqlSessionUtil.openSession().getMapper(CarMapper.class);
    Map<String,Object> car = mapper.selectByIdRetMap(167L);
    System.out.println(car);
}
执行结果示例
{
	car_num=1203, 
	id=167, 
	guide_price=20.00, 
	produce_time=2010-12-03, 
	brand=奔驰GLC, 
	car_type=电车
}
注意事项
  1. 如果返回多条记录,采用单个Map接收会抛出TooManyResultsException异常
  2. Map的key是数据库列名或别名
  3. Map的value类型会自动转换,但要注意类型转换可能带来的问题

11.4 返回List

查询结果条数大于等于1条数据时,可以返回一个存储Map集合的List集合。

使用场景
  • 查询多条记录且没有对应的实体类
  • 动态查询结果
  • 需要灵活处理查询结果
示例代码
// CarMapper接口
/**
 * 查询所有的Car,返回一个List集合。List集合中存储的是Map集合。
 * @return List<Map<String,Object>> 结果集
 */
List<Map<String,Object>> selectAllRetListMap();
// Mapper XML
<select id="selectAllRetListMap" resultType="map">
    select 
        id,
        car_num carNum,
        brand,
        guide_price guidePrice,
        produce_time produceTime,
        car_type carType 
    from t_car
</select>
// 测试代码
@Test
public void testSelectAllRetListMap(){
    CarMapper mapper = SqlSessionUtil.openSession().getMapper(CarMapper.class);
    List<Map<String,Object>> cars = mapper.selectAllRetListMap();
    System.out.println(cars);
}
执行结果示例
[
    {
        carType=燃油车, 
        carNum=103, 
        guidePrice=50.30, 
        produceTime=2020-10-01, 
        id=33, 
        brand=奔驰E300L
    }, 
    {
        carType=电车, 
        carNum=102, 
        guidePrice=30.23, 
        produceTime=2018-09-10, 
        id=34, 
        brand=比亚迪汉
    },
    ...
]
注意事项
  1. 查询结果为空时返回空集合
  2. 每个Map代表一条记录
  3. 适合处理动态查询结果

11.5 返回Map<String,Map>

使用Car的id作为key,方便后续取出对应的Map集合。

使用场景
  • 需要根据主键快速查找记录
  • 批量查询后需要按主键组织数据
  • 需要建立主键到记录的映射关系
示例代码
// CarMapper接口
/**
 * 获取所有的Car,返回一个Map集合。
 * Map集合的key是Car的id。
 * Map集合的value是对应Car。
 * @return Map<Long,Map<String,Object>> 结果集
 */
@MapKey("id")
Map<Long,Map<String,Object>> selectAllRetMap();
// Mapper XML
<select id="selectAllRetMap" resultType="map">
    select 
        id,
        car_num carNum,
        brand,
        guide_price guidePrice,
        produce_time produceTime,
        car_type carType 
    from t_car
</select>
// 测试代码
@Test
public void testSelectAllRetMap(){
    CarMapper mapper = SqlSessionUtil.openSession().getMapper(CarMapper.class);
    Map<Long,Map<String,Object>> cars = mapper.selectAllRetMap();
    System.out.println(cars);
}
执行结果示例
{
    64={
        carType=燃油车, 
        carNum=133, 
        guidePrice=50.30, 
        produceTime=2020-01-10, 
        id=64, 
        brand=丰田霸道
    }, 
    66={
        carType=燃油车, 
        carNum=133, 
        guidePrice=50.30, 
        produceTime=2020-01-10, 
        id=66, 
        brand=丰田霸道
    },
    ...
}
注意事项
  1. 必须使用@MapKey注解指定作为key的字段
  2. 查询结果为空时返回空Map
  3. 适合需要根据主键快速查找的场景

11.6 resultMap结果映射

当查询结果的列名和Java对象的属性名不对应时,有三种解决方案:

  1. 使用as给列起别名
  2. 使用resultMap进行结果映射
  3. 开启驼峰命名自动映射
使用resultMap进行结果映射
使用场景
  • 复杂的对象关系映射
  • 需要自定义类型转换
  • 需要处理复杂的嵌套对象
示例代码
// CarMapper接口
/**
 * 查询所有Car,使用resultMap进行结果映射
 * @return List<Car> 结果集
 */
List<Car> selectAllByResultMap();
// Mapper XML
<!--
    resultMap:
        id:这个结果映射的标识,作为select标签的resultMap属性的值。
        type:结果集要映射的类。可以使用别名。
-->
<resultMap id="carResultMap" type="car">
    <!--对象的唯一标识,官方解释是:为了提高mybatis的性能。建议写上。-->
    <id property="id" column="id"/>
    <result property="carNum" column="car_num"/>
    <!--当属性名和数据库列名一致时,可以省略。但建议都写上。-->
    <!--javaType用来指定属性类型。jdbcType用来指定列类型。一般可以省略。-->
    <result property="brand" column="brand" javaType="string" jdbcType="VARCHAR"/>
    <result property="guidePrice" column="guide_price"/>
    <result property="produceTime" column="produce_time"/>
    <result property="carType" column="car_type"/>
</resultMap>

<select id="selectAllByResultMap" resultMap="carResultMap">
    select * 
  	from t_car
</select>
// 测试代码
@Test
public void testSelectAllByResultMap(){
    CarMapper carMapper = SqlSessionUtil.openSession().getMapper(CarMapper.class);
    List<Car> cars = carMapper.selectAllByResultMap();
    System.out.println(cars);
}
开启驼峰命名自动映射
使用场景
  • 属性名遵循Java命名规范
    Java命名规范:首字母小写,后面每个单词首字母大写,遵循驼峰命名方式。
  • 数据库列名遵循SQL命名规范
    SQL命名规范:全部小写,单词之间采用下划线分割。
  • 需要简化映射配置
配置方式

在mybatis-config.xml中配置:

<settings>
    <setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
命名规范对应关系
实体类中的属性名数据库表的列名
carNumcar_num
carTypecar_type
produceTimeproduce_time
示例代码
// CarMapper接口
/**
 * 查询所有Car,启用驼峰命名自动映射
 * @return List<Car> 结果集
 */
List<Car> selectAllByMapUnderscoreToCamelCase();
// Mapper XML
<select id="selectAllByMapUnderscoreToCamelCase" resultType="Car">
    select * 
  	from t_car
</select>
// 测试代码
@Test
public void testSelectAllByMapUnderscoreToCamelCase(){
    CarMapper carMapper = SqlSessionUtil.openSession().getMapper(CarMapper.class);
    List<Car> cars = carMapper.selectAllByMapUnderscoreToCamelCase();
    System.out.println(cars);
}

11.7 返回总记录条数

使用场景
  • 分页查询时获取总记录数
  • 统计查询
  • 数据报表
示例代码
// CarMapper接口
/**
 * 获取总记录条数
 * @return 总记录数
 */
Long selectTotal();
// Mapper XML
<!--long是别名,可参考mybatis开发手册。-->
<select id="selectTotal" resultType="long">
    select count(*) 
  	from t_car
</select>
// 测试代码
@Test
public void testSelectTotal(){
    CarMapper carMapper = SqlSessionUtil.openSession().getMapper(CarMapper.class);
    Long total = carMapper.selectTotal();
    System.out.println(total);
}
执行结果示例
总记录数:14
注意事项
  1. 建议使用count(1)或count(*)而不是count(列名)
  2. 注意count的返回值类型,建议使用Long而不是Integer
  3. 对于大数据量表,count操作可能较慢,需要考虑优化

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2331015.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

基于 Spring Boot 瑞吉外卖系统开发(一)

基于 Spring Boot 瑞吉外卖系统开发&#xff08;一&#xff09; 系统概述 系统功能 技术选型 初始项目和数据准备 初始项目和SQL文件下载 创建数据库并导入数据 打开reggie项目 运行效果 主函数启动项目&#xff0c;访问URL&#xff1a; http://127.0.0.1:8080/backend/pag…

WordPress超简洁的主题:果果CMS主题

果果CMS是基于WordPress开发的超精简的一款主题&#xff0c;它在原有的特性上添加了许多新特性&#xff0c;例如&#xff1a;随机文章、随机标签、随机分类、广告、友情链接等。 新版特性&#xff1a; 小&#xff1a;主题安装包文件大小只有140.48KB。少&#xff1a;主题最小…

leetcode13.罗马数字转整数

遍历&#xff0c;下一个值不大于当前值就加上当前值&#xff0c;否则就减去当前值 class Solution {public int romanToInt(String s) {Map<Character, Integer> map Map.of(I, 1,V, 5,X, 10,L, 50,C, 100,D, 500,M, 1000);int sum 0;for (int i 0; i < s.length(…

线程安全问题的原因与解决方案总结

目录 一 什么是线程安全&#xff1f; 二 线程安全问题的实例 三 线程安全问题的原因 1.多个线程修改共享数据 2.抢占式执行 3.修改操作不是原子的 4.内存可见性问题 5.指令重排序 四 解决方案 1.同步代码块 2.同步方法 3.加锁lock解决问题 一 什么是线程安全&…

Tunable laser激光器的前向和后向锁波长方案

----转载自秦岭农民的文章 Tunable laser可调激光器的锁波长方案 激光器锁波长技术是指通过各种手段将激光器的输出波长稳定在某一特定值或范围内&#xff0c;以满足高精度应用的需求。这些技术包括Etalon、波长计/光谱仪反馈、波长参考源、温度控制、电流控制、锁相环&#…

蓝桥杯:日期统计

文章目录 问题描述解法一递归解法二&#xff1a;暴力破解 问题描述 首先我们要了解什么是子序列&#xff0c;就是一个序列之中可以忽略元素但是不能改变顺序之后获得的序列就叫做子序列。 如"123"就是"11234"的子序列而不是"11324"的子序列 解法…

IQ解调原理#通信原理系列

IQ解调原理&#xff1a;接收端收到s(t)信号后&#xff0c;分为两路&#xff1a; 一路信号乘以cosω₀t再积分&#xff0c;就可以得到a&#xff1a; 另一路乘以 -sinω₀t再积分&#xff0c;就可以得到b&#xff1a;

C++蓝桥杯实训篇(三)

片头 嗨&#xff01;小伙伴们&#xff0c;大家好~ 今天我们来学习前缀和与差分相关知识&#xff0c;准备好了吗&#xff1f;咱们开始咯&#xff01; 一、一维前缀和 以上&#xff0c;是我们用数学知识求解区间和&#xff0c;现在我们使用前缀和来求解&#xff1a; 我们知道&am…

【数据挖掘】岭回归(Ridge Regression)和线性回归(Linear Regression)对比实验

这是一个非常实用的 岭回归&#xff08;Ridge Regression&#xff09;和线性回归&#xff08;Linear Regression&#xff09;对比实验&#xff0c;使用了 scikit-learn 中的 California Housing 数据集 来预测房价。 &#x1f4e6; 第一步&#xff1a;导入必要的库 import num…

CExercise_07_1指针和数组_1编写函数交换数组中两个下标的元素

题目&#xff1a; 要求编写函数将数组作为参数传递来实现&#xff1a; 1.编写函数交换数组中两个下标的元素。函数声明如下&#xff1a;void swap(int *arr, int i, int j) 。要求不使用[]运算符&#xff0c;将[]还原成解引用运算符和指针加法来完成。 关键点 通过指针交换数组…

塔能科技:智能路灯物联运维产业发展现状与趋势分析

随着智慧城市建设的推进&#xff0c;智能路灯物联运维产业正经历快速发展&#xff0c;市场规模持续扩大。文章探讨了智能路灯物联运维的技术体系、市场机遇和挑战&#xff0c;并预测了未来发展趋势&#xff0c;为行业发展提供参考。 关键词 智能路灯&#xff1b;物联运维&#…

ZW3D二次开发_普通对话框_设置对话框弹出位置

ZW3D的普通对话框可以在UI设计时静态地设置对话框弹出的位置&#xff0c;方法如下&#xff1a; 选中对话框的最顶级对象&#xff0c;即ZsCc::Form对象&#xff0c;在属性管理器中添加一个动态属性“form_pos”&#xff0c;类型为“StringList”&#xff0c;如下图所示 不同属性…

低代码开发「JNPF」应用场景

政务系统快速搭建 在数字化政务转型的浪潮下&#xff0c;JNPF 快速开发平台扮演着关键角色&#xff0c;为政府部门提供了高效且便捷的审批流程自动化解决方案。 以 “一网通办” 为例&#xff0c;通过平台的可视化拖拽式配置功能&#xff0c;政府工作人员能够将原本复杂繁琐的…

欧拉函数模板

1.欧拉函数模板 - 蓝桥云课 问题描述 这是一道模板题。 首先给出欧拉函数的定义&#xff1a;即 Φ(n) 表示的是小于等于 n 的数中和 n 互质的数的个数。 比如说 Φ(6)2&#xff0c;当 n 是质数的时候&#xff0c;显然有 Φ(n)n−1。 题目大意&#xff1a; 给定 n 个正整数…

屏幕空间反射SSR-笔记

屏幕空间反射SSR 相关文章&#xff1a; [OpenGL] 屏幕空间反射效果 Games202-RealTime GI in Screen Space github上的例子&#xff0c;使用visual studio2019 github例子对应的文章 使用OpenGL和C实现发光柱子的SSR倒影 下面是一个使用OpenGL和C实现屏幕空间反射(SSR)来创建…

动态规划算法深度解析:0-1背包问题(含完整流程)

简介&#xff1a; 0-1背包问题是经典的组合优化问题&#xff1a;给定一组物品&#xff08;每个物品有重量和价值&#xff09;&#xff0c;在背包容量限制下选择物品装入背包&#xff0c;要求总价值最大化且每个物品不可重复选取。 动态规划核心思想 通过构建二维状态表dp[i]…

LeetCode刷题SQL笔记

系列博客目录 文章目录 系列博客目录1.distinct关键字 去除重复2.char_length()3.group by 与 count()连用4.date类型有个函数datediff()5.mod 函数6.join和left join的区别1. **JOIN&#xff08;内连接&#xff0c;INNER JOIN&#xff09;**示例&#xff1a; 2. **LEFT JOIN&a…

循环神经网络 - 参数学习之随时间反向传播算法

本文中&#xff0c;我们以同步的序列到序列模式为例来介绍循环神经网络的参数学习。 循环神经网络中存在一个递归调用的函数 &#x1d453;(⋅)&#xff0c;因此其计算参数梯度的方式和前馈神经网络不太相同。在循环神经网络中主要有两种计算梯度的方式&#xff1a;随时间反向…

球类(继承和多态)

父类Ball&#xff0c;设置为抽象类&#xff0c;调用get和set方法创建对象&#xff0c;将子类重写的功能函数抽象化。 // 抽象球类 abstract class Ball {private String name;private double radius; // 半径private double weight; // 重量private double price; // 价格// 构…

DFS和BFS的模版

dfs dfs金典例题理解就是走迷宫 P1605 迷宫 - 洛谷 dfs本质上在套一个模版&#xff1a; ///dfs #include<bits/stdc.h> using namespace std; int a[10][10]{0}; int m,n,t,ans0; int ex,ey; int v[10][10]{0}; int dx[4]{-1,0,1,0}; int dy[4]{0,1,0,-1}; void dfs(in…