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

保定哪做网站好进入微信公众号首页

保定哪做网站好,进入微信公众号首页,wordpress页眉导航栏位置,企业qq手机版转载自 springboot-mybatis多数据源的两种整合方法 简介#xff1a; 随着并发量的不断增加#xff0c;显然单个数据库已经承受不了高并发带来的压力。一个项目使用多个数据库#xff08;无论是主从复制- - 读写分离还是分布式数据库结构#xff09;的重要性变得越来越明显…转载自  springboot-mybatis多数据源的两种整合方法 简介 随着并发量的不断增加显然单个数据库已经承受不了高并发带来的压力。一个项目使用多个数据库无论是主从复制- - 读写分离还是分布式数据库结构的重要性变得越来越明显。传统项目中个人对传统项目的理解就是所有的业务模块都在一个tomcat中完成多个相同的tomcat集群也可认为是传统项目整合多数据源有两种方法分包和AOP。 版本 springboot1.5.9.RELEASE  mariadb5.7 一、分包方式实现 1、在application.properties中配置两个数据库 ## test1 database spring.datasource.test1.urljdbc:mysql://localhost:3307/multipledatasource1?useUnicodetruecharacterEncodingUTF-8serverTimezoneUTCuseSSLfalse spring.datasource.test1.usernameroot spring.datasource.test1.passwordroot spring.datasource.test1.driver-class-namecom.mysql.cj.jdbc.Driver ## test2 database spring.datasource.test2.urljdbc:mysql://localhost:3307/multipledatasource2?useUnicodetruecharacterEncodingUTF-8serverTimezoneUTCuseSSLfalse spring.datasource.test2.usernameroot spring.datasource.test2.passwordroot spring.datasource.test2.driver-class-namecom.mysql.cj.jdbc.Driver 2、建立连个数据源的配置文件 springbooot中的参数可以参考上一篇博客不定期更新中https://blog.csdn.net/tuesdayma/article/details/81029539 第一个配置文件 //表示这个类为一个配置类 Configuration // 配置mybatis的接口类放的地方 MapperScan(basePackages com.mzd.multipledatasources.mapper.test01, sqlSessionFactoryRef test1SqlSessionFactory) public class DataSourceConfig1 {// 将这个对象放入Spring容器中Bean(name test1DataSource)// 表示这个数据源是默认数据源Primary// 读取application.properties中的配置参数映射成为一个对象// prefix表示参数的前缀ConfigurationProperties(prefix spring.datasource.test1)public DataSource getDateSource1() {return DataSourceBuilder.create().build();}Bean(name test1SqlSessionFactory)// 表示这个数据源是默认数据源Primary// Qualifier表示查找Spring容器中名字为test1DataSource的对象public SqlSessionFactory test1SqlSessionFactory(Qualifier(test1DataSource) DataSource datasource)throws Exception {SqlSessionFactoryBean bean new SqlSessionFactoryBean();bean.setDataSource(datasource);bean.setMapperLocations(// 设置mybatis的xml所在位置new PathMatchingResourcePatternResolver().getResources(classpath*:mapping/test01/*.xml));return bean.getObject();}Bean(test1SqlSessionTemplate)// 表示这个数据源是默认数据源Primarypublic SqlSessionTemplate test1sqlsessiontemplate(Qualifier(test1SqlSessionFactory) SqlSessionFactory sessionfactory) {return new SqlSessionTemplate(sessionfactory);} } 第二个配置文件 Configuration MapperScan(basePackages com.mzd.multipledatasources.mapper.test02, sqlSessionFactoryRef test2SqlSessionFactory) public class DataSourceConfig2 {Bean(name test2DataSource)ConfigurationProperties(prefix spring.datasource.test2)public DataSource getDateSource2() {return DataSourceBuilder.create().build();}Bean(name test2SqlSessionFactory)public SqlSessionFactory test2SqlSessionFactory(Qualifier(test2DataSource) DataSource datasource)throws Exception {SqlSessionFactoryBean bean new SqlSessionFactoryBean();bean.setDataSource(datasource);bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(classpath*:mapping/test02/*.xml));return bean.getObject();}Bean(test2SqlSessionTemplate)public SqlSessionTemplate test2sqlsessiontemplate(Qualifier(test2SqlSessionFactory) SqlSessionFactory sessionfactory) {return new SqlSessionTemplate(sessionfactory);} } 注意 1、Primary这个注解必须要加因为不加的话spring将分不清楚那个为主数据源默认数据源 2、mapper的接口、xml形式以及dao层都需要两个分开目录如图    3、bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(“XXXX”));mapper的xml形式文件位置必须要配置不然将报错no statement 这种错误也可能是mapper的xml中namespace与项目的路径不一致导致的具体看情况吧注意一下就行问题不大的 4、在service层中根据不同的业务注入不同的dao层。 5、如果是主从复制- -读写分离比如test01中负责增删改test02中负责查询。但是需要注意的是负责增删改的数据库必须是主库master 6、如果是分布式结构的话不同模块操作各自的数据库就好test01包下全是test01业务test02全是test02业务但是如果test01中掺杂着test02的编辑操作这时候将会产生事务问题即test01中的事务是没法控制test02的事务的这个问题在之后的博客中会解决。 二、AOP实现 简介 用这种方式实现多数据源的前提必须要清楚两个知识点AOP原理和AbstractRoutingDataSource抽象类。 1、AOP这个东西。。。不切当的说就是相当于拦截器只要满足要求的都会被拦截过来然后进行一些列的操作。具体需要自己去体会。。。 2、AbstractRoutingDataSource这个类是实现多数据源的关键他的作用就是动态切换数据源实质有多少个数据源就存多少个数据源在targetDataSources是AbstractRoutingDataSource的一个map类型的属性其中value为每个数据源key表示每个数据源的名字这个属性中然后根据determineCurrentLookupKey这个方法获取当前数据源在map中的key值然后determineTargetDataSource方法中动态获取当前数据源如果当前数据源不存并且默认数据源也不存在就抛出异常。 public abstract class AbstractRoutingDataSource extends AbstractDataSource implements InitializingBean {//多数据源map集合private MapObject, Object targetDataSources;//默认数据源private Object defaultTargetDataSource;//其实就是targetDataSources后面的afterPropertiesSet方法会将targetDataSources赋值给resolvedDataSourcesprivate MapObject, DataSource resolvedDataSources;private DataSource resolvedDefaultDataSource;public void setTargetDataSources(MapObject, Object targetDataSources) {this.targetDataSources targetDataSources;}protected DataSource determineTargetDataSource() {Assert.notNull(this.resolvedDataSources, DataSource router not initialized);Object lookupKey this.determineCurrentLookupKey();DataSource dataSource (DataSource)this.resolvedDataSources.get(lookupKey);if (dataSource null (this.lenientFallback || lookupKey null)) {dataSource this.resolvedDefaultDataSource;}if (dataSource null) {throw new IllegalStateException(Cannot determine target DataSource for lookup key [ lookupKey ]);} else {return dataSource;}}protected abstract Object determineCurrentLookupKey(); } 具体实现 1、定义一个动态数据源继承AbstractRoutingDataSource 抽象类并重写determineCurrentLookupKey方法 public class DynamicDataSource extends AbstractRoutingDataSource {Overrideprotected Object determineCurrentLookupKey() {DataSourceType.DataBaseType dataBaseType DataSourceType.getDataBaseType();return dataBaseType;}} 2、创建一个切换数据源类型的类 ThreadLocal这个知识点可以参考我的博客https://blog.csdn.net/tuesdayma/article/details/74841657 就是为了线程的安全性每个线程之间不会相互影响。 public class DataSourceType {public enum DataBaseType {TEST01, TEST02}// 使用ThreadLocal保证线程安全private static final ThreadLocalDataBaseType TYPE new ThreadLocalDataBaseType();// 往当前线程里设置数据源类型public static void setDataBaseType(DataBaseType dataBaseType) {if (dataBaseType null) {throw new NullPointerException();}System.err.println([将当前数据源改为] dataBaseType);TYPE.set(dataBaseType);}// 获取数据源类型public static DataBaseType getDataBaseType() {DataBaseType dataBaseType TYPE.get() null ? DataBaseType.TEST01 : TYPE.get();System.err.println([获取当前数据源的类型为] dataBaseType);return dataBaseType;}// 清空数据类型public static void clearDataBaseType() {TYPE.remove();}} 3、定义多个数据源怎么定义就不多说了和方法一是一样的主要是将定义好的多个数据源放在动态数据源中。 Configuration MapperScan(basePackages com.mzd.multipledatasources.mapper, sqlSessionFactoryRef SqlSessionFactory) public class DataSourceConfig {PrimaryBean(name test1DataSource)ConfigurationProperties(prefix spring.datasource.test1)public DataSource getDateSource1() {return DataSourceBuilder.create().build();}Bean(name test2DataSource)ConfigurationProperties(prefix spring.datasource.test2)public DataSource getDateSource2() {return DataSourceBuilder.create().build();}Bean(name dynamicDataSource)public DynamicDataSource DataSource(Qualifier(test1DataSource) DataSource test1DataSource,Qualifier(test2DataSource) DataSource test2DataSource) {MapObject, Object targetDataSource new HashMap();targetDataSource.put(DataSourceType.DataBaseType.TEST01, test1DataSource);targetDataSource.put(DataSourceType.DataBaseType.TEST02, test2DataSource);DynamicDataSource dataSource new DynamicDataSource();dataSource.setTargetDataSources(targetDataSource);dataSource.setDefaultTargetDataSource(test1DataSource);return dataSource;}Bean(name SqlSessionFactory)public SqlSessionFactory test1SqlSessionFactory(Qualifier(dynamicDataSource) DataSource dynamicDataSource)throws Exception {SqlSessionFactoryBean bean new SqlSessionFactoryBean();bean.setDataSource(dynamicDataSource);bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(classpath*:mapping/*.xml));return bean.getObject();} } 4、定义AOP就是不同业务切换不同数据库的入口。如果觉得execution太长不愿意写就可以定义一个注解来实现。可参考于我的博客https://blog.csdn.net/tuesdayma/article/details/79704238 Aspect Component public class DataSourceAop {Before(execution(* com.mzd.multipledatasources.service..*.test01*(..)))public void setDataSource2test01() {System.err.println(test01业务);DataSourceType.setDataBaseType(DataBaseType.TEST01);}Before(execution(* com.mzd.multipledatasources.service..*.test02*(..)))public void setDataSource2test02() {System.err.println(test02业务);DataSourceType.setDataBaseType(DataBaseType.TEST02);} } 整体目录如图  源代码https://github.com/mzd123/springboot-multipledatasources/tree/master
http://www.sadfv.cn/news/203348/

相关文章:

  • jquery验证网站地址网络电商是做什么的
  • 北京网站建设公司兴田德润实惠网站开发报价
  • 开源 html5网站模板wordpress侧边栏设置
  • 罗湖建设网站做网站后要回源码有何用
  • 上海有哪些做网站的公司网站外链建设记住5种外链方式不可用
  • 网站建设维护协议wordpress主题放在
  • 通辽市 做网站外贸营销工具
  • 做一个购物商城网站多少钱牡丹江47号公告
  • 网站推广宣传语开源wiki做网站
  • 免费行情软件app网站大全下载有图片如何制作网络
  • 建设工程信息比较好的网站跨境电商个人开店的平台
  • 阿里云主机 搭建网站开发制作小程序公司
  • 超级简历网站青岛开发区网站建设公司
  • 网站建设到备案263邮箱登录入口官网
  • 北京网站优化常识承德 网站维护
  • 怎么进成品网站后台简述网络营销的定义
  • 曼朗策划网站建设怎么依赖网站开发app
  • 廊坊网站建设方案托管东莞网站建设技术支持
  • 什么软件做网站最好金华专业的网站建设
  • 做网站怎么切psd图麻将app软件开发
  • 东莞整站优化排名哪些网站做面试题
  • 专门做房产的网站wordpress中文版和英文版
  • 农安县住房和城乡建设厅网站网站建设规划书样板
  • 哈尔滨php网站开发公司仿西部数码网站
  • 杭州做企业网站的公司做爰小视频网站
  • 哈尔滨大型网站建设贴吧网站怎么做
  • 做一个免费网站的流程汽车网站建设参考文献开题报告
  • 做美工用的网站网站公共模板是什么意思
  • 深圳定制网站建设服务公司网站和app可以做充值余额功能
  • 去视频网站做编辑网站建设公司好做吗