1\ Database Configuration
#test数据源spring. Datasource.test.url=jdbc:mysql://*:3306/db?useunicode=true&characterencoding=utf-8Spring.datasource.test.username=Rootspring.datasource.test.password=***#线上环境spring. Datasource.online.url=jdbc:mysql://127.0.0.1:3306/db?useunicode=true&characterencoding=utf-8Spring.datasource.online.username=Rootspring.datasource.online.password=***Spring.datasource.driver-class-name=Com.mysql.jdbc.Driverspring.datasource.type=com.alibaba.druid.pool.druiddatasource# The following supplemental settings for connection pooling, applied to all data sources above # initialization size, minimum, maximum spring.datasource.initialSize =5Spring.datasource.minIdle=1spring.datasource.maxActive= -# Configure the time to get the connection wait timeout spring.datasource.maxWait=60000# How long does the configuration interval take to detect idle connections that need to be closed, in milliseconds Spring.datasource.timeBetweenEvictionRunsMillis=60000# Configure the minimum time for a connection to survive in a pool, in milliseconds Spring.datasource.minEvictableIdleTimeMillis=300000Spring.datasource.validationQuery=SELECT 1 fromDUALspring.datasource.testWhileIdle=trueSpring.datasource.testOnBorrow=falseSpring.datasource.testOnReturn=false# Open Pscache, and specify the size of Pscache on each connection spring.datasource.poolPreparedStatements=false#spring. Datasource.maxpoolpreparedstatementperconnectionsize= -# Configure Monitoring Statistics interception filters, remove the post-monitoring interface SQL cannot be counted,'Wall'for firewall spring.datasource.filters=stat,wall,log4j# connectproperties properties to open mergesql function; Slow SQL Records spring.datasource.connectionProperties=druid.stat.mergesql=true;d ruid.stat.slowsqlmillis= the
2\ Data Source Configuration
PackageCom.test.config;ImportJavax.sql.DataSource;Importorg.apache.ibatis.session.SqlSessionFactory;ImportOrg.mybatis.spring.SqlSessionFactoryBean;Importorg.mybatis.spring.SqlSessionTemplate;ImportOrg.mybatis.spring.annotation.MapperScan;ImportOrg.springframework.beans.factory.annotation.Qualifier;Importorg.springframework.boot.context.properties.ConfigurationProperties;ImportOrg.springframework.context.annotation.Bean;Importorg.springframework.context.annotation.Configuration;ImportCom.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;Importorg.springframework.context.annotation.Primary;ImportOrg.springframework.core.io.support.PathMatchingResourcePatternResolver;ImportOrg.springframework.jdbc.datasource.DataSourceTransactionManager, @Configuration @mapperscan (basepackages = "Com.test.mapper.platform", Sqlsessiontemplateref = "Testsqlsessiontemplate") Public classplatformdatasourceconfig {@Bean (name= "Testdatasource") @ConfigurationProperties (prefix= "Spring.datasource.test") @Primary PublicDataSource Testdatasource () {returndruiddatasourcebuilder.create (). build (); } @Bean (Name= "Testsqlsessionfactory") @Primary PublicSqlsessionfactory testsqlsessionfactory (@Qualifier ("Testdatasource") DataSource DataSource)throwsException {Sqlsessionfactorybean bean=NewSqlsessionfactorybean (); Bean.setdatasource (DataSource); Bean.setmapperlocations (NewPathmatchingresourcepatternresolver (). Getresources ("Classpath:mapper/platform/*.xml")); returnBean.getobject (); } @Bean (Name= "Testtransactionmanager") @Primary PublicDatasourcetransactionmanager Testtransactionmanager (@Qualifier ("Testdatasource") DataSource DataSource) {return NewDatasourcetransactionmanager (DataSource); } @Bean (Name= "Testsqlsessiontemplate") @Primary PublicSqlsessiontemplate testsqlsessiontemplate (@Qualifier ("Testsqlsessionfactory") sqlsessionfactory SqlSessionFactory )throwsException {return Newsqlsessiontemplate (sqlsessionfactory); }}
3. Service Layer
Packagecom.test.service.test;ImportCom.test.mapper.platform.UserMapper;ImportCom.test.model.platform.User;Importorg.springframework.beans.factory.annotation.Autowired;ImportOrg.springframework.stereotype.Service; @Service Public classTest {@AutowiredPrivateUsermapper Usermapper; PublicTest () {} Public voidtesttest () {User User=usermapper.selectbyprimarykey (1); System.out.println ("///////////////////////////////"); System.out.println (User.getid ()); }}
4\
PackageCom.test.controller;Importcom.test.service.test.test;Importorg.springframework.beans.factory.annotation.Autowired;ImportOrg.springframework.stereotype.Controller;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RequestMethod;Importorg.springframework.web.bind.annotation.ResponseBody; @RequestMapping (Value= "/test") @Controller Public classTestCase {@AutowiredPrivatetest testtest; @RequestMapping (Value= "/index", method =requestmethod.get) @ResponseBody PublicString Index () {testtest.testtest (); return"Hello"; }}
Mybatis+druid+springboot Annotation Mode to configure multiple data sources