springboot手动配置数据源:

时间:2018-12-13 15:22:31   收藏:0   阅读:525
@Configuration
    @EnableTransactionManagement
    @PropertySource(value = {"classpath:config/source.properties"})
    public class BeanConfig {
     
        @Autowired
        private Environment env;
     
        @Bean(destroyMethod = "close")
        public DataSource dataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(env.getProperty("source.driverClassName").trim());
        dataSource.setUrl(env.getProperty("source.url").trim());
        dataSource.setUsername(env.getProperty("source.username").trim());
        dataSource.setPassword(env.getProperty("source.password").trim());
        return dataSource;
        }
     
        @Bean
        public JdbcTemplate jdbcTemplate() {
        JdbcTemplate jdbcTemplate = new JdbcTemplate();
        jdbcTemplate.setDataSource(dataSource());
        return jdbcTemplate;
        }
    }

src/main/resources/config/source.properties 中配置数据源信息

原文:https://www.cnblogs.com/jsersudo/p/10114033.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!