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

Mybatis 多对一和一对多查询

文章目录

  • Mybatis 多对一 and 一对多查询详解
    • 数据库
    • 需求
    • Mybatis代码
    • 注意

Mybatis 多对一 and 一对多查询详解

数据库

员工表 t_emp

在这里插入图片描述

部门表 t_dept在这里插入图片描述


CREATE TABLE `t_emp` (`emp_id` int NOT NULL AUTO_INCREMENT,`emp_name` varchar(25) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,`age` int DEFAULT NULL,`gender` varchar(25) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,`dept_id` int DEFAULT NULL,PRIMARY KEY (`emp_id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;CREATE TABLE `t_dept` (`dept_id` int NOT NULL AUTO_INCREMENT,`dept_name` varchar(25) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,PRIMARY KEY (`dept_id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;INSERT INTO `t_emp` (`emp_id`, `emp_name`, `age`, `gender`, `dept_id`) VALUES (1, '张三', 20, '男', 1), (2, '李四', 22, '男', 2), (3, '王五', 23, '男', 3), (4, '赵六', 24, '男', 1);INSERT INTO `t_dept` (`dept_id`, `dept_name`) VALUES (1, 'A'), (2, 'B'), (3, 'C');

需求

多对一:员工表对应部门表
查询指定id的员工以及其对应的部门

一对多:部门表对应员工表
查询指定的部门以及其包含的员工


Mybatis代码

在这里插入图片描述

DeptMapper接口文件

package com.atguigu.mybatis.mapper;import com.ruanjian.pojo.Dept;
import com.ruanjian.pojo.Emp;
import org.apache.ibatis.annotations.Param;import java.util.List;public interface DeptMapper {// 一对多查询// 查询部门表中的员工信息// 需要在部门类中添加员工的集合  private List<Emp> emps;Dept getDeptAndEmpById(@Param("deptId") Integer deptId);// 分步查询// 1.先查出指定id的部门信息,部门信息中有对应员工的dept_idDept getDeptAndEmpByStepOne(@Param("deptId") int deptId);// 2.再根据dept_id查出相对应的员工信息List<Emp> getDeptAndEmpByStepTwo(@Param("deptId") int deptId);}

EmpMapper接口文件

package com.atguigu.mybatis.mapper;
import com.ruanjian.pojo.Dept;
import com.ruanjian.pojo.Emp;
import org.apache.ibatis.annotations.Param;public interface EmpMapper {// 多对一查询// 查询指定id员工以及该员工所对应的部门信息Emp getEmpAndDeptById(@Param("empId") Integer empId);Emp getEmpAndDeptById_association(@Param("empId") Integer empId);// 分步查询// 先从员工表中查询出指定id的员工的数据,该数据中有对应部门id// 再根据部门id从部门表中查询出对应的部门信息// 分步查询第一步Emp getEmpAndDeptByIdOne(@Param("empId") Integer empId);// 分步查询第二步Dept getEmpAndDeptByIdTwo(@Param("deptId") Integer deptId);
}

Dept类文件 部门实体类
package com.ruanjian.pojo;/*
员工对部门 是多对一,多对一是在多的那个类(员工类)中添加一个部门对象
部门对员工 是一对多,一对多是在一的那个类中(部门类)中添加一个员工集合对一 对的就是一个对象
对多 对的就是一个集合*/import java.util.List;// 部门实体类
public class Dept {private Integer deptId;private String deptName;private List<Emp> emps;  // 添加的一个员工的集合public Dept() {}public Dept(Integer deptId, String deptName) {this.deptId = deptId;this.deptName = deptName;}public Integer getDeptId() {return deptId;}public void setDeptId(Integer deptId) {this.deptId = deptId;}public String getDeptName() {return deptName;}public void setDeptName(String deptName) {this.deptName = deptName;}public List<Emp> getEmps() {return emps;}public void setEmps(List<Emp> emps) {this.emps = emps;}@Overridepublic String toString() {return "Dept{" +"deptId=" + deptId +", deptName='" + deptName + '\'' +", emps=" + emps +'}';}
}

Emp类文件
员工实体类

package com.ruanjian.pojo;/*
员工对部门 是多对一,多对一是在多的那个类(员工类)中添加一个部门对象
部门对员工 是一对多,一对多是在一的那个类中(部门类)中添加一个员工集合对一 对的就是一个对象
对多 对的就是一个集合*/// 员工实体类// 对一就是对应的一个对象
// 对多就是对应的一个集合
public class Emp {private Integer empId;private String empName;private Integer age;private String gender;private Dept dept;  // 加上一个部门的对象public Emp() {}public Emp(Integer empId, String empName, Integer age, String gender) {this.empId = empId;this.empName = empName;this.age = age;this.gender = gender;}public Integer getEmpId() {return empId;}public void setEmpId(Integer empId) {this.empId = empId;}public String getEmpName() {return empName;}public void setEmpName(String empName) {this.empName = empName;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public String getGender() {return gender;}public void setGender(String gender) {this.gender = gender;}public Dept getDept() {return dept;}public void setDept(Dept dept) {this.dept = dept;}@Overridepublic String toString() {return "Emp{" +"empId=" + empId +", empName='" + empName + '\'' +", age=" + age +", gender='" + gender + '\'' +", dept=" + dept +'}';}
}

DeptMapper

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.atguigu.mybatis.mapper.DeptMapper"><!-- 处理一对多的映射关系1. collection2. 分步查询
-->
<!--*************************** 一对多 collection ****************************************--><resultMap id="deptAndEmpById_resultMap" type="dept"><id column="dept_id" property="deptId"></id><result column="dept_name" property="deptName"></result><!--    collection : 处理一对多的映射关系(处理集合类型的属性)--><!--        ofType表示的是集合中的类型--><collection property="emps" ofType="Emp"><id column="emp_id" property="empId"></id><result column="emp_name" property="empName"></result><result column="age" property="age"></result><result column="gender" property="gender"></result></collection></resultMap><!--    Dept getDeptAndEmpById(@Param("deptId") Integer deptId);--><select id="getDeptAndEmpById" resultMap="deptAndEmpById_resultMap">select *from t_deptleft join t_emp on t_dept.dept_id = t_emp.dept_idwhere t_dept.dept_id=#{deptId}</select><!--*************************** 一对多 collection ****************************************--><!--*************************** 一对多  分步查询  ****************************************--><resultMap id="deptAndEmpResultMapByStep" type="Dept"><id column="dept_id" property="deptId"></id><result column="dept_name" property="deptName"></result><collection property="emps"select="com.atguigu.mybatis.mapper.DeptMapper.getDeptAndEmpByStepTwo"column="dept_id"></collection></resultMap><!--    Dept getDeptAndEmpByStepOne(@Param("deptId") int deptId);// 先查出指定id的部门信息,部门信息中有对应员工的dept_id --><select id="getDeptAndEmpByStepOne" resultMap="deptAndEmpResultMapByStep">select * from t_dept where dept_id = #{deptId}</select><!--    List<Emp> getDeptAndEmpByStepTwo(@Param("deptId") int deptId);// 再根据dept_id查出相对应的员工信息--><select id="getDeptAndEmpByStepTwo" resultType="Emp">select * from t_emp where dept_id = #{deptId}</select></mapper>

EmpMapper

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.atguigu.mybatis.mapper.EmpMapper"><!--处理多对一的映射关系有三种方法:第一种:级联方式处理第二种:association 处理多对一的映射关系(处理的是实体类类型的属性)第三种:分布查询--><!-- ************ 第一种:级联方式处理  *************** --><resultMap id="EmpAndDeptById_resultMap" type="Emp"><id column="emp_id" property="empId"></id><result column="emp_name" property="empName"></result><result column="age" property="age"></result><result column="gender" property="gender"></result><result column="dept_id" property="dept.deptId"></result><result column="dept_name" property="dept.deptName"></result></resultMap><!--Emp getEmpAndDeptById(@Param("empId") Integer empId);--><select id="getEmpAndDeptById" resultMap="EmpAndDeptById_resultMap">select *from t_empleft join t_dept on t_emp.dept_id = t_dept.dept_idwhere t_emp.emp_id=#{empId}</select><!-- *********************************************--><!-- *************** 第二种 association ******************************--><!--  第二种 association--><resultMap id="getEmpAndDeptById_association_resultMap" type="Emp"><id column="emp_id" property="empId"></id><result column="emp_name" property="empName"></result><result column="age" property="age"></result><result column="gender" property="gender"></result><!--association: 处理多对一的映射关系(处理实体类类型的属性)property: 设置需要处理映射关系的属性的属性名javaType: 设置要处理的属性的类型, 就是把<association>标签下设置映射关系的字段,封装给某个类--><association property="dept" javaType="Dept"><id column="dept_id" property="deptId"></id><result column="dept_name" property="deptName"></result></association></resultMap><!--    Emp getEmpAndDeptById_association(@Param("empId") Integer empId);--><select id="getEmpAndDeptById_association" resultMap="getEmpAndDeptById_association_resultMap">select *from t_empleft join t_dept on t_emp.dept_id = t_dept.dept_idwhere t_emp.emp_id=#{empId}</select><!-- *********************************************--><!-- ****************** 第三种:分布查询 ***************************--><resultMap id="getEmpAndDeptByIdOne_resultMap" type="Emp"><id column="emp_id" property="empId"></id><result column="emp_name" property="empName"></result><result column="age" property="age"></result><result column="gender" property="gender"></result><!--property: 设置需要处理映射关系的属性的属性名select: 填写分步查询的sql的唯一标识,就是设置下一步要执行的sql语句column: 将上一个sql查询出的某个字段作为分步查询的下一个sql语句sql条件,相当于函数的参数,传给下一个sql语句fetchType: 在开启了延时加载的环境中,通过该属性设置当前的分步查询是否使用延迟加载有两个值:eager(立即加载) lazy(延迟加载)--><association property="dept"fetchType="eager"select="com.atguigu.mybatis.mapper.EmpMapper.getEmpAndDeptByIdTwo"column="dept_id"></association></resultMap><!--    Emp getEmpAndDeptByIdOne(@Param("empId") Integer empId);--><select id="getEmpAndDeptByIdOne" resultMap="getEmpAndDeptByIdOne_resultMap">select * from t_emp where emp_id = #{empId}</select><!--    Dept getEmpAndDeptByIdTwo(@Param("deptId") Integer deptId);--><select id="getEmpAndDeptByIdTwo" resultType="Dept">select * from t_dept where dept_id = #{deptId}</select><!-- *********************************************--></mapper>

ResultMapTest

import com.atguigu.mybatis.mapper.DeptMapper;
import com.atguigu.mybatis.mapper.EmpMapper;
import com.ruanjian.pojo.Dept;
import com.ruanjian.pojo.Emp;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;import java.io.IOException;public class ResultMapTest {private SqlSession session;/*处理多对一的映射关系有三种方法:第一种:级联方式处理第二种:association 处理多对一的映射关系(处理的是实体类类型的属性)第三种:分步查询*/// *********************** 多对一  ****************************************@Test// 第三种:分步查询public void textGetEmpAndDeptByIdOne() {EmpMapper mapper = session.getMapper(EmpMapper.class);Emp emp = mapper.getEmpAndDeptByIdOne(1);System.out.println(emp);}@Test// 第二种:associationpublic void textGetEmpAndDeptById_association() {EmpMapper mapper = session.getMapper(EmpMapper.class);Emp emp = mapper.getEmpAndDeptById_association(2);System.out.println(emp);}@Test// 第一种:级联方式处理public void textGetEmpAndDeptById() {EmpMapper mapper = session.getMapper(EmpMapper.class);Emp emp = mapper.getEmpAndDeptById(2);System.out.println(emp);}// *********************** 一对多  ****************************************@Test// 分步查询public void textGetDeptAndEmpByStep() {DeptMapper mapper = session.getMapper(DeptMapper.class);Dept dept = mapper.getDeptAndEmpByStepOne(2);System.out.println(dept);}@Test// 一对多查询public void textGetDeptAndEmpById() {DeptMapper mapper = session.getMapper(DeptMapper.class);Dept dept = mapper.getDeptAndEmpById(1);System.out.println(dept);}// ***************************************************************// junit会在每一个@Test方法前执行@Before方法@Beforepublic void init() throws IOException {session = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml")).openSession();}// junit会在每一个@Test方法后执行@After方法@Afterpublic void clear() {session.commit();session.close();}
}

db.properties

mysql.driver=com.mysql.cj.jdbc.Driver
mysql.url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false
mysql.username=root
mysql.password=123456

log4j.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"><log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"><appender name="STDOUT" class="org.apache.log4j.ConsoleAppender"><param name="Encoding" value="UTF-8"/><layout class="org.apache.log4j.PatternLayout"><param name="ConversionPattern" value="%-5p %d{MM-dd HH:mm:ss,SSS} %m  (%F:%L) \n"/></layout></appender><logger name="java.sql"><level value="debug"/></logger><logger name="org.apache.ibatis"><level value="info"/></logger><root><level value="debug"/><appender-ref ref="STDOUT"/></root>
</log4j:configuration>

mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><!--环境配置--><!--引入外部db.properties--><properties resource="db.properties"/><settings><!--        <setting name="cacheEnabled" value="true" />--><!--  开启延时加载--><setting name="lazyLoadingEnabled" value="true"/><!-- 开启时即为true时任何方法的调用都会加载相关类的全部属性false时是按需加载,true是全部加载--><setting name="aggressiveLazyLoading" value="false"/><setting name="mapUnderscoreToCamelCase" value="true"/></settings><typeAliases><package name="com.ruanjian.pojo"/></typeAliases><!--配置mybatis的连接环境(可以配置多个环境)--><environments default="development"><!--开发环境--><environment id="development"><!--使用JDBC事务管理--><transactionManager type="JDBC"/><!--数据库连接相关配置,db.properties文件中的内容--><!--使用连接池技术--><dataSource type="POOLED"><!--数据库驱动--><property name="driver" value="${mysql.driver}"/><!--连接字符串--><property name="url" value="${mysql.url}"/><!--数据库用户名--><property name="username" value="${mysql.username}"/><!--数据库密码--><property name="password" value="${mysql.password}"/></dataSource></environment></environments><!--mapping文件路径配置--><mappers><!--        <mapper resource="mapper/DeptMapper.xml"/>--><package name="com.atguigu.mybatis.mapper"/></mappers>
</configuration>

pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.itheima</groupId><artifactId>MyBaits_2</artifactId><version>1.0-SNAPSHOT</version><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>8</source><target>8</target></configuration></plugin></plugins></build><properties><maven.compiler.sourece>11</maven.compiler.sourece><maven.compiler.target>11</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.2</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.11</version><scope>runtime</scope></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>text</scope></dependency><!-- log4j日志 --><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version></dependency></dependencies>
</project>

注意

新建包时用点
例如:com.atguigu.mybatis.mapper

resources目录下建立多层目录的时候时是用分割线
例如:com/atguigu/mybatis/mapper


mybatis-config.xml文件中

<package name="com.atguigu.mybatis.mapper"/>

<package>的使用条件:
接口文件要和xml文件同名,并且在同一个目录下

使用注解写的接口,只能有class的方式注册,例:

<mapper class="com.atguigu.mybatis.mapper.DeptMapper"></mapper>

相关文章:

Mybatis 多对一和一对多查询

文章目录 Mybatis 多对一 and 一对多查询详解数据库需求Mybatis代码注意 Mybatis 多对一 and 一对多查询详解 数据库 员工表 t_emp 部门表 t_dept CREATE TABLE t_emp (emp_id int NOT NULL AUTO_INCREMENT,emp_name varchar(25) CHARACTER SET utf8 COLLATE utf8_general_ci…...

MySQL的数据库操作、数据类型、表操作

目录 一、数据库操作 &#xff08;1&#xff09;、显示数据库 &#xff08;2&#xff09;、创建数据库 &#xff08;3&#xff09;、删除数据库 &#xff08;4&#xff09;、使用数据库 二、常用数据类型 &#xff08;1&#xff09;、数值类型 &#xff08;2&#xff0…...

音视频技术开发周刊 | 317

每周一期&#xff0c;纵览音视频技术领域的干货。 新闻投稿&#xff1a;contributelivevideostack.com。 MIT惊人再证大语言模型是世界模型&#xff01;LLM能分清真理和谎言&#xff0c;还能被人类洗脑 MIT等学者的「世界模型」第二弹来了&#xff01;这次&#xff0c;他们证明…...

【JavaSE专栏58】“Java构造函数:作用、类型、调用顺序和最佳实践“ ⚙️⏱️

解析Java构造函数&#xff1a;作用、类型、调用顺序和最佳实践" &#x1f680;&#x1f4da;&#x1f50d;&#x1f914;&#x1f4dd;&#x1f504;⚙️⏱️&#x1f4d6;&#x1f310; 摘要引言1. 什么是构造函数 &#x1f914;2. 构造函数的类型与用途 &#x1f4dd;1.…...

Ubuntu系统HUSTOJ 用 vim 修改php.ini 重启PHP服务

cd / sudo find -name php.ini 输出&#xff1a; ./etc/php/7.4/cli/php.ini ./etc/php/7.4/fpm/php.ini sudo vim /etc/php/7.4/cli/php.ini sudo vim /etc/php/7.4/fpm/php.ini 知识准备&#xff1a; vim的搜索与替换 在正常模式下键入 / &#xff0c;即可进入搜索模式…...

案例分析真题-信息安全

案例分析真题-信息安全 2009年真题 【问题1】 【问题2】 【问题3】 2010年真题 【问题1】 【问题2】 【问题3】 2011 年真题 【问题1】 【问题2】 【问题3】 骚戴理解&#xff1a;这个破题目完全考的知识储备&#xff0c;不知道的连手都动不了&#xff0c;没法分析 2013年真题…...

envi5.3处理高分二号影像数据辐射定标大气校正

目录 一、多光谱影像处理 1. 辐射定标 2.大气校正 1. 需要准备一些数据: 2.大气校正过程 3、正射校正 二、全色影像处理 1. 辐射定标 2. 正射校正 三、图像融合 1.几何配准 2.图像融合 高分二号处理流程 envi5.3的安装教程&#xff1a; ENVI5.3安装 安装完ENVI5.3后&#xff0…...

C语言 结构体

结构体的自引用: 自引用的目的&#xff1a; 结构体的自引用就是指在结构体内部&#xff0c;包含指向自身类型结构体的指针。 像链表就会用到结构体的自引用。假如我们要创建链表 链表的没个节点都是一个结构体&#xff0c;它里面存放着它的数据和下个节点的地址。 假如我们用…...

frp-内网穿透部署-ubuntu22服务器-windows server-详细教程

文章目录 1.下载frp2.配置服务器2.1.配置frps.ini文件2.2.设置服务文件2.3.设置开机自启和服务操作2.4.后台验证2.5.服务器重启 3.配置本地window3.1.frpc配置3.2.添加开机计划启动3.3.控制台启动隐藏窗口 4.centos防火墙和端口3.1.开放端口3.2.查看端口 5.关闭进程5.1.杀死进程…...

MySQL内存使用的监控开关和使用查看

参考文档&#xff1a; https://brands.cnblogs.com/tencentcloud/p/11151 https://www.cnblogs.com/grasp/p/10306697.html MySQL如何使用内存 在MySQL中&#xff0c;内存占用主要包括以下几部分&#xff0c;全局共享的内存、线程独占的内存、内存分配器占用的内存&#xff0…...

数据库管理-第113期 Oracle Exadata 04-硬件选择(20231020)

数据库管理-第113期 Oracle Exadata 04-硬件选择&#xff08;2023010290&#xff09; 本周没写文章&#xff0c;主要是因为到上海参加了Oracle CAB/PAB会议&#xff0c;这个放在后面再讲&#xff0c;本期讲一讲Exadata&#xff0c;尤其是存储节点的硬件选择及其对应的一些通用…...

带着问题去分析:Spring Bean 生命周期 | 京东物流技术团队

1: Bean在Spring容器中是如何存储和定义的 Bean在Spring中的定义是_org.springframework.beans.factory.config.BeanDefinition_接口&#xff0c;BeanDefinition里面存储的就是我们编写的Java类在Spring中的元数据&#xff0c;包括了以下主要的元数据信息&#xff1a; 1&…...

C语言修行之函数篇(一)tolower —— 转换为小写字母

文章目录 函数说明函数声明函数返回值函数实现函数实例 函数说明 对于大写字母&#xff0c;如果在当前语言环境中存在小写表示形式&#xff0c;则tolower()返回其小写等效物。否则&#xff0c;tolower()函数执行相同的任务。 函数声明 #include <ctype.h> int tolower(…...

【JavaSE专栏55】Java集合类HashTable解析

&#x1f332;Java集合类HashTable解析 &#x1f332;Java集合类HashTable解析摘要引言Hashtable是什么&#xff1f;Hashtable vs. HashMap&#xff1a;何时使用Hashtable&#xff1f;多线程环境&#xff1a;历史遗留系统&#xff1a;不需要进行特殊操作&#xff1a; Hashtable…...

Apollo上机实践:一次对自动驾驶技术的亲身体验

上机实践 概述自动驾驶通信分布式系统开发模式开发工具 自动驾驶感知传感器特性感知流程及算法部署感知模型 自动驾驶决策规划决策规划流程和算法使用 Dreamview 进行控制在环的规划调试开发规划场景和算法 福利活动 主页传送门&#xff1a;&#x1f4c0; 传送 概述 Apollo 是…...

QTcpServer简单的TCP服务器连接

1、简介 简单实现控制TCP服务器获取连接的套接字。点击断开服务器即可关闭所有连接&#xff0c;最大连接数量为5个。 声明源文件 #include "mainwindow.h"MainWindow::MainWindow(QWidget *parent): QMainWindow(parent) {//设置固定大小setFixedSize(1024,600);b…...

LeetCode热题100——双指针

双指针 1.移动零2.盛最多水的容器3.三数之和 1.移动零 给定一个数组 nums&#xff0c;编写一个函数将所有 0 移动到数组的末尾&#xff0c;同时保持非零元素的相对顺序。 // 题解&#xff1a;使用双指针&#xff0c;其中快指针指向非零元素&#xff0c;慢指针指向首个零元素下…...

Ubuntu ARMv8编译Qt源码以及QtCreator

最近需要在NVIDIA小盒子上面跑一个程序&#xff0c;一开始想着在Ubuntu x64下交叉编译一版&#xff0c;后来发现libqxcb.so 这个库在configure时就会一直报错&#xff0c;多方查找怀疑可能是由于硬件不支持在x64环境下编译AMR架构的xcb库。 所以最后在ARM下直接编译Qt源码了&am…...

虚机Centos忘记密码如何重置

1进入开机前的页面&#xff0c;选中第一个&#xff0c;按“e”键&#xff0c;进入编辑模式 2找到ro crashkernel项&#xff0c;将ro替换成 rw initsysroot/bin/sh 3 Ctrlx mount -o remount, rw / chroot /sysroot chroot /sysroot passwd root 输入两次密码 touch /.a…...

OpenGL_Learn02

1. 监听窗口&#xff0c;绑定回调函数 #include <glad/glad.h> #include <GLFW/glfw3.h> #include <iostream>void framebuffer_size_callback(GLFWwindow* window, int width, int height) {glViewport(0, 0, width, height);std::cout << "变了…...

Zustand 状态管理库:极简而强大的解决方案

Zustand 是一个轻量级、快速和可扩展的状态管理库&#xff0c;特别适合 React 应用。它以简洁的 API 和高效的性能解决了 Redux 等状态管理方案中的繁琐问题。 核心优势对比 基本使用指南 1. 创建 Store // store.js import create from zustandconst useStore create((set)…...

Admin.Net中的消息通信SignalR解释

定义集线器接口 IOnlineUserHub public interface IOnlineUserHub {/// 在线用户列表Task OnlineUserList(OnlineUserList context);/// 强制下线Task ForceOffline(object context);/// 发布站内消息Task PublicNotice(SysNotice context);/// 接收消息Task ReceiveMessage(…...

前端导出带有合并单元格的列表

// 导出async function exportExcel(fileName "共识调整.xlsx") {// 所有数据const exportData await getAllMainData();// 表头内容let fitstTitleList [];const secondTitleList [];allColumns.value.forEach(column > {if (!column.children) {fitstTitleL…...

力扣-35.搜索插入位置

题目描述 给定一个排序数组和一个目标值&#xff0c;在数组中找到目标值&#xff0c;并返回其索引。如果目标值不存在于数组中&#xff0c;返回它将会被按顺序插入的位置。 请必须使用时间复杂度为 O(log n) 的算法。 class Solution {public int searchInsert(int[] nums, …...

python报错No module named ‘tensorflow.keras‘

是由于不同版本的tensorflow下的keras所在的路径不同&#xff0c;结合所安装的tensorflow的目录结构修改from语句即可。 原语句&#xff1a; from tensorflow.keras.layers import Conv1D, MaxPooling1D, LSTM, Dense 修改后&#xff1a; from tensorflow.python.keras.lay…...

安宝特方案丨船舶智造的“AR+AI+作业标准化管理解决方案”(装配)

船舶制造装配管理现状&#xff1a;装配工作依赖人工经验&#xff0c;装配工人凭借长期实践积累的操作技巧完成零部件组装。企业通常制定了装配作业指导书&#xff0c;但在实际执行中&#xff0c;工人对指导书的理解和遵循程度参差不齐。 船舶装配过程中的挑战与需求 挑战 (1…...

在Ubuntu24上采用Wine打开SourceInsight

1. 安装wine sudo apt install wine 2. 安装32位库支持,SourceInsight是32位程序 sudo dpkg --add-architecture i386 sudo apt update sudo apt install wine32:i386 3. 验证安装 wine --version 4. 安装必要的字体和库(解决显示问题) sudo apt install fonts-wqy…...

C++:多态机制详解

目录 一. 多态的概念 1.静态多态&#xff08;编译时多态&#xff09; 二.动态多态的定义及实现 1.多态的构成条件 2.虚函数 3.虚函数的重写/覆盖 4.虚函数重写的一些其他问题 1&#xff09;.协变 2&#xff09;.析构函数的重写 5.override 和 final关键字 1&#…...

Java求职者面试指南:计算机基础与源码原理深度解析

Java求职者面试指南&#xff1a;计算机基础与源码原理深度解析 第一轮提问&#xff1a;基础概念问题 1. 请解释什么是进程和线程的区别&#xff1f; 面试官&#xff1a;进程是程序的一次执行过程&#xff0c;是系统进行资源分配和调度的基本单位&#xff1b;而线程是进程中的…...

Selenium常用函数介绍

目录 一&#xff0c;元素定位 1.1 cssSeector 1.2 xpath 二&#xff0c;操作测试对象 三&#xff0c;窗口 3.1 案例 3.2 窗口切换 3.3 窗口大小 3.4 屏幕截图 3.5 关闭窗口 四&#xff0c;弹窗 五&#xff0c;等待 六&#xff0c;导航 七&#xff0c;文件上传 …...