From e2f66e2343b58d141830aa63de2c8ecaec7f36a6 Mon Sep 17 00:00:00 2001 From: lgcareer <18610854716@163.com> Date: Fri, 11 Oct 2019 14:01:42 +0800 Subject: [PATCH] get SqlSessionFactory from MybatisSqlSessionFactoryBean (#985) * rename from DatasourceUserMapper to DataSourceUserMapper * add unit test in UserMapper and WorkerGroupMapper * change cn.escheduler to org.apache.dolphinscheduler * add unit test in UdfFuncMapperTest * add unit test in UdfFuncMapperTest * remove DatabaseConfiguration * add ConnectionFactoryTest * cal duration in processInstancesList * change desc to description * change table name in mysql ddl * change table name in mysql ddl * change escheduler to dolphinscheduler * change escheduler to dolphinscheduler * change escheduler to dolphinscheduler * remove log4j-1.2-api and modify AlertMapperTest * remove log4j-1.2-api * Add alertDao to spring management * Add alertDao to spring management * get SqlSessionFactory from MybatisSqlSessionFactoryBean --- .../apache/dolphinscheduler/dao/AlertDao.java | 11 +++--- .../dao/datasource/ConnectionFactory.java | 35 +++++++++++++------ 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/AlertDao.java b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/AlertDao.java index 4a2076cef..b5bee4ab8 100644 --- a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/AlertDao.java +++ b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/AlertDao.java @@ -16,16 +16,17 @@ */ package org.apache.dolphinscheduler.dao; +import org.apache.commons.lang3.StringUtils; import org.apache.dolphinscheduler.common.enums.AlertStatus; import org.apache.dolphinscheduler.common.enums.AlertType; import org.apache.dolphinscheduler.common.enums.ShowType; +import org.apache.dolphinscheduler.dao.datasource.ConnectionFactory; import org.apache.dolphinscheduler.dao.entity.Alert; -import org.apache.dolphinscheduler.dao.mapper.AlertMapper; -import org.apache.dolphinscheduler.dao.mapper.UserAlertGroupMapper; import org.apache.dolphinscheduler.dao.entity.ProcessDefinition; import org.apache.dolphinscheduler.dao.entity.ProcessInstance; import org.apache.dolphinscheduler.dao.entity.User; -import org.apache.commons.lang3.StringUtils; +import org.apache.dolphinscheduler.dao.mapper.AlertMapper; +import org.apache.dolphinscheduler.dao.mapper.UserAlertGroupMapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -47,8 +48,8 @@ public class AlertDao extends AbstractBaseDao { @Override protected void init() { -// alertMapper = ConnectionFactory.getSqlSession().getMapper(AlertMapper.class); -// userAlertGroupMapper = ConnectionFactory.getSqlSession().getMapper(UserAlertGroupMapper.class); + alertMapper = ConnectionFactory.getSqlSession().getMapper(AlertMapper.class); + userAlertGroupMapper = ConnectionFactory.getSqlSession().getMapper(UserAlertGroupMapper.class); } /** diff --git a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/datasource/ConnectionFactory.java b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/datasource/ConnectionFactory.java index aa1d4a77c..3dbb290c3 100644 --- a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/datasource/ConnectionFactory.java +++ b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/datasource/ConnectionFactory.java @@ -17,19 +17,20 @@ package org.apache.dolphinscheduler.dao.datasource; import com.alibaba.druid.pool.DruidDataSource; +import com.baomidou.mybatisplus.core.MybatisConfiguration; +import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean; import org.apache.commons.configuration.ConfigurationException; import org.apache.commons.configuration.PropertiesConfiguration; import org.apache.dolphinscheduler.common.Constants; import org.apache.ibatis.mapping.Environment; -import org.apache.ibatis.session.Configuration; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; -import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.apache.ibatis.transaction.TransactionFactory; import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory; import org.mybatis.spring.SqlSessionTemplate; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import javax.sql.DataSource; @@ -43,7 +44,7 @@ public class ConnectionFactory { private static SqlSessionFactory sqlSessionFactory; /** - * 加载配置文件 + * Load configuration file */ private static org.apache.commons.configuration.Configuration conf; @@ -79,7 +80,7 @@ public class ConnectionFactory { /** * get sql session factory */ - public static SqlSessionFactory getSqlSessionFactory() { + public static SqlSessionFactory getSqlSessionFactory() throws Exception { if (sqlSessionFactory == null) { synchronized (ConnectionFactory.class) { if (sqlSessionFactory == null) { @@ -88,13 +89,17 @@ public class ConnectionFactory { Environment environment = new Environment("development", transactionFactory, dataSource); - Configuration configuration = new Configuration(environment); + MybatisConfiguration configuration = new MybatisConfiguration(); + configuration.setEnvironment(environment); configuration.setLazyLoadingEnabled(true); configuration.addMappers("org.apache.dolphinscheduler.dao.mapper"); - - SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder(); - sqlSessionFactory = builder.build(configuration); + MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean(); + sqlSessionFactoryBean.setDataSource(dataSource); + sqlSessionFactoryBean.setTypeEnumsPackage("org.apache.dolphinscheduler.*.enums"); + sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:org/apache/dolphinscheduler/dao/mapper/*.xml")); + sqlSessionFactoryBean.setConfiguration(configuration); + return sqlSessionFactoryBean.getObject(); } } } @@ -106,10 +111,20 @@ public class ConnectionFactory { * get sql session */ public static SqlSession getSqlSession() { - return new SqlSessionTemplate(getSqlSessionFactory()); + try { + return new SqlSessionTemplate(getSqlSessionFactory()); + } catch (Exception e) { + logger.error(e.getMessage(),e); + throw new RuntimeException("get sqlSession failed!"); + } } public static T getMapper(Class type){ - return getSqlSession().getMapper(type); + try { + return getSqlSession().getMapper(type); + } catch (Exception e) { + logger.error(e.getMessage(),e); + throw new RuntimeException("get mapper failed!"); + } } } -- GitLab