• springboot+jndi+tomcat配置多数据源


    1.在application.properties中,添加jndi配置,如下图

     2.新建dataSourceConfig类

     3.dataSourceConfig类详细代码,这里只贴出其中一个,多个数据源类似配置,只需更改 basePackages 路径和@Value("${spring.datasource.source1.jndi-name}"),以及下面名称带test1前缀的地方,不要和其他dataSourceConfig重名

    @Configuration //注册到springboot 容器中
    @MapperScan(basePackages = "对应的Mapper包路径",sqlSessionTemplateRef = "test1SqlSessionTemplate")
    public class DataSource1Config {

    //application.properties中的jndi名称
    @Value("${spring.datasource.source1.jndi-name}")
    private String jndiName;

    @Bean(name = "test1DataSource",destroyMethod = "") // destroy method is disabled for Weblogic update app ability
    @ConfigurationProperties(prefix = "spring.datasource.bigdata")
    public DataSource bigdataDs() throws NamingException {
    JndiObjectFactoryBean bean=new JndiObjectFactoryBean();
    bean.setJndiName(jndiName);
    bean.setProxyInterface(DataSource.class);
    bean.setLookupOnStartup(false);
    bean.afterPropertiesSet();
    return (DataSource) bean.getObject();
    }

    @Bean(name = "test1SqlSessionFactory")
    public SqlSessionFactory testSqlSessionFactory(@Qualifier("test1DataSource") DataSource dataSource) throws Exception {
    SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
    bean.setDataSource(dataSource);
    //加载其他文件,如mapper.xml
    // bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/test1/*.xml"));
    return bean.getObject();
    }

    //事务管理
    @Bean(name = "test1TransactionManager")
    public DataSourceTransactionManager testTransactionManager(@Qualifier("test1DataSource") DataSource dataSource) {
    return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "test1SqlSessionTemplate")
    public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("test1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
    return new SqlSessionTemplate(sqlSessionFactory);
    }
    }
    4.打开tomcat目录下context.xml文件,添加以下配置,多个数据源写多个Resource即可,注意name需要和application.properties配置的名称一致

    <Resource name="jdbc/数据库名称" auth="Container" type="javax.sql.DataSource"
    maxTotal="100" maxIdle="30" maxWaitMillis="10000"
    username="javauser" password="javadude" driverClassName="com.mysql.jdbc.Driver"
    url="jdbc:mysql://localhost:3306/javatest"/>

     5.打成war包,部署到tomcat服务器运行,注意查看启动日志

  • 相关阅读:
    mysql存储过程详细教程
    js timestamp与datetime之间的相互转换
    spring集成httpclient配置
    Spring使用@scope("session")爆org.springframework.beans.factory.UnsatisfiedDependencyException
    算术编码Arithmetic Coding-高质量代码实现详解
    String Reduction
    (转)Maximum subarray problem--Kadane’s Algorithm
    判断一个字符串在至多删除k个字符后是否为回文串
    最小区间使得至少包含每组元素中的一个
    Pots of gold game:看谁拿的钱多
  • 原文地址:https://www.cnblogs.com/dhrs/p/11812469.html
Copyright © 2020-2023  润新知