【微服务】后台管理项目多数据源管理方案实战

目录

前言

1、使用Spring提供的AbstractRoutingDataSource

2、使用MyBatis注册多个SqlSessionFactory

3、使用dynamic-datasource框架


前言

        Java后台使用MyBatis-plus 快速访问多个数 据源,这里分享三种常用的多数据源管理方案

1、使用Spring提供的AbstractRoutingDataSource

这种方式的核心是使用 Spring 提供的 AbstractRoutingDataSource 抽象类,注入多个数据源。核心代码如下:
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
import org.springframework.stereotype.Component;import javax.sql.DataSource;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;@Component
@Primary   // 将该Bean设置为主要注入Bean
public class DynamicDataSource extends AbstractRoutingDataSource {// 当前使用的数据源标识public static ThreadLocal<String> name=new ThreadLocal<>();// 写@AutowiredDataSource dataSource1;// 读@AutowiredDataSource dataSource2;// 返回当前数据源标识@Overrideprotected Object determineCurrentLookupKey() {return name.get();}@Overridepublic void afterPropertiesSet() {// 为targetDataSources初始化所有数据源Map<Object, Object> targetDataSources=new HashMap<>();targetDataSources.put("W",dataSource1);targetDataSources.put("R",dataSource2);super.setTargetDataSources(targetDataSources);// 为defaultTargetDataSource 设置默认的数据源super.setDefaultTargetDataSource(dataSource1);super.afterPropertiesSet();}
}
将自己实现的 DynamicDataSource 注册成为默认的 DataSource 实例后,只需要在每次使用 DataSource 时,提前

改变一下其中的name标识,就可以快速切换数据源。这里使用到AOP功能,关键代码如下:

注解类,用于标识读库或者写库

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;/**** @Author 黎剑* @Slogan 无意与众不同,只因品位出众,致敬未来的你*/
@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface WR {String value() default "W";
}

切面类

import com.lijian.dynamic.datasource.DynamicDataSource;
import com.lijian.dynamic.datasource.annotation.WR;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;/**** @author 黎剑* @slogan 无意与众不同,只因品位出众,致敬未来的你*/
@Component
@Aspect
public class DynamicDataSourceAspect implements Ordered {// 前置@Before("within(com.lijian.dynamic.datasource.service.impl.*) && @annotation(wr)")public void before(JoinPoint point, WR wr){String name = wr.value();DynamicDataSource.name.set(name);System.out.println(name);}@Overridepublic int getOrder() {return 0;}//TODO 环绕通知
}

数据库配置类


import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.lijian.dynamic.datasource.DynamicDataSource;
import com.lijian.dynamic.datasource.plugin.DynamicDataSourcePlugin;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;import javax.sql.DataSource;/**** @author 黎剑* @slogan 无意与众不同,只因品位出众,致敬未来的你*/
@Configuration
public class DataSourceConfig {@Bean@ConfigurationProperties(prefix = "spring.datasource.datasource1")public DataSource dataSource1() {// 底层会自动拿到spring.datasource中的配置, 创建一个DruidDataSourcereturn DruidDataSourceBuilder.create().build();}@Bean@ConfigurationProperties(prefix = "spring.datasource.datasource2")public DataSource dataSource2() {// 底层会自动拿到spring.datasource中的配置, 创建一个DruidDataSourcereturn DruidDataSourceBuilder.create().build();}@Beanpublic DataSourceTransactionManager transactionManager1(DynamicDataSource dataSource){DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();dataSourceTransactionManager.setDataSource(dataSource);return dataSourceTransactionManager;}@Beanpublic DataSourceTransactionManager transactionManager2(DynamicDataSource dataSource){DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();dataSourceTransactionManager.setDataSource(dataSource);return dataSourceTransactionManager;}
}

application.yml

spring:datasource:type: com.alibaba.druid.pool.DruidDataSourcedatasource1:url: jdbc:mysql://127.0.0.1:3306/datasource1?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF8&useSSL=falseusername: rootpassword: 123456initial-size: 1min-idle: 1max-active: 20test-on-borrow: truedriver-class-name: com.mysql.cj.jdbc.Driverdatasource2:url: jdbc:mysql://127.0.0.1:3306/datasource2?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF8&useSSL=falseusername: rootpassword: 123456initial-size: 1min-idle: 1max-active: 20test-on-borrow: truedriver-class-name: com.mysql.cj.jdbc.Driver

服务层实现类

import com.lijian.dynamic.datasource.annotation.WR;
import com.lijian.dynamic.datasource.mapper.FrendMapper;
import com.lijian.dynamic.datasource.entity.Frend;
import com.lijian.dynamic.datasource.service.FrendService;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;import java.util.List;@Service
public class FrendImplService implements FrendService {@AutowiredFrendMapper frendMapper;@Override@WR("R")        // 库2public List<Frend> list() {return frendMapper.list();}@Override@WR("W")        // 库1public void save(Frend frend) {frendMapper.save(frend);}
}

启动类

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.transaction.annotation.EnableTransactionManagement;@SpringBootApplication
@MapperScan("com.lijian.dynamic.datasource.mapper")
@EnableAspectJAutoProxy(exposeProxy=true) // 启动AOP
public class DynamicDatasourceApplication {public static void main(String[] args) {SpringApplication.run(DynamicDatasourceApplication.class, args);}}

2、使用MyBatis注册多个SqlSessionFactory

如果使用MyBatis框架,要注册多个数据源的话,就需要将MyBatis底层的DataSource、SqlSessionFactory、 DataSourceTransactionManager这些核心对象一并进行手动注册。例如:
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.support.TransactionTemplate;import javax.sql.DataSource;@Configuration
// 继承mybatis:
// 1. 指定扫描的mapper接口包(主库)
// 2. 指定使用sqlSessionFactory是哪个(主库)
@MapperScan(basePackages = "com.lijian.datasource.dynamic.mybatis.mapper.r",sqlSessionFactoryRef="rSqlSessionFactory")
public class RMyBatisConfig {@Bean@ConfigurationProperties(prefix = "spring.datasource.datasource2")public DataSource dataSource2() {// 底层会自动拿到spring.datasource中的配置, 创建一个DruidDataSourcereturn DruidDataSourceBuilder.create().build();}@Bean@Primarypublic SqlSessionFactory rSqlSessionFactory()throws Exception {final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();// 指定主库sessionFactory.setDataSource(dataSource2());// 指定主库对应的mapper.xml文件/*sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/r/*.xml"));*/return sessionFactory.getObject();}@Beanpublic DataSourceTransactionManager rTransactionManager(){DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();dataSourceTransactionManager.setDataSource(dataSource2());return dataSourceTransactionManager;}@Beanpublic TransactionTemplate rTransactionTemplate(){return new TransactionTemplate(rTransactionManager());}
}
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.support.TransactionOperations;
import org.springframework.transaction.support.TransactionTemplate;import javax.sql.DataSource;/***** 写数据源配置*/
@Configuration
// 继承mybatis:
// 1. 指定扫描的mapper接口包(主库)
// 2. 指定使用sqlSessionFactory是哪个(主库)
@MapperScan(basePackages = "com.lijian.datasource.dynamic.mybatis.mapper.w",sqlSessionFactoryRef="wSqlSessionFactory")
public class WMyBatisConfig {@Bean@ConfigurationProperties(prefix = "spring.datasource.datasource1")public DataSource dataSource1() {// 底层会自动拿到spring.datasource中的配置, 创建一个DruidDataSourcereturn DruidDataSourceBuilder.create().build();}@Bean@Primarypublic SqlSessionFactory wSqlSessionFactory()throws Exception {final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();// 指定主库sessionFactory.setDataSource(dataSource1());// 指定主库对应的mapper.xml文件/*sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/order/*.xml"));*/return sessionFactory.getObject();}@Bean@Primarypublic DataSourceTransactionManager wTransactionManager(){DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();dataSourceTransactionManager.setDataSource(dataSource1());return dataSourceTransactionManager;}@Beanpublic TransactionTemplate wTransactionTemplate(){return new TransactionTemplate(wTransactionManager());}
}

 注解类

import java.lang.annotation.*;@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MultiTransactional {String[] value() default {};
}

 AOP

import com.lijian.datasource.dynamic.mybatis.service.transaction.ComboTransaction;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;@Aspect
@Component
public class MultiTransactionAop {private final ComboTransaction comboTransaction;@Autowiredpublic MultiTransactionAop(ComboTransaction comboTransaction) {this.comboTransaction = comboTransaction;}@Pointcut("within(com.lijian.datasource.dynamic.mybatis.service.impl.*)")public void pointCut() {}@Around("pointCut() && @annotation(multiTransactional)")public Object inMultiTransactions(ProceedingJoinPoint pjp, MultiTransactional multiTransactional) {return comboTransaction.inCombinedTx(() -> {try {return pjp.proceed();       //执行目标方法} catch (Throwable throwable) {if (throwable instanceof RuntimeException) {throw (RuntimeException) throwable;}throw new RuntimeException(throwable);}}, multiTransactional.value());}
}
import com.alibaba.druid.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;import java.util.concurrent.Callable;
import java.util.stream.Stream;@Component
public class ComboTransaction {@Autowiredprivate Db1TxBroker db1TxBroker;@Autowiredprivate Db2TxBroker db2TxBroker;public <V> V inCombinedTx(Callable<V> callable, String[] transactions) {if (callable == null) {return null;}// 相当于循环 [wTransactionManager,wTransactionManager]Callable<V> combined = Stream.of(transactions).filter(ele -> !StringUtils.isEmpty(ele)).distinct().reduce(callable, (r, tx) -> {switch (tx) {case DbTxConstants.DB1_TX:return () -> db1TxBroker.inTransaction(r);case DbTxConstants.DB2_TX:return () -> db2TxBroker.inTransaction(r);default:return null;}}, (r1, r2) -> r2);try {return combined.call();} catch (RuntimeException e) {throw e;} catch (Exception e) {throw new RuntimeException(e);}}
}
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;import java.util.concurrent.Callable;@Component
public class Db1TxBroker {@Transactional(DbTxConstants.DB1_TX)public <V> V inTransaction(Callable<V> callable) {try {return callable.call();} catch (RuntimeException e) {throw e;} catch (Exception e) {throw new RuntimeException(e);}}
}
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;import java.util.concurrent.Callable;@Component
public class Db2TxBroker {@Transactional(DbTxConstants.DB2_TX)public <V> V inTransaction(Callable<V> callable) {try {return callable.call();} catch (RuntimeException e) {throw e;} catch (Exception e) {throw new RuntimeException(e);}}
}
public class DbTxConstants {public static final String DB1_TX = "wTransactionManager";public static final String DB2_TX = "rTransactionManager";
}

服务层类

import com.lijian.datasource.dynamic.mybatis.entity.Frend;
import com.lijian.datasource.dynamic.mybatis.mapper.r.RFrendMapper;
import com.lijian.datasource.dynamic.mybatis.mapper.w.WFrendMapper;
import com.lijian.datasource.dynamic.mybatis.service.FrendService;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionTemplate;import java.util.List;@Service
public class FrendImplService implements FrendService {@Autowiredprivate RFrendMapper rFrendMapper;@Autowiredprivate WFrendMapper wFrendMapper;@AutowiredTransactionTemplate wTransactionTemplate;@AutowiredTransactionTemplate rTransactionTemplate;// 读-- 读库@Overridepublic List<Frend> list() {return rFrendMapper.list();}// 保存-- 写库@Overridepublic void save(Frend frend) {wFrendMapper.save(frend);}// 保存-- 写库@Overridepublic void saveW(Frend frend) {frend.setName("无羡W");wFrendMapper.save(frend);}// 保存-- 读库@Overridepublic void saveR(Frend frend) {frend.setName("无羡R");rFrendMapper.save(frend);}@Transactional(transactionManager = "wTransactionManager")public void saveAll(Frend frend) throws Exception {FrendService frendService = (FrendService) AopContext.currentProxy();frendService.saveAllR(frend);}@Transactional(transactionManager = "rTransactionManager")public void saveAllR(Frend frend) {saveW(frend);saveR(frend);int a = 1 / 0;}
}
这样就完成了读库的注册。而读库与写库之间,就可以通过指定不同的 Mapper XML 文件的地址来进行区分。

3、使用dynamic-datasource框架

        dynamic-datasource是 MyBaits-plus 作者设计的一个多数据源开源方案。使用这个框架需要引入对应的 pom 依赖
        <dependency><groupId>com.baomidou</groupId><artifactId>dynamic-datasource-spring-boot-starter</artifactId><version>3.5.0</version></dependency>
这样就可以在 SpringBoot 的配置文件中直接配置多个数据源,application.yaml
spring:datasource:dynamic:#设置默认的数据源或者数据源组,默认值即为masterprimary: master#严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使用默认数据源strict: falsedatasource:master:url: jdbc:mysql://127.0.0.1:3306/datasource1?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF8&useSSL=falseusername: rootpassword: 123456initial-size: 1min-idle: 1max-active: 20test-on-borrow: truedriver-class-name: com.mysql.cj.jdbc.Driverslave_1:url: jdbc:mysql://127.0.0.1:3306/datasource2?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF8&useSSL=falseusername: rootpassword: 123456initial-size: 1min-idle: 1max-active: 20test-on-borrow: truedriver-class-name: com.mysql.cj.jdbc.Driver
这样就配置完成了 master slave_1 两个数据库。
接下来在使用时,只要在对应的方法或者类上添加 @DS注解即可。服务层实现类, 例如
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.lijian.dynamic.datasource.mapper.FrendMapper;
import com.lijian.dynamic.datasource.entity.Frend;
import com.lijian.dynamic.datasource.service.FrendService;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;import java.util.List;@Service
public class FrendImplService implements FrendService {@AutowiredFrendMapper frendMapper;@Override@DS("slave")  // 从库, 如果按照下划线命名方式配置多个  , 可以指定前缀即可(组名)public List<Frend> list() {return frendMapper.list();}@Override@DS("#session.userID")@DSTransactional #事务控制public void save(Frend frend) {frendMapper.save(frend);}@DS("master")@DSTransactionalpublic void saveAll(){// 执行多数据源的操作}
}

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

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

相关文章

【C++深度探索】继承机制详解(一)

hello hello~ &#xff0c;这里是大耳朵土土垚~&#x1f496;&#x1f496; &#xff0c;欢迎大家点赞&#x1f973;&#x1f973;关注&#x1f4a5;&#x1f4a5;收藏&#x1f339;&#x1f339;&#x1f339; &#x1f4a5;个人主页&#xff1a;大耳朵土土垚的博客 &#x1…

代码托管服务:GitHub、GitLab、Gitee

目录 引言GitHub&#xff1a;全球最大的代码托管平台概述功能特点适用场景 GitLab&#xff1a;一体化的开发平台概述功能特点适用场景 Gitee&#xff08;码云&#xff09;&#xff1a;中国本土化的代码托管服务概述功能特点适用场景 功能对比结论 引言 在现代软件开发中&#…

numpy - array(3)

arr1 np.array([[(1000, 1001, 1002, 1003), (1010, 1011, 1012, 1013), (1020, 1021, 1022, 1023)],[(1100, 1101, 1102, 1103), (1110, 1111, 1112, 1113), (1120, 1121, 1122, 1123)]], dtypeint) (1) 根据坐标访问元素或内容,更改访问的内容&#xff0c;array也会更改。“…

C++操作系列(一):MinGW环境安装与配置(无报错版)

本文选择MinGW作为安装对象。 1. 下载MinGW 进入官网&#xff1a;MinGW - Minimalist GNU for Windows download | SourceForge.net 点击File&#xff1a; 划到最下面&#xff1a; &#xfeff; Windows 64位系统下载seh结尾的安装包&#xff1a; 2. 安装MinGW 解压MinGW&am…

力扣第218题“天际线问题”

在本篇文章中&#xff0c;我们将详细解读力扣第218题“天际线问题”。通过学习本篇文章&#xff0c;读者将掌握如何使用扫描线算法和堆来解决这一问题&#xff0c;并了解相关的复杂度分析和模拟面试问答。每种方法都将配以详细的解释&#xff0c;以便于理解。 问题描述 力扣第…

【CSS】深入理解CSS 的steps()函数

在CSS动画中&#xff0c;steps()函数是一个时间函数&#xff0c;它主要用于animation-timing-function属性&#xff0c;以定义动画的步进方式。当你想要动画的某个属性&#xff08;如background-position或transform: translateX()&#xff09;在特定的步骤之间变化时&#xff…

探索 ES6:现代 JavaScript 的新特性

随着 JavaScript 的不断演进&#xff0c;ECMAScript 2015&#xff08;简称 ES6&#xff09;作为 JavaScript 的一次重大更新&#xff0c;带来了许多新特性和语法改进&#xff0c;极大地提升了开发体验和代码质量。本文将详细介绍 ES6 的主要新特性&#xff0c;并展示如何在实际…

NLTK:原理与使用详解

文章目录 NLTK简介NLTK的核心功能1. 文本处理2. 词汇处理3. 语法分析4. 语义分析5. 情感分析 NLTK的使用1. 安装NLTK2. 导入NLTK库3. 下载NLTK数据集4. 文本处理示例5. 情感分析示例 总结 NLTK简介 NLTK是一个开源的Python库&#xff0c;用于处理和分析人类语言数据。它提供了…

扛鼎中国AI搜索,天工凭什么?

人类的创作不会没有瓶颈&#xff0c;但AI的热度可不会消停。 大模型之战依旧精彩&#xff0c;OpenAI选择在Google前一天举行发布会&#xff0c;两家AI企业之间的拉扯赚足了热度。 反观国内&#xff0c;百模大战激发了大家对于科技变革的热切期盼&#xff0c;而如今行业已逐渐…

【操作系统期末速成】 EP01 | 学习笔记(基于五道口一只鸭)

文章目录 一、前言&#x1f680;&#x1f680;&#x1f680;二、正文&#xff1a;☀️☀️☀️1.1 考点一&#xff1a;操作系统的概率及特征 三、总结&#xff1a;&#x1f353;&#x1f353;&#x1f353; 一、前言&#x1f680;&#x1f680;&#x1f680; ☀️ 回报不在行动…

文章浮现之单细胞VDJ的柱状图

应各位老师的需求复现一篇文章的中的某个图 具体复现图5的整个思路图&#xff0c;这里没有原始数据&#xff0c;所以我使用虚拟生产的metadata进行画图 不废话直接上代码&#xff0c;先上python的代码的结果图 import matplotlib.pyplot as plt import numpy as np# 数据&#…

架构师篇-8、运用事件风暴进行业务领域建

如何成为优秀架构师&#xff1f; 需要有一定的技术积累&#xff0c;但是核心是懂业务。 具备一定的方法&#xff0c;并且有很强的业务理解能力。 技术架构师&#xff1a;形成技术方案&#xff0c;做的更多的是底层的平台&#xff0c;提供工具。 业务架构师&#xff1a;解决方…

两数之和你会,三数之和你也会吗?o_O

前言 多少人梦想开始的地方&#xff0c;两数之和。 但是今天要聊的不是入门第一题&#xff0c;也没有面试官会考这一题吧…不会真有吧&#xff1f; 咳咳不管有没有&#xff0c;今天的猪脚是它的兄弟&#xff0c;三数之和&#xff0c;作为双指针经典题目之一&#xff0c;也是常…

Vue3中Element Plus组件库el-eialog弹框中的input无法获取表单焦点的解决办法

以下是vue.js官网给出的示例 <script setup> import { ref, onMounted } from vue// 声明一个 ref 来存放该元素的引用 // 必须和模板里的 ref 同名 const input ref(null)onMounted(() > {input.value.focus() }) </script><template><input ref&qu…

如何在Vue3项目中使用Pinia进行状态管理

**第一步&#xff1a;安装Pinia依赖** 要在Vue3项目中使用Pinia进行状态管理&#xff0c;首先需要安装Pinia依赖。可以使用以下npm命令进行安装&#xff1a; bash npm install pinia 或者如果你使用的是yarn&#xff0c;可以使用以下命令&#xff1a; bash yarn add pinia *…

Tomcat的安装和虚拟主机和context配置

一、 安装Tomcat 注意&#xff1a;安装 tomcat 前必须先部署JDK 1. 安装JDK 方法1&#xff1a;Oracle JDK 的二进制文件安装 [rootnode5 ~]# mkdir /data [rootnode5 ~]# cd /data/ [rootnode5 data]# rz[rootnode5 data]# ls jdk-8u291-linux-x64.tar.gz [rootnode5 data]…

C++:std::function的libc++实现

std::function是个有点神奇的模板&#xff0c;无论是普通函数、函数对象、lambda表达式还是std::bind的返回值&#xff08;以上统称为可调用对象&#xff08;Callable&#xff09;&#xff09;&#xff0c;无论可调用对象的实际类型是什么&#xff0c;无论是有状态的还是无状态…

【C++】string基本用法(常用接口介绍)

文章目录 一、string介绍二、string类对象的创建&#xff08;常见构造&#xff09;三、string类对象的容量操作1.size()和length()2.capacity()3.empty()4.clear()5.reserve()6.resize() 四、string类对象的遍历与访问1.operator[ ]2.正向迭代器begin()和end()3.反向迭代器rbeg…

QTableView与QSqlQueryModel的简单使用

测试&#xff1a; 这里有一个sqlite数据库 存储了10万多条数据&#xff0c;col1是1,col2是2. 使用QSqlQueryModel和QTableView来显示这些数据&#xff0c;也非常非常流畅。 QString aFile QString::fromLocal8Bit("E:/桌面/3.db");if (aFile.isEmpty())return;//打…

关于考摩托车驾照

刚通过了摩托车驾照考试&#xff0c;说两句。 1、在哪儿考试就要搞清楚当地的规定&#xff0c;不要以为全国要求都一样。 2、首先是报驾校。虽然至少有些地方允许自学后&#xff08;不报驾校&#xff09;考试&#xff0c;但报驾校听听教练说的&#xff0c;还是能提高通过率&a…