提交 a6bd568d 编写于 作者: Q qiaozhanwei 提交者: lgcareer

add ConnectionFactoryTest and ConnectionFactory read datasource from appliction.yml (#931)

上级 0798b7d1
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dolphinscheduler.dao.config;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
......
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dolphinscheduler.dao.config;
......@@ -10,12 +26,9 @@ import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* Created by qiaozhanwei on 2019/9/17.
*/
public class YmlConfig {
private static Map<String,String> allMap=new HashMap<String,String>();
public static Map<String,String> allMap=new HashMap<String,String>();
static {
Yaml yaml = new Yaml();
InputStream inputStream = YmlConfig.class.getResourceAsStream("/application.yml");
......@@ -26,11 +39,6 @@ public class YmlConfig {
}
}
public static void main(String[] args) {
String ss = allMap.get("spring.datasource.url");
System.out.println(ss);
}
public static void iteratorYml(Map map,String key){
Iterator iterator = map.entrySet().iterator();
while(iterator.hasNext()){
......
......@@ -17,6 +17,7 @@
package org.apache.dolphinscheduler.dao.datasource;
import com.alibaba.druid.pool.DruidDataSource;
import org.apache.dolphinscheduler.dao.config.YmlConfig;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
......@@ -29,7 +30,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.sql.DataSource;
import java.util.Map;
/**
......@@ -45,11 +46,11 @@ public class ConnectionFactory {
*/
public static DruidDataSource getDataSource() {
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setDriverClassName("com.mysql.jdbc.Driver");
druidDataSource.setUrl("jdbc:mysql://192.168.220.188:3306/escheduler?useUnicode=true&characterEncoding=UTF-8");
druidDataSource.setUsername("root");
druidDataSource.setPassword("root@123");
Map<String, String> allMap = YmlConfig.allMap;
druidDataSource.setDriverClassName(allMap.get("spring.datasource.driver-class-name"));
druidDataSource.setUrl(allMap.get("spring.datasource.url"));
druidDataSource.setUsername(allMap.get("spring.datasource.username"));
druidDataSource.setPassword(allMap.get("spring.datasource.password"));
druidDataSource.setInitialSize(5);
druidDataSource.setMinIdle(5);
druidDataSource.setMaxActive(20);
......
///*
// * Licensed to the Apache Software Foundation (ASF) under one or more
// * contributor license agreements. See the NOTICE file distributed with
// * this work for additional information regarding copyright ownership.
// * The ASF licenses this file to You under the Apache License, Version 2.0
// * (the "License"); you may not use this file except in compliance with
// * the License. You may obtain a copy of the License at
// *
// * http://www.apache.org/licenses/LICENSE-2.0
// *
// * Unless required by applicable law or agreed to in writing, software
// * distributed under the License is distributed on an "AS IS" BASIS,
// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// * See the License for the specific language governing permissions and
// * limitations under the License.
// */
//package org.apache.dolphinscheduler.dao.datasource;
//
//import com.alibaba.druid.pool.DruidDataSource;
//import org.apache.ibatis.session.SqlSessionFactory;
//import org.mybatis.spring.SqlSessionFactoryBean;
//import org.mybatis.spring.annotation.MapperScan;
//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 org.springframework.jdbc.datasource.DataSourceTransactionManager;
//import org.springframework.transaction.PlatformTransactionManager;
//
//import java.sql.SQLException;
//
///**
// * data base configuration
// */
//@Configuration
//@PropertySource({"classpath:application.yml"})
//@MapperScan(basePackages = "org.apache.dolphinscheduler.dao.mapper", sqlSessionFactoryRef = "SqlSessionFactory")
//public class DatabaseConfiguration {
//
// /**
// * register data source
// */
// @Primary
// @Bean(name = "DataSource", initMethod = "init", destroyMethod = "close")
// public DruidDataSource dataSource() {
// return ConnectionFactory.getDataSource();
// }
//
// @Primary
// @Bean(name = "SqlSessionFactory")
// public SqlSessionFactory sqlSessionFactory() throws Exception {
// SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
// sqlSessionFactoryBean.setDataSource(dataSource());
//
// return sqlSessionFactoryBean.getObject();
// }
//
// @Primary
// @Bean(name = "TransactionManager")
// public PlatformTransactionManager transactionManager() throws SQLException {
// return new DataSourceTransactionManager(dataSource());
// }
//}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dolphinscheduler.dao.mapper;
import org.apache.dolphinscheduler.dao.datasource.ConnectionFactory;
import org.junit.Assert;
import org.junit.Test;
import java.sql.Connection;
public class ConnectionFactoryTest {
@Test
public void testConnection()throws Exception{
Connection connection = ConnectionFactory.getDataSource().getPooledConnection().getConnection();
Assert.assertEquals(connection != null , true);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册