背景
最近公司在服务拆迁,接口转移,相同的功能接口到要迁移到对应的服务中,因为时间比较赶,别问为什么没给时间,没人,没资源,但是活还是得干的,为了减少工作量和稳妥的需要分两步走
先迁移相关代码,保证包的路径不变,请求接口的路径不变
将迁移的相关代码进行迁表迁库(这目前还没做,计划9月实施)
实施
配置文件
数据库配置相关类
importcom.alibaba.druid.pool.DruidDataSource;importjava.io.Serializable;importjava.sql.SQLException;publicclassJdbcConfigimplementsSerializable{publicJdbcConfig(){super();//TODOAuto-generatedconstructorstub}publicbooleanisDecrypt(){returndecrypt;}publicvoidsetDecrypt(booleandecrypt){this.decrypt=decrypt;}publicStringgetDriverClass(){returndriverClass;}publicvoidsetDriverClass(StringdriverClass){this.driverClass=driverClass;}publicStringgetTerminalUrl(){returnterminalUrl;}publicvoidsetTerminalUrl(StringterminalUrl){this.terminalUrl=terminalUrl;}publicStringgetSlaveurl(){returnslaveurl;}publicvoidsetSlaveurl(Stringslaveurl){this.slaveurl=slaveurl;}publicStringgetUsername(){returnusername;}publicvoidsetUsername(Stringusername){this.username=username;}publicStringgetPassword(){returnpassword;}publicvoidsetPassword(Stringpassword){this.password=password;}publicintgetInitialSize(){returninitialSize;}publicvoidsetInitialSize(intinitialSize){this.initialSize=initialSize;}publicintgetMinIdle(){returnminIdle;}publicvoidsetMinIdle(intminIdle){this.minIdle=minIdle;}publicintgetMaxActive(){returnmaxActive;}publicvoidsetMaxActive(intmaxActive){this.maxActive=maxActive;}privatebooleandecrypt;privateStringdriverClass;privateStringterminalUrl;privateStringslaveurl;privateStringusername;privateStringpassword;privateintinitialSize;privateintminIdle;privateintmaxActive;publicDruidDataSourcemainds()throwsSQLException{DruidDataSourceds=ds();ds.setUrl(terminalUrl);returnds;}publicDruidDataSourceslaveds()throwsSQLException{DruidDataSourceds=ds();ds.setUrl(slaveurl);returnds;}privateDruidDataSourceds()throwsSQLException{DruidDataSourceds=newDruidDataSource();ds.setUrl(terminalUrl);ds.setUsername(username);ds.setPassword(password);ds.setFilters("config");ds.setConnectionProperties("config.decrypt="+decrypt);ds.setInitialSize(initialSize);ds.setMaxActive(maxActive);ds.setTimeBetweenEvictionRunsMillis(60000);ds.setMinEvictableIdleTimeMillis(300000);ds.setValidationQuery("select'X'");ds.setTestWhileIdle(true);ds.setTestOnBorrow(false);ds.setTestOnReturn(false);System.out.println("terminalUrl:"+terminalUrl);returnds;}}
相关配置
@Bean@ConfigurationProperties(prefix="jdbc")publicJdbcConfigjdbcConfig(){returnnewJdbcConfig();}@Bean@ConfigurationProperties(prefix="business.jdbc")publicJdbcConfigbusinessJdbcConfig(){returnnewJdbcConfig();}@Bean(initMethod="init",destroyMethod="close")publicDruidDataSourcebusinessDataSource()throwsSQLException{returnbusinessJdbcConfig().mainds();}@Bean(initMethod="init",destroyMethod="close")publicDruidDataSourcemasterDataSource()throwsSQLException{returnjdbcConfig().mainds();}@Bean("jdbctemplate")publicJdbcTemplatejdbcTemplate()throwsSQLException{JdbcTemplatet=newJdbcTemplate();t.setDataSource(masterDataSource());returnt;}@Bean("businessJdbctemplate")publicJdbcTemplatebusinessjdbcTemplate()throwsSQLException{JdbcTemplatet=newJdbcTemplate();t.setDataSource(businessDataSource());returnt;}
使用
@BeanpublicRobotDataDaorobotDataDao()throwsSQLException{RobotDataDaodao=newRobotDataDao();dao.setJdbcTemplate(jdbcTemplate());dao.setTableName("t_business_robot_data");dao.setPrimaryKey("id");dao.setSeqName("seq_t_business");returndao;}
@BeanpublicVDeviceDaovdeviceDao()throwsSQLException{VDeviceDaodao=newVDeviceDao();dao.setJdbcTemplate(businessjdbcTemplate());dao.setTableName("t_device");dao.setPrimaryKey("id");dao.setSeqName("seq_t_default");returndao;}
特别注意的,一定要配置的,因为现在有多数据源了就要配置对应的事务配置,单个默认的,多个就要指定
@ConfigurationpublicclassTransactionConfig{@BeanpublicPlatformTransactionManagerbfscrmTransactionManager(@Qualifier("masterDataSource")DataSourcemasterDataSource){returnnewDataSourceTransactionManager(masterDataSource);}}
这就配置好了多个数据源了。