当前位置:   article > 正文

java配置mysql和sqlserver双数据源_p.setproperty("mysql", "mysql");放置sqlserver的内容是什么

p.setproperty("mysql", "mysql");放置sqlserver的内容是什么

1.在application配置文件中增加如下配置

jdbc.driverClassName=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/warehouse?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
jdbc.username=root
jdbc.password=123456



spring.datasource.other.driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.other.url: jdbc:sqlserver://localhost:1433;DatabaseName=A
spring.datasource.other.username: SA
spring.datasource.other.password: Aa123456
spring.datasource.other.initialSize: 1
spring.datasource.other.minIdle: 5
spring.datasource.other.maxActive: 100
spring.datasource.other.maxWait: 60000
spring.datasource.other.testWhileIdle: true
spring.datasource.other.testOnReturn: false
spring.datasource.other.testOnBorrow: false
spring.datasource.other.validationQuery: SELECT 1s
spring.datasource.other.removeAbandoned: true
spring.datasource.other.removeAbandonedTimeout: 1800
spring.datasource.other.logAbandoned: true
spring.datasource.other.filters: mergeStat
spring.datasource.other.timeBetweenEvictionRunsMillis: 60000
spring.datasource.other.minEvictableIdleTimeMillis: 25200000
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

2.增加mysql的两个配置类



import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.PropertySource;

import javax.sql.DataSource;


@Configuration
@PropertySource(value = {"classpath:application.properties"})//也可以使用@ConfigurationProperties直接注入属性
public class DataSourceConfig {
    @Value("${jdbc.url}")
    private String jdbcUrl;

    @Value("${jdbc.driverClassName}")
    private String jdbcDriverClassName;

    @Value("${jdbc.username}")
    private String jdbcUsername;

    @Value("${jdbc.password}")
    private String jdbcPassword;

    /**
     * 配置数据库
     * @return
     */
    @Bean(name = "dataSource")
    public DataSource dataSource(){
        DruidDataSource datasource = new DruidDataSource();
        // 数据库驱动
        datasource.setDriverClassName(jdbcDriverClassName);
        // 相应驱动的jdbcUrl
        datasource.setUrl(jdbcUrl);
        // 数据库的用户名
        datasource.setUsername(jdbcUsername);
        // 数据库的密码
        datasource.setPassword(jdbcPassword);
        // 每个分区最大的连接数
        datasource.setMaxActive(20);
        // 每个分区最小的连接数
        datasource.setMinIdle(5);
        return datasource;

    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51

import com.github.pagehelper.PageHelper;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.type.TypeHandler;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import javax.sql.DataSource;
import java.util.Properties;


@MapperScan(basePackages = "com.wenying.mapper", sqlSessionFactoryRef = "sqlSessionFactory")
@Configuration
public class MyBatisConfig {

    @Qualifier("dataSource")
    @Autowired
    private DataSource dataSource;


    @Bean(name="sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(PageHelper pageHelper) {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        try {
            // 设置数据源
            sqlSessionFactoryBean.setDataSource(dataSource);
            Properties properties = new Properties();
            properties.setProperty("mapUnderscoreToCamelCase","true");
            //全局映射器启用缓存
            properties.setProperty("cacheEnabled","true");
            properties.setProperty("useGeneratedKeys","true");
            properties.setProperty("defaultExecutorType","REUSE");
            sqlSessionFactoryBean.setConfigurationProperties(properties);
            sqlSessionFactoryBean.setPlugins(new Interceptor[] { pageHelper });
            // 设置mybatis的主配置文件
            ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();

            //设置mybatis扫描的mapper.xml文件的路径(非常重要,否则找不到mapper.xml文件)
            sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:com/wenying/mapper/*.xml"));
            // 设置别名包,便于在mapper.xml文件中ParemeType和resultType不要写完整的包
            sqlSessionFactoryBean.setTypeAliasesPackage("com/wenying/dto");
           
            return sqlSessionFactoryBean.getObject();
        }catch (Exception e) {
            e.printStackTrace();
            throw  new RuntimeException();
        }

    }


    @Bean
    public PageHelper pageHelper() {
        PageHelper pageHelper = new PageHelper();
        Properties p = new Properties();
        p.setProperty("offsetAsPageNum", "true");
        p.setProperty("rowBoundsWithCount", "true");
        p.setProperty("reasonable", "true");
        p.setProperty("dialect", "mysql");
        pageHelper.setProperties(p);
        return pageHelper;
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73

3.配置sqlserver的一个配置文件



import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

/**
 * sqlerver数据库配置
 * @auth swy
 **/
@PropertySource(value = {"classpath:application.properties"})
@Configuration
@MapperScan(basePackages = "com.wenying.sqlservermapper", sqlSessionFactoryRef = "otherSqlSessionFactory")
public class MybatisDbOtherConfig {

    @Bean(name = "otherDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.other")
    public DataSource dataSource(){
        return DataSourceBuilder.create().build();
    }
    @Bean(name = "otherTransactionManager")
    public DataSourceTransactionManager transactionManager(@Qualifier("otherDataSource") DataSource dataSource){
        return new DataSourceTransactionManager(dataSource);
    }
    @Bean(name = "otherSqlSessionFactory")
    public SqlSessionFactory basicSqlSessionFactory(@Qualifier("otherDataSource") DataSource basicDataSource) throws Exception{
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(basicDataSource);
        factoryBean.setTypeAliasesPackage("com/wenying/sqlserverdto");
        factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:com/wenying/sqlservermapper/*Mapper.xml"));
        return factoryBean.getObject();
    }

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/148529
推荐阅读
相关标签
  

闽ICP备14008679号