首页天道酬勤java读取数据库数据,mysql主从配置

java读取数据库数据,mysql主从配置

张世龙 05-05 20:17 118次浏览

/***主从psdbmh

@ mappers can (base packages=' com.wyq.mysqlreadwriteseparate.mapper ',sqlsessiontemplateref=' SQL template ' )

@ configuration properties (prefix=' spring.data source.master ' ) publicdatasourcemaster ({ returndruiddatasourceburcebuildatasource )

(}/***库(从*/@Bean

@ configuration properties (prefix=' spring.data source.slave ' ) publicdatasourceslaver ({ returndruiddatasourceburcebuildataddadarce )

}/***实例化数据源路由*/@ beanpublicdatasourcerouterdynamicdb (@ qualifier ) )数据源主数据源,

@ auto wired (要求=false ) qualifier (slaver ) DataSource slaveDataSource ) {

maptargetdatasources=new hashmap (;

target data sources.put (data source enum.master.get data source name ),masterDataSource ); 斯拉夫数据源!=空) {

target data sources.put (data source enum.slave.get data source name ),slaveDataSource );

}

动态数据源. settargetdatasources (目标数据源);

动态数据源. setdefaulttargetdatasource (主数据源; returndynamicDataSource;

(}/*** psdbmhsessionFactory

bean.setmapperlocations (newpathmatchingresourcepatternresolver (.get resources (' class path * : mapper/* mapper.) )

bean.setdata source (动态数据源; returnbean.getObject (;

}/* * *创建SQL模板

(}/***事务psdbmh

datasourcetransactionmanagerdatasourcetransactionmanager=newdatasourcetransactionmanager (;

datasourcetransactionmanager.setdata source (动态数据源); returndatasourcetransactionmanager;

}

}

shardingjdbc读写分离,为什么要用ssm不用ssh