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

网站怎么做搜索功能网站开发怎么自动获取位置

网站怎么做搜索功能,网站开发怎么自动获取位置,发布文章后马上更新网站主页,品牌宣传方案怎么写目录 前言 1.引入Springboot相关的aop切面依赖 2.创建自定义注解DataSourceKey 3.创建对ThreadLocal类 4.创建aop切面 5.创建动态数据源类 6.创建多数据库连接配置类 7.关键代码讲解 8.nacos主要配置 前言 通过Spring AOP#xff08;面向切面编程#xff09;的功能来动…目录 前言 1.引入Springboot相关的aop切面依赖 2.创建自定义注解DataSourceKey 3.创建对ThreadLocal类 4.创建aop切面 5.创建动态数据源类 6.创建多数据库连接配置类 7.关键代码讲解 8.nacos主要配置 前言 通过Spring AOP面向切面编程的功能来动态地切换数据源。使用Aspect和Component注解通过切面扫描自定义注解获取数据源的key可以在不修改原有业务代码的情况下在service里面的类方法中加入DataSourceKey注解即可访问指定的数据源。 1.引入Springboot相关的aop切面依赖 dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-aop/artifactId/dependency 2.创建自定义注解DataSourceKey Target(ElementType.METHOD) Retention(RetentionPolicy.RUNTIME) public interface DataSourceKey {//默认使用auth数据库String value() default dataSourceSystem; } 3.创建对ThreadLocal类 通过线程隔离的方式实现数据源的切换。 package com.example.auth.datasource;/*** 数据库上下文切换对象针对每个线程做不同操作*/ public class DataSourceContextHolder {private static final ThreadLocalString contextHolder new ThreadLocal();public static void setDataSourceKey(String dataSourceKey) {contextHolder.set(dataSourceKey);}public static String getDataSourceKey() {return contextHolder.get();}public static void clearDataSourceKey() {contextHolder.remove();} }4.创建aop切面 通过包扫描动态切换数据源主要通过扫描注解的方式获取数据源的key值即数据源名称。 package com.example.auth.datasource;import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.DeclareAnnotation; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; /*** 多数据源切面*/ Aspect Component public class DatasourceAspect {private Logger logger LoggerFactory.getLogger(DatasourceAspect.class);Before(annotation(dataSourceKey) execution(* com.example.auth.datasource.*.*(..)))public void beforeSwitchDataSource(JoinPoint joinPoint, DataSourceKey dataSourceKey) {String key dataSourceKey.value();logger.info(key:{},key);DataSourceContextHolder.setDataSourceKey(key);}Before(annotation(dataSourceKey) execution(* com.example.auth.service.*.*(..)))public void beforeServiceSwitchDataSource(JoinPoint joinPoint, DataSourceKey dataSourceKey) {String key dataSourceKey.value();logger.info(key:{},key);DataSourceContextHolder.setDataSourceKey(key);}After(annotation(dataSourceKey) execution(* com.example.auth.service.*.*(..)))public void afterServiceSwitchDataSource(JoinPoint joinPoint, DataSourceKey dataSourceKey) {String key dataSourceKey.value();logger.info(key:{},key);DataSourceContextHolder.clearDataSourceKey();}After(annotation(dataSourceKey) execution(* com.example.auth.datasource.*.*(..)) )public void afterSwitchDataSource(JoinPoint joinPoint, DataSourceKey dataSourceKey) {logger.info(key:{},dataSourceKey.value());DataSourceContextHolder.clearDataSourceKey();}}5.创建动态数据源类 通过该类可动态改变数据源名称。 package com.example.auth.datasource;import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;public class DynamicDataSource extends AbstractRoutingDataSource {private Logger logger LoggerFactory.getLogger(DynamicDataSource.class);Overrideprotected Object determineCurrentLookupKey() {String key DataSourceContextHolder.getDataSourceKey();logger.info(数据源:{},key);return DataSourceContextHolder.getDataSourceKey();} }6.创建多数据库连接配置类 package com.example.auth.datasource;import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean; import org.apache.ibatis.session.SqlSessionFactory; import org.jasypt.encryption.StringEncryptor; import org.jasypt.encryption.pbe.PooledPBEStringEncryptor; import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig; import org.mybatis.spring.annotation.MapperScan; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.jdbc.DataSourceBuilder; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.*; import org.springframework.core.annotation.Order; import org.springframework.core.io.support.PathMatchingResourcePatternResolver;import javax.activation.DataContentHandler; import javax.annotation.PostConstruct; import javax.sql.DataSource; import java.util.HashMap; import java.util.Map;/*** 多数据源配置*/ Configuration MapperScan(basePackages com.example.auth.mapper) public class MultiDataSourceConfig {private Logger logger LoggerFactory.getLogger(MultiDataSourceConfig.class);Autowiredprivate DataSource dataSourceAuth;Autowiredprivate DataSource dataSourceConsumer;Autowiredprivate DataSource dataSourceMq;Autowiredprivate DataSource dataSourceGateway;Autowiredprivate DataSource dataSourceSystem;AutowiredQualifier(dynamicDataSource)private DataSource dynamicDataSource;Autowiredprivate StringEncryptor stringEncryptor;Bean(name dataSourceAuth)ConfigurationProperties(prefix spring.datasource.auth)public DataSource dataSourceAuth() {return DataSourceBuilder.create().build();}Bean(name dataSourceConsumer)ConfigurationProperties(prefix spring.datasource.consumer)public DataSource dataSourceConsumer() {return DataSourceBuilder.create().build();}Bean(name dataSourceMq)ConfigurationProperties(prefix spring.datasource.mq)public DataSource dataSourceMq() {return DataSourceBuilder.create().build();}Bean(name dataSourceGateway)ConfigurationProperties(prefix spring.datasource.gateway)public DataSource dataSourceGateway() {return DataSourceBuilder.create().build();}Bean(name dataSourceSystem)ConfigurationProperties(prefix spring.datasource.system)public DataSource dataSourceSystem() {return DataSourceBuilder.create().build();}Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor interceptor new MybatisPlusInterceptor();//分页插件interceptor.addInnerInterceptor(new PaginationInnerInterceptor());//注册乐观锁插件interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());return interceptor;}Beanpublic StringEncryptor stringEncryptor() {PooledPBEStringEncryptor encryptor new PooledPBEStringEncryptor();SimpleStringPBEConfig config new SimpleStringPBEConfig();config.setPassword(encryptionkey); // 加密密钥config.setAlgorithm(PBEWithHmacSHA512AndAES_256);config.setKeyObtentionIterations(1000);config.setPoolSize(1);config.setProviderName(SunJCE);config.setSaltGeneratorClassName(org.jasypt.salt.RandomSaltGenerator);config.setStringOutputType(base64);encryptor.setConfig(config);return encryptor;}PostConstructpublic void init(){/* String enStr stringEncryptor.encrypt(Root123);String deSTr stringEncryptor.decrypt(N8VBWG5nOHvy5efX3/mlPAmdBykE7iDZFl362LyeaPRXMbLT0PzEIlB/KDXrNYz6);System.out.println(enStrenStr);System.out.println(deSTrdeSTr);*/}/*** 不加* param interceptor* return* throws Exception*/Beanpublic SqlSessionFactory sqlSessionFactory (MybatisPlusInterceptor interceptor) throws Exception {MybatisSqlSessionFactoryBean ssfb new MybatisSqlSessionFactoryBean();ssfb.setDataSource(dynamicDataSource); // 使用 DynamicDataSourcessfb.setPlugins(interceptor);ssfb.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(classpath:/mapper/*Mapper.xml));return ssfb.getObject();}Beanpublic DataSource dynamicDataSource() {DynamicDataSource dynamicDataSource new DynamicDataSource();// 假设你有多个数据源需要在这里将它们添加到 targetDataSources 中MapObject, Object targetDataSources new HashMap();targetDataSources.put(dataSourceSystem, dataSourceSystem);targetDataSources.put(dataSourceAuth, dataSourceAuth);targetDataSources.put(dataSourceConsumer, dataSourceConsumer);targetDataSources.put(dataSourceMq, dataSourceMq);targetDataSources.put(dataSourceGateway,dataSourceGateway);dynamicDataSource.setTargetDataSources(targetDataSources);dynamicDataSource.setDefaultTargetDataSource(dataSourceSystem);// 设置默认数据源return dynamicDataSource;}}7.关键代码讲解 注入dynamicDataSource实体通过该实体bean动态获取数据源从而达到随意切换数据源的目的。 单个dataSource的注入如 dataSourceAuth主要是给动态数据源的切换提前准备多数据源。 8.nacos主要配置 spring:datasource:system: driver-class-name: com.mysql.cj.jdbc.Driverjdbc-url: jdbc:mysql://localhost:3306/system?serverTimezoneAsia/ShanghaiuseUnicodetruecharacterEncodingutf-8allowMultiQueriestruenullCatalogMeansCurrenttrueusername: rootpassword: ENC(N8VBWG5nOHvy5efX3/mlPAmdBykE7iDZFl362LyeaPRXMbLT0PzEIlB/KDXrNYz6)type: com.alibaba.druid.pool.DruidDataSourcedruid:initial-size: 5min-idle: 1max-active: 10max-wait: 60000validation-query: SELECT 1 FROM DUALtest-on-borrow: falsetest-on-return: falsetest-while-idle: truetime-between-eviction-runs-millis: 60000auth: driver-class-name: com.mysql.cj.jdbc.Driverjdbc-url: jdbc:mysql://localhost:3306/auth?serverTimezoneAsia/ShanghaiuseUnicodetruecharacterEncodingutf-8allowMultiQueriestruenullCatalogMeansCurrenttrueusername: rootpassword: ENC(N8VBWG5nOHvy5efX3/mlPAmdBykE7iDZFl362LyeaPRXMbLT0PzEIlB/KDXrNYz6)type: com.alibaba.druid.pool.DruidDataSourcedruid:initial-size: 5min-idle: 1max-active: 10max-wait: 60000validation-query: SELECT 1 FROM DUALtest-on-borrow: falsetest-on-return: falsetest-while-idle: truetime-between-eviction-runs-millis: 60000consumer: driver-class-name: com.mysql.cj.jdbc.Driverjdbc-url: jdbc:mysql://localhost:3306/consumer?serverTimezoneAsia/ShanghaiuseUnicodetruecharacterEncodingutf-8allowMultiQueriestruenullCatalogMeansCurrenttrueusername: rootpassword: ENC(N8VBWG5nOHvy5efX3/mlPAmdBykE7iDZFl362LyeaPRXMbLT0PzEIlB/KDXrNYz6)type: com.alibaba.druid.pool.DruidDataSourcedruid:initial-size: 5min-idle: 1max-active: 10max-wait: 60000validation-query: SELECT 1 FROM DUALtest-on-borrow: falsetest-on-return: falsetest-while-idle: truetime-between-eviction-runs-millis: 60000mq: driver-class-name: com.mysql.cj.jdbc.Driverjdbc-url: jdbc:mysql://localhost:3306/mq?serverTimezoneAsia/ShanghaiuseUnicodetruecharacterEncodingutf-8allowMultiQueriestruenullCatalogMeansCurrenttrueusername: rootpassword: ENC(N8VBWG5nOHvy5efX3/mlPAmdBykE7iDZFl362LyeaPRXMbLT0PzEIlB/KDXrNYz6)type: com.alibaba.druid.pool.DruidDataSourcedruid:initial-size: 5min-idle: 1max-active: 10max-wait: 60000validation-query: SELECT 1 FROM DUALtest-on-borrow: falsetest-on-return: falsetest-while-idle: truetime-between-eviction-runs-millis: 60000gateway: driver-class-name: com.mysql.cj.jdbc.Driverjdbc-url: jdbc:mysql://localhost:3306/gateway?serverTimezoneAsia/ShanghaiuseUnicodetruecharacterEncodingutf-8allowMultiQueriestruenullCatalogMeansCurrenttrueusername: rootpassword: ENC(N8VBWG5nOHvy5efX3/mlPAmdBykE7iDZFl362LyeaPRXMbLT0PzEIlB/KDXrNYz6)type: com.alibaba.druid.pool.DruidDataSourcedruid:initial-size: 5min-idle: 1max-active: 10max-wait: 60000validation-query: SELECT 1 FROM DUALtest-on-borrow: falsetest-on-return: falsetest-while-idle: truetime-between-eviction-runs-millis: 60000
http://www.eeditor.cn/news/119980/

相关文章:

  • 单页网站seo阜阳做网站有吗
  • 百色建设局网站wordpress主题的安装
  • 电子商务旅游网站建设策划书学it去哪里学比较好
  • 装饰公司网站设计昆山公司网站建设电话
  • 邢台医院网站建设怎么注册一个企业邮箱
  • 温州营销网站公司哪家好篮球网站建设目标
  • 手机音乐网站程序源码南京触屏网站开发
  • html 旅游网站三维动画制作
  • 如何做网站详细步骤图搭建 网站 实例
  • 太原网站制作在线长沙房地产公司有哪些
  • 河南网站建设价格大全手机网站如何开发
  • 办公门户网站模板下载网站制作里的更多怎么做
  • wordpress 小程序专业的网站优化公司排名
  • 有没有做专利导航运营的网站桂林两江四湖在哪里
  • 中山手机网站建设青岛缤纷网络科技有限公司
  • flash网站引导页wordpress获取链接地址
  • 微网站建设公司厦门建设局网站改到哪
  • 洛阳专业做网站多少钱网站建设十年经验
  • 怎们自己做网站莱芜金点子最新招工
  • 自己做网站怎么跳过备案wordpress安装包
  • 蜗牛星际做网站怎么制作网站教程电商
  • 建收费网站网页界面设计要中重点掌握
  • 龙岗品牌网站建设自由设计师是什么意思
  • 多用户商城网站建设公司wordpress基于谷歌框架
  • 零食网站建设需求分析自己可以建立网站吗
  • 网站建设数据怎样做关于自己的网站
  • 瑞安市做网站黄骅港什么时候开海
  • 获奖网站设计怎么网上接网站开发单自己做
  • 做游戏人设计网站大连做网站seo
  • 通辽市城乡建设局网站企业网站建设合同范本