zoukankan      html  css  js  c++  java
  • 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服务器运行,注意查看启动日志

  • 相关阅读:
    WPF之感触
    C# WinForm 给DataTable中指定位置添加列
    MyEclipse 8.6 download 官方下载地址
    将博客搬至CSDN
    Building Microservices with Spring Cloud
    Building Microservices with Spring Cloud
    Building Microservices with Spring Cloud
    Building Microservices with Spring Cloud
    Building Microservices with Spring Cloud
    Building Microservices with Spring Cloud
  • 原文地址:https://www.cnblogs.com/dhrs/p/11812469.html
Copyright © 2011-2022 走看看