Browse Source

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

pull/2/head
qiaozhanwei 5 years ago committed by lgcareer
parent
commit
a6bd568df7
  1. 16
      dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/config/MybatisPlusConfig.java
  2. 26
      dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/config/YmlConfig.java
  3. 13
      dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/datasource/ConnectionFactory.java
  4. 63
      dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/datasource/DatabaseConfiguration.java
  5. 33
      dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/ConnectionFactoryTest.java

16
dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/config/MybatisPlusConfig.java

@ -1,3 +1,19 @@
/*
* 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;

26
dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/config/YmlConfig.java

@ -1,3 +1,19 @@
/*
* 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()){

13
dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/datasource/ConnectionFactory.java

@ -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);

63
dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/datasource/DatabaseConfiguration.java

@ -1,63 +0,0 @@
///*
// * 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());
// }
//}

33
dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/ConnectionFactoryTest.java

@ -0,0 +1,33 @@
/*
* 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);
}
}
Loading…
Cancel
Save