Commit be30372a by zhouhaibao

[+]sop增加sql脚本执行器

1 parent 1cf372ff
Showing 20 changed files with 1435 additions and 3 deletions
package cn.com.glsx.process.modules.entity;
import javax.persistence.*;
@Table(name = "sop_data_source")
public class SopDataSource {
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
private Integer id;
/**
* 数据源类型 MYSQL,ORACLE,MSSQL,POSTGRESQL
*/
@Column(name = "db_type")
private String dbType;
/**
* ip地址
*/
@Column(name = "db_server")
private String dbServer;
/**
* 端口
*/
@Column(name = "db_port")
private Integer dbPort;
/**
* 账号
*/
@Column(name = "db_user_name")
private String dbUserName;
/**
* 密码
*/
@Column(name = "db_pwd")
private String dbPwd;
/**
* 数据源名称
*/
private String name;
@Column(name = "db_name")
private String dbName;
/**
* @return id
*/
public Integer getId() {
return id;
}
/**
* @param id
*/
public void setId(Integer id) {
this.id = id;
}
/**
* 获取数据源类型 MYSQL,ORACLE,MSSQL,POSTGRESQL
*
* @return db_type - 数据源类型 MYSQL,ORACLE,MSSQL,POSTGRESQL
*/
public String getDbType() {
return dbType;
}
/**
* 设置数据源类型 MYSQL,ORACLE,MSSQL,POSTGRESQL
*
* @param dbType 数据源类型 MYSQL,ORACLE,MSSQL,POSTGRESQL
*/
public void setDbType(String dbType) {
this.dbType = dbType == null ? null : dbType.trim();
}
/**
* 获取ip地址
*
* @return db_server - ip地址
*/
public String getDbServer() {
return dbServer;
}
/**
* 设置ip地址
*
* @param dbServer ip地址
*/
public void setDbServer(String dbServer) {
this.dbServer = dbServer == null ? null : dbServer.trim();
}
/**
* 获取账号
*
* @return db_user_name - 账号
*/
public String getDbUserName() {
return dbUserName;
}
/**
* 设置账号
*
* @param dbUserName 账号
*/
public void setDbUserName(String dbUserName) {
this.dbUserName = dbUserName == null ? null : dbUserName.trim();
}
/**
* 获取密码
*
* @return db_pwd - 密码
*/
public String getDbPwd() {
return dbPwd;
}
/**
* 设置密码
*
* @param dbPwd 密码
*/
public void setDbPwd(String dbPwd) {
this.dbPwd = dbPwd == null ? null : dbPwd.trim();
}
/**
* 获取数据源名称
*
* @return name - 数据源名称
*/
public String getName() {
return name;
}
/**
* 设置数据源名称
*
* @param name 数据源名称
*/
public void setName(String name) {
this.name = name == null ? null : name.trim();
}
/**
* @return db_name
*/
public String getDbName() {
return dbName;
}
/**
* @param dbName
*/
public void setDbName(String dbName) {
this.dbName = dbName == null ? null : dbName.trim();
}
public Integer getDbPort() {
return dbPort;
}
public void setDbPort(Integer dbPort) {
this.dbPort = dbPort;
}
}
\ No newline at end of file \ No newline at end of file
package cn.com.glsx.process.modules.mapper;
import cn.com.glsx.process.modules.entity.SopDataSource;
import com.glsx.plat.mybatis.mapper.CommonBaseMapper;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface SopDataSourceMapper extends CommonBaseMapper<SopDataSource> {
}
\ No newline at end of file \ No newline at end of file
...@@ -50,6 +50,29 @@ ...@@ -50,6 +50,29 @@
<artifactId>rpc-scrm-proxy-api</artifactId> <artifactId>rpc-scrm-proxy-api</artifactId>
<version>3.7.0-SNAPSHOT</version> <version>3.7.0-SNAPSHOT</version>
</dependency> </dependency>
<dependency>
<groupId>commons-dbutils</groupId>
<artifactId>commons-dbutils</artifactId>
<version>1.6</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>12.1.0.1-atlassian-hosted</version>
</dependency>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-901-1.jdbc4</version>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc4</artifactId>
<version>4.0</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
......
/*
* @Copyright (c) 2018 缪聪(mcg-helper@qq.com)
*
* Licensed 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 cn.com.glsx.process.common.sysenum;
/**
*
* @ClassName: DatabaseTypeEnum
* @Description: TODO(支持的数据库)
* @author: 缪聪(mcg-helper@qq.com)
* @date: 2018年3月9日 下午2:55:27
*
*/
public enum DatabaseTypeEnum {
MYSQL("MYSQL", "MYSQL", "com.mysql.jdbc.Driver"), ORACLE("ORACLE", "ORACLE", "oracle.jdbc.driver.OralceDriver"), MSSQL("MSSQL", "MSSQL", "com.microsoft.sqlserver.jdbc.SQLServerDriver"), POSTGRESQL("POSTGRESQL", "POSTGRESQL", "org.postgresql.Driver");
private String name;
private String value;
private String driverClass;
private DatabaseTypeEnum(String name, String value, String driverClass) {
this.name = name;
this.value = value;
this.driverClass = driverClass;
}
public static DatabaseTypeEnum getDatabaseByName(String name) {
for (DatabaseTypeEnum c : DatabaseTypeEnum.values()) {
if (c.getName().equals(name)) {
return c;
}
}
return null;
}
public static String getValueByName(String name) {
for (DatabaseTypeEnum c : DatabaseTypeEnum.values()) {
if (c.getName().equals(name)) {
return c.value;
}
}
return null;
}
public static String getNameByValue(String value) {
for (DatabaseTypeEnum c : DatabaseTypeEnum.values()) {
if (c.getValue().equals(value)) {
return c.name;
}
}
return null;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getDriverClass() {
return driverClass;
}
public void setDriverClass(String driverClass) {
this.driverClass = driverClass;
}
}
\ No newline at end of file \ No newline at end of file
package cn.com.glsx.process.modules.service;
import cn.com.glsx.process.modules.entity.SopDataSource;
import cn.com.glsx.process.modules.mapper.SopDataSourceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class SopDataSourceService {
@Autowired
private SopDataSourceMapper sopDataSourceMapper;
public SopDataSource getById(Integer id){
return sopDataSourceMapper.selectByPrimaryKey(id);
};
}
/*
* @Copyright (c) 2018 缪聪(mcg-helper@qq.com)
*
* Licensed 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 cn.com.glsx.process.plugin.dbconn;
import org.apache.commons.dbutils.DbUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
public abstract class AbstractConnect {
private static Logger logger = LoggerFactory.getLogger(AbstractConnect.class);
public DbConnect dbConnect;
public AbstractConnect(DataSource dataSource) {
super();
initDbConnect(dataSource);
}
/**
* 初始化
*
* @see [类、类#方法、类#成员]
*/
private void initDbConnect(DataSource dataSource) {
if (dbConnect == null) {
dbConnect = new DbConnect(dataSource);
}
}
/**
* 带可变参数查询,返回执行结果
*
* @param sql
* 查询sql
* @param para
* 可变参数
* @return
* @throws SQLException
*/
public List<Map<String, Object>> querySql(String sql, Object... para) throws SQLException {
return dbConnect.querySql(sql, para);
}
/**
* 带可变参数查询,返回执行结果
*
* @param <T>
*
* @param sql
* 查询sql
* @param para
* 可变参数
* @return
* @throws SQLException
*/
@SuppressWarnings("unchecked")
public <T> List<T> executeQuery(T clazz, String sql, Object... para) throws SQLException {
return dbConnect.querySql(clazz, sql, para);
}
/**
* 带可变参数查询,返回long类型数据
*
* @param countSql
* 查询记录条数的sql
* @param para
* 可变参数
* @return
* @throws SQLException
* @throws SQLException
*/
public Long queryForLong(String countSql, Object... para) throws SQLException {
return dbConnect.queryForLong(countSql, para);
}
/**
* 带可变参数, 执行sql插入,返回新增记录的自增主键<BR>
* 注意: 若插入的表无自增主键则返回 0,异常的话则返回 null
*
* @param sql
* @param para
* @return
* @throws SQLException
* @see [类、类#方法、类#成员]
*/
public Long executeInsert(String sql, Object... para) throws SQLException {
return dbConnect.insertSql(sql, para);
}
/**
* 带可变参数, 执行sql,返回执行影响的记录条数
*
* @param sql
* 执行的sql 语句
* @param para
* 可变参数
* @return
* @throws SQLException
*/
public int executeUpdate(String sql, Object... para) throws SQLException {
return dbConnect.executeUpdate(sql, para);
}
public boolean testConnect() {
boolean result = false;
try {
Connection connection = dbConnect.getDataSource().getConnection();
if (connection != null) {
DbUtils.closeQuietly(connection);
result = true;
}
} catch (SQLException e) {
logger.error("测试连接出错,异常信息:", e);
return result;
}
return result;
}
}
\ No newline at end of file \ No newline at end of file
/*
* @Copyright (c) 2018 缪聪(mcg-helper@qq.com)
*
* Licensed 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 cn.com.glsx.process.plugin.dbconn;
import org.apache.commons.dbutils.*;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
*
* 带事务支持的数据库底层操作类
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public class DbConnect {
private static Logger logger = LoggerFactory.getLogger(DbConnect.class);
private DataSource dataSource;
private ThreadLocal<Connection> threadLocal = new ThreadLocal<Connection>();
public DbConnect(DataSource dataSource) {
this.dataSource = dataSource;
}
/**
* 创建数据库连接
*
* @return
* @see [类、类#方法、类#成员]
*/
public Connection getConnection() {
Connection connection = threadLocal.get();
try {
if (connection == null) {
connection = dataSource.getConnection();
threadLocal.set(connection);
}
} catch (SQLException e) {
logger.error("获取数据库连接失败,异常信息:", e.getMessage());
throw new RuntimeException("获取数据库连接失败.....");
}
return connection;
}
/**
* 带可变参数查询,返回执行结果
*
* @param sql 查询sql
* @param para 可变参数
* @return
*/
public List<Map<String, Object>> querySql(String sql, Object... para) throws SQLException {
logger.debug("查询Sql: {}, 查询参数: {}", sql, ToStringBuilder.reflectionToString(para));
QueryRunner runner = new QueryRunner();
Connection conn = null;
List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
try {
conn = getConnection();
result = runner.query(conn, sql, new MapListHandler(), para);
} catch (SQLException e) {
logger.error("查询出错,异常信息: {}", e.getMessage());
throw e;
} finally {
if (conn != null && conn.getAutoCommit() == true) {
freeConnection();
}
}
return result;
}
/**
*
* @Title: querySql
* @Description: TODO(带可变参数查询,返回执行结果)
* @param: @param clazz 转换的对象实例
* @param: @param sql 查询sql语句
* @param: @param para 查询参数
* @param: @return
* @param: @throws SQLException
* @return: List
* @throws
*/
public <T> List querySql(T clazz, String sql, Object... para) throws SQLException {
logger.debug("查询Sql: {}, 查询参数: {}", sql, ToStringBuilder.reflectionToString(para));
QueryRunner runner = new QueryRunner();
Connection conn = null;
List<T> result = new ArrayList<T>();
try {
conn = getConnection();
// 下划线分隔的表字段名转换为实体bean驼峰命名属性
BeanProcessor bean = new GenerousBeanProcessor();
RowProcessor processor = new BasicRowProcessor(bean);
result = (List<T>) runner.query(conn, sql, new BeanListHandler((Class) clazz, processor), para);
} catch (SQLException e) {
logger.error("查询出错,异常信息: {}------", e.getMessage());
throw e;
} finally {
if (conn != null && conn.getAutoCommit() == true) {
freeConnection();
}
}
return result;
}
/**
*
* @Title: queryForLong
* @Description: TODO(带可变参数查询,返回long类型数据)
* @param: @param countSql 查询记录总条数
* @param: @param para 参数
* @param: @return
* @param: @throws SQLException
* @return: Long
* @throws
*/
public Long queryForLong(String countSql, Object... para) throws SQLException {
logger.debug("queryForLong: {}, para: {}", countSql, ToStringBuilder.reflectionToString(para));
QueryRunner runner = new QueryRunner();
Long number = null;
Connection conn = null;
try {
conn = getConnection();
number = runner.query(conn, countSql, new ScalarHandler<Long>(), para);
} catch (SQLException e) {
logger.error("------queryForLong error: {}------", e.getMessage());
throw e;
} finally {
if (conn != null && conn.getAutoCommit() == true) {
freeConnection();
}
}
return number;
}
/**
*
* @Title: executeUpdate
* @Description: TODO(带可变参数, 执行sql,返回执行影响的记录条数)
* @param: @param sql 执行的sql语句
* @param: @param para 参数
* @param: @return
* @param: @throws SQLException
* @return: int
* @throws
*/
public int executeUpdate(String sql, Object... para) throws SQLException {
logger.debug("executeUpdate: {}, para: {}", sql, ToStringBuilder.reflectionToString(para));
QueryRunner runner = new QueryRunner();
Connection conn = null;
int count = 0;
try {
conn = getConnection();
count = runner.update(conn, sql, para);
} catch (SQLException e) {
logger.error("------executeUpdate error: {}------", e.getMessage());
throw e;
} finally {
if (conn != null && conn.getAutoCommit() == true) {
freeConnection();
}
}
return count;
}
/**
*
* @Title: insertSql
* @Description: TODO(带可变参数, 执行sql插入,返回新增记录的自增主键。注意: 若插入的表无自增主键则返回 0,异常的话则返回 null)
* @param: @param sql 执行的sql语句
* @param: @param para 参数
* @param: @return
* @param: @throws SQLException
* @return: Long
* @throws
*/
public Long insertSql(String sql, Object... para) throws SQLException {
logger.debug("InsertSql: {}, para: {}", sql, ToStringBuilder.reflectionToString(para));
QueryRunner runner = new QueryRunner();
Connection conn = null;
Long id = null;
try {
conn = getConnection();
id = (Long) runner.insert(conn, sql, new ScalarHandler<Object>(), para);
} catch (SQLException e) {
logger.error("------insertSql error: {}------", e.getMessage());
throw e;
} finally {
if (conn != null && conn.getAutoCommit() == true) {
freeConnection();
}
}
return id;
}
/**
*
* @Title: executeBatch
* @Description: TODO(批量更新)
* @param: @param sql 执行的sql语句
* @param: @param params 二维数组参数
* @param: @throws SQLException
* @return: void
* @throws
*/
public void executeBatch(String sql, Object[][] params) throws SQLException {
logger.debug("executeBatch: {}, params:{}", sql, ToStringBuilder.reflectionToString(params));
QueryRunner runner = new QueryRunner();
Connection conn = null;
try {
conn = getConnection();
runner.batch(conn, sql, params);
} catch (SQLException e) {
logger.error("------executeBatch Error:{}------", e.getMessage());
throw e;
} finally {
if (conn != null && conn.getAutoCommit() == true) {
freeConnection();
}
}
}
/**
*
* @Title: executeBatch
* @Description: TODO(批量更新)
* @param: @param sql 执行的sql语句
* @param: @param params 参数
* @param: @throws SQLException
* @return: void
* @throws
*/
public void executeBatch(String sql, List<Object[]> params) throws SQLException {
Object[][] paramArr = params.toArray(new Object[0][]);
executeBatch(sql, paramArr);
}
/**
*
* @Title: freeConnection
* @Description: TODO(释放数据库连接)
* @param:
* @return: void
* @throws
*/
public void freeConnection() {
logger.debug("------释放数据库连接------");
Connection conn = threadLocal.get();
if (conn != null) {
DbUtils.closeQuietly(conn);
threadLocal.remove(); // 解除当前线程上绑定conn
}
}
/**
*
* @Title: commit
* @Description: TODO(提交事务)
* @param:
* @return: void
* @throws
*/
public void commit() {
logger.debug("------提交事务-------");
try {
Connection conn = threadLocal.get();
if (conn != null) {
conn.commit();
}
} catch (SQLException e) {
logger.error("提交事务失败,异常信息:{}", e.getMessage());
}
}
/**
*
* @Title: rollback
* @Description: TODO(回滚事务)
* @param:
* @return: void
*/
public void rollback() {
logger.debug("------ 系统异常,回滚事务------");
try {
Connection conn = threadLocal.get();
if (conn != null) {
conn.rollback();
}
} catch (SQLException e) {
logger.error("回滚事务失败,异常信息:{}", e.getMessage());
}
}
public DataSource getDataSource() {
return dataSource;
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
}
\ No newline at end of file \ No newline at end of file
/*
* @Copyright (c) 2018 缪聪(mcg-helper@qq.com)
*
* Licensed 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 cn.com.glsx.process.plugin.dbconn;
import cn.com.glsx.process.common.sysenum.DatabaseTypeEnum;
import cn.com.glsx.process.modules.entity.SopDataSource;
import cn.com.glsx.process.plugin.dbconn.mssql.MssqlConnectImpl;
import cn.com.glsx.process.plugin.dbconn.mysql.MysqlConnectImpl;
import cn.com.glsx.process.plugin.dbconn.oracle.OracleConnectImpl;
import cn.com.glsx.process.plugin.dbconn.postgresql.PostgresqlConnectImpl;
import com.microsoft.sqlserver.jdbc.SQLServerDataSource;
import com.mysql.cj.jdbc.MysqlDataSource;
import oracle.jdbc.pool.OracleDataSource;
import org.postgresql.ds.PGPoolingDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
public class FlowDataAdapterImpl implements McgBizAdapter {
private static Logger logger = LoggerFactory.getLogger(FlowDataAdapterImpl.class);
private McgConnect mcgConnect;
public FlowDataAdapterImpl(SopDataSource mcgDataSource) {
if(DatabaseTypeEnum.MYSQL.getValue().equals(mcgDataSource.getDbType())) {
MysqlDataSource mysqlDataSource = new MysqlDataSource();
mysqlDataSource.setServerName(mcgDataSource.getDbServer());
mysqlDataSource.setPort(mcgDataSource.getDbPort());
mysqlDataSource.setDatabaseName(mcgDataSource.getDbName());
try {
mysqlDataSource.setCharacterEncoding("UTF-8");
mysqlDataSource.setCharacterSetResults("UTF-8");
} catch (SQLException e) {
throw new RuntimeException(e);
}
mysqlDataSource.setUser(mcgDataSource.getDbUserName());
mysqlDataSource.setPassword(mcgDataSource.getDbPwd());
try {
mysqlDataSource.setConnectTimeout(3000);
} catch (SQLException e) {
logger.error("mysql数据源适配失败,异常信息:", e);
}
mcgConnect = new MysqlConnectImpl(mysqlDataSource);
} else if(DatabaseTypeEnum.ORACLE.getValue().equals(mcgDataSource.getDbType())) {
try {
OracleDataSource oracleDataSource = new OracleDataSource();
oracleDataSource.setDriverType("thin");
oracleDataSource.setNetworkProtocol("tcp");
oracleDataSource.setServerName(mcgDataSource.getDbServer());
oracleDataSource.setDatabaseName(mcgDataSource.getDbName());
oracleDataSource.setPortNumber(mcgDataSource.getDbPort());
oracleDataSource.setUser(mcgDataSource.getDbUserName());
oracleDataSource.setPassword(mcgDataSource.getDbPwd());
oracleDataSource.setLoginTimeout(3000);
mcgConnect = new OracleConnectImpl(oracleDataSource);
} catch (SQLException e) {
logger.error("oracle数据源适配失败,异常信息:", e);
}
} else if(DatabaseTypeEnum.MSSQL.getValue().equals(mcgDataSource.getDbType())) {
try {
SQLServerDataSource sqlServerDataSource = new SQLServerDataSource();
sqlServerDataSource.setDatabaseName(mcgDataSource.getDbName());
sqlServerDataSource.setUser(mcgDataSource.getDbUserName());
sqlServerDataSource.setPassword(mcgDataSource.getDbPwd());
sqlServerDataSource.setServerName(mcgDataSource.getDbServer());
sqlServerDataSource.setPortNumber(mcgDataSource.getDbPort());
sqlServerDataSource.setLoginTimeout(3000);
mcgConnect = new MssqlConnectImpl(sqlServerDataSource);
} catch (Exception e) {
logger.error("oracle数据源适配失败,异常信息:", e);
}
} else if(DatabaseTypeEnum.POSTGRESQL.getValue().equals(mcgDataSource.getDbType())) {
PGPoolingDataSource pgDataSource = new PGPoolingDataSource();
try {
pgDataSource.setDatabaseName(mcgDataSource.getDbName());
pgDataSource.setUser(mcgDataSource.getDbUserName());
pgDataSource.setPassword(mcgDataSource.getDbPwd());
pgDataSource.setPortNumber(mcgDataSource.getDbPort());
pgDataSource.setServerName(mcgDataSource.getDbServer());
pgDataSource.setLoginTimeout(3000);
} catch (SQLException e) {
logger.error("postgresql数据源适配失败,异常信息:", e);
}
mcgConnect = new PostgresqlConnectImpl(pgDataSource);
}
}
@Override
public List<Map<String, Object>> tableQuery(String sql, Object... para) throws SQLException {
return mcgConnect.querySql(sql, para);
}
@Override
public int executeUpdate(String sql, Object... para) throws SQLException {
return mcgConnect.executeUpdate(sql, para);
}
@Override
public boolean testConnect() {
boolean flag = false;
try {
flag = mcgConnect.testConnect();
} catch (Exception e) {
logger.error("数据连接测试失败,异常信息:", e);
}
return flag;
}
}
\ No newline at end of file \ No newline at end of file
/*
* @Copyright (c) 2018 缪聪(mcg-helper@qq.com)
*
* Licensed 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 cn.com.glsx.process.plugin.dbconn;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
public interface McgBizAdapter {
boolean testConnect();
List<Map<String, Object>> tableQuery(String sql, Object... para) throws SQLException;
public int executeUpdate(String sql, Object... para) throws SQLException;
}
/*
* @Copyright (c) 2018 缪聪(mcg-helper@qq.com)
*
* Licensed 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 cn.com.glsx.process.plugin.dbconn;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
public interface McgConnect {
boolean testConnect();
List<Map<String, Object>> querySql(String sql, Object... para) throws SQLException;
int executeUpdate(String sql, Object... para) throws SQLException;
}
/*
* @Copyright (c) 2018 缪聪(mcg-helper@qq.com)
*
* Licensed 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 cn.com.glsx.process.plugin.dbconn;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.Serializable;
@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement
public class McgDataSource implements Serializable {
private static final long serialVersionUID = -7416980731775213841L;
@XmlElement
private String dataSourceId;
@XmlElement
private String name;
@XmlElement
private String dbType;
@XmlElement
private String dbServer;
@XmlElement
private Integer dbPort;
@XmlElement
private String dbName;
@XmlElement
private String dbUserName;
@XmlElement
private String dbPwd;
@XmlElement
private String note;
public String getDataSourceId() {
return dataSourceId;
}
public void setDataSourceId(String dataSourceId) {
this.dataSourceId = dataSourceId;
}
public String getDbType() {
return dbType;
}
public void setDbType(String dbType) {
this.dbType = dbType;
}
public String getDbServer() {
return dbServer;
}
public void setDbServer(String dbServer) {
this.dbServer = dbServer;
}
public String getDbName() {
return dbName;
}
public void setDbName(String dbName) {
this.dbName = dbName;
}
public String getDbUserName() {
return dbUserName;
}
public void setDbUserName(String dbUserName) {
this.dbUserName = dbUserName;
}
public String getDbPwd() {
return dbPwd;
}
public void setDbPwd(String dbPwd) {
this.dbPwd = dbPwd;
}
public Integer getDbPort() {
return dbPort;
}
public void setDbPort(Integer dbPort) {
this.dbPort = dbPort;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
/*
* @Copyright (c) 2018 缪聪(mcg-helper@qq.com)
*
* Licensed 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 cn.com.glsx.process.plugin.dbconn.mssql;
import cn.com.glsx.process.plugin.dbconn.AbstractConnect;
import cn.com.glsx.process.plugin.dbconn.McgConnect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.sql.DataSource;
public class MssqlConnectImpl extends AbstractConnect implements McgConnect {
private static Logger logger = LoggerFactory.getLogger(MssqlConnectImpl.class);
public MssqlConnectImpl(DataSource dataSource) {
super(dataSource);
}
}
/*
* @Copyright (c) 2018 缪聪(mcg-helper@qq.com)
*
* Licensed 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 cn.com.glsx.process.plugin.dbconn.mysql;
import cn.com.glsx.process.plugin.dbconn.AbstractConnect;
import cn.com.glsx.process.plugin.dbconn.McgConnect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.sql.DataSource;
public class MysqlConnectImpl extends AbstractConnect implements McgConnect {
private static Logger logger = LoggerFactory.getLogger(MysqlConnectImpl.class);
public MysqlConnectImpl(DataSource dataSource) {
super(dataSource);
}
}
/*
* @Copyright (c) 2018 缪聪(mcg-helper@qq.com)
*
* Licensed 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 cn.com.glsx.process.plugin.dbconn.oracle;
import cn.com.glsx.process.plugin.dbconn.AbstractConnect;
import cn.com.glsx.process.plugin.dbconn.McgConnect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.sql.DataSource;
public class OracleConnectImpl extends AbstractConnect implements McgConnect {
private static Logger logger = LoggerFactory.getLogger(OracleConnectImpl.class);
public OracleConnectImpl(DataSource dataSource) {
super(dataSource);
}
}
/*
* @Copyright (c) 2018 缪聪(mcg-helper@qq.com)
*
* Licensed 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 cn.com.glsx.process.plugin.dbconn.postgresql;
import cn.com.glsx.process.plugin.dbconn.AbstractConnect;
import cn.com.glsx.process.plugin.dbconn.McgConnect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.sql.DataSource;
public class PostgresqlConnectImpl extends AbstractConnect implements McgConnect {
private static Logger logger = LoggerFactory.getLogger(PostgresqlConnectImpl.class);
public PostgresqlConnectImpl(DataSource dataSource) {
super(dataSource);
}
}
/*
* @Copyright (c) 2018 缪聪(mcg-helper@qq.com)
*
* Licensed 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 cn.com.glsx.process.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.math.BigDecimal;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.Types;
import java.util.Date;
import java.util.Map;
import java.util.TreeMap;
public class JDBCTypesUtils {
private static Logger logger = LoggerFactory.getLogger(JDBCTypesUtils.class);
private static Map<String, Integer> jdbcTypes;
private static Map<Integer, String> jdbcTypeValues;
private static Map<Integer, Class<?>> jdbcJavaTypes;
static {
jdbcTypes = new TreeMap<String, Integer>();
jdbcTypeValues = new TreeMap<Integer, String>();
jdbcJavaTypes = new TreeMap<Integer, Class<?>>();
Field[] fields = Types.class.getFields();
try {
for (int i = 0, len = fields.length; i < len; ++i) {
if (Modifier.isStatic(fields[i].getModifiers())) {
String name = fields[i].getName();
Integer value = (Integer) fields[i].get(Types.class);
jdbcTypes.put(name, value);
jdbcTypeValues.put(value, name);
}
}
} catch (Exception e) {
logger.error(e.getMessage());
}
// 初始化jdbcJavaTypes:
jdbcJavaTypes.put(new Integer(Types.LONGNVARCHAR), String.class); // -16 字符串
jdbcJavaTypes.put(new Integer(Types.NCHAR), String.class); // -15 字符串
jdbcJavaTypes.put(new Integer(Types.NVARCHAR), String.class); // -9 字符串
jdbcJavaTypes.put(new Integer(Types.ROWID), String.class); // -8 字符串
jdbcJavaTypes.put(new Integer(Types.BIT), Boolean.class); // -7 布尔
jdbcJavaTypes.put(new Integer(Types.TINYINT), Byte.class); // -6 数字
jdbcJavaTypes.put(new Integer(Types.BIGINT), Long.class); // -5 数字
jdbcJavaTypes.put(new Integer(Types.LONGVARBINARY), Blob.class); // -4 二进制
jdbcJavaTypes.put(new Integer(Types.VARBINARY), Blob.class); // -3 二进制
jdbcJavaTypes.put(new Integer(Types.BINARY), Blob.class); // -2 二进制
jdbcJavaTypes.put(new Integer(Types.LONGVARCHAR), String.class); // -1 字符串
// jdbcJavaTypes.put(new Integer(Types.NULL), String.class); // 0 /
jdbcJavaTypes.put(new Integer(Types.CHAR), String.class); // 1 字符串
jdbcJavaTypes.put(new Integer(Types.NUMERIC), BigDecimal.class); // 2 数字
jdbcJavaTypes.put(new Integer(Types.DECIMAL), BigDecimal.class); // 3 数字
jdbcJavaTypes.put(new Integer(Types.INTEGER), Integer.class); // 4 数字
jdbcJavaTypes.put(new Integer(Types.SMALLINT), Short.class); // 5 数字
jdbcJavaTypes.put(new Integer(Types.FLOAT), Float.class); // 6 数字
jdbcJavaTypes.put(new Integer(Types.REAL), BigDecimal.class); // 7 数字
jdbcJavaTypes.put(new Integer(Types.DOUBLE), BigDecimal.class); // 8 数字
jdbcJavaTypes.put(new Integer(Types.VARCHAR), String.class); // 12 字符串
jdbcJavaTypes.put(new Integer(Types.BOOLEAN), Boolean.class); // 16 布尔
// jdbcJavaTypes.put(new Integer(Types.DATALINK), String.class); // 70 /
jdbcJavaTypes.put(new Integer(Types.DATE), Date.class); // 91 日期
jdbcJavaTypes.put(new Integer(Types.TIME), Date.class); // 92 日期
jdbcJavaTypes.put(new Integer(Types.TIMESTAMP), Date.class); // 93 日期
jdbcJavaTypes.put(new Integer(Types.OTHER), Object.class); // 1111 其他类型?
// jdbcJavaTypes.put(new Integer(Types.JAVA_OBJECT), Object.class); // 2000
// jdbcJavaTypes.put(new Integer(Types.DISTINCT), String.class); // 2001
// jdbcJavaTypes.put(new Integer(Types.STRUCT), String.class); // 2002
// jdbcJavaTypes.put(new Integer(Types.ARRAY), String.class); // 2003
jdbcJavaTypes.put(new Integer(Types.BLOB), Blob.class); // 2004 二进制
jdbcJavaTypes.put(new Integer(Types.CLOB), Clob.class); // 2005 大文本
// jdbcJavaTypes.put(new Integer(Types.REF), String.class); // 2006
// jdbcJavaTypes.put(new Integer(Types.SQLXML), String.class); // 2009
jdbcJavaTypes.put(new Integer(Types.NCLOB), Clob.class); // 2011 大文本
}
public static int getJdbcCode(String jdbcName) {
return jdbcTypes.get(jdbcName);
}
public static String getJdbcName(int jdbcCode) {
return jdbcTypeValues.get(jdbcCode);
}
public static Class<?> jdbcTypeToJavaType(int jdbcType) {
return jdbcJavaTypes.get(jdbcType);
}
public static boolean isJavaNumberType(int jdbcType) {
Class<?> type = jdbcJavaTypes.get(jdbcType);
return (type == null) ? false : (Number.class.isAssignableFrom(type)) ? true : false;
}
}
/*
* @Copyright (c) 2018 缪聪(mcg-helper@qq.com)
*
* Licensed 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 cn.com.glsx.process.util;
import com.google.common.base.CaseFormat;
import org.apache.commons.lang.StringUtils;
/**
*
* @ClassName: Tools
* @Description: TODO(字符操作、hibernate validator)
* @author: 缪聪(mcg-helper@qq.com)
* @date: 2018年3月9日 下午5:41:39
*
*/
public class Tools {
public static String genFlowInstanceId(String httpSessionId, String flowId) {
return httpSessionId + "##" + flowId;
}
/*
* 分割字符串".",截取最后一段(如导入类型:java.utils.Date)截取最后的Date
*/
public static String splitLast(String data) {
if(StringUtils.isEmpty(data)) {
return null;
}
String[] split = data.split("\\.");
return split[split.length-1];
}
/*
* 字符串转换驼峰变量名
*/
public static String convertFieldName(String name) {
return CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, name);
}
/*
* 字符串转换类名
*/
public static String convertClassName(String name) {
return CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, name);
}
public static boolean isEmpty(String s) {
return s == null || "".equals(s) || "null".equals(s);
}
public static String genWsshConnUniqueId(String httpSessionId, String webSocketSessionId) {
return String.format("%s#%s", httpSessionId, webSocketSessionId);
}
public static String genMcgWsConnUniqueId(String mcgWebScoketCode, String httpSessionId) {
return String.format("%s#%s", mcgWebScoketCode, httpSessionId);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.com.glsx.process.modules.mapper.SopDataSourceMapper">
<resultMap id="BaseResultMap" type="cn.com.glsx.process.modules.entity.SopDataSource">
<!--
WARNING - @mbg.generated
-->
<result column="id" jdbcType="INTEGER" property="id" />
<result column="db_type" jdbcType="VARCHAR" property="dbType" />
<result column="db_server" jdbcType="VARCHAR" property="dbServer" />
<result column="db_port" jdbcType="VARCHAR" property="dbPort" />
<result column="db_user_name" jdbcType="VARCHAR" property="dbUserName" />
<result column="db_pwd" jdbcType="VARCHAR" property="dbPwd" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="db_name" jdbcType="VARCHAR" property="dbName" />
</resultMap>
</mapper>
\ No newline at end of file \ No newline at end of file
package cn.com.glsx.cn.com.glsx; package cn.com.glsx.cn.com.glsx;
import cn.com.glsx.Application; import cn.com.glsx.Application;
import cn.com.glsx.process.modules.entity.SopDataSource;
import cn.com.glsx.process.modules.service.SopDataSourceService;
import cn.com.glsx.process.modules.service.UserDemoService; import cn.com.glsx.process.modules.service.UserDemoService;
import cn.com.glsx.process.plugin.dbconn.FlowDataAdapterImpl;
import cn.com.glsx.process.plugin.dbconn.McgBizAdapter;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
/** /**
* 测试spring cloud 服务调用方式 * 测试spring cloud 服务调用方式
*/ */
...@@ -18,10 +26,16 @@ public class TestApplication { ...@@ -18,10 +26,16 @@ public class TestApplication {
@Autowired @Autowired
private UserDemoService userDemoService; private UserDemoService userDemoService;
@Test @Autowired
public void test() { private SopDataSourceService sopDataSourceService;
@Test
public void test() throws SQLException {
SopDataSource byId = sopDataSourceService.getById(1);
McgBizAdapter mcgBizAdapter = new FlowDataAdapterImpl(byId);
List<Map<String, Object>> maps = mcgBizAdapter.tableQuery("SELECT * FROM sop_data_source");
System.out.println(maps);
} }
} }
\ No newline at end of file \ No newline at end of file
...@@ -31,7 +31,7 @@ ...@@ -31,7 +31,7 @@
<property name="enableSubPackages" value="true" /> <property name="enableSubPackages" value="true" />
</javaClientGenerator> </javaClientGenerator>
<!-- 可配置多个 --> <!-- 可配置多个 -->
<table tableName="sop_tpl_node" domainObjectName="SopTplNode" enableSelectByExample="false" enableCountByExample="false" enableDeleteByExample="false" enableUpdateByExample="false"> <table tableName="sop_data_source" domainObjectName="SopDataSource" enableSelectByExample="false" enableCountByExample="false" enableDeleteByExample="false" enableUpdateByExample="false">
<generatedKey column="id" sqlStatement="MySql" identity="true"/> <generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table> </table>
<!--<table tableName="sop_tpl_node" domainObjectName="SopTplNode" enableSelectByExample="false" enableCountByExample="false" enableDeleteByExample="false" enableUpdateByExample="false"> <!--<table tableName="sop_tpl_node" domainObjectName="SopTplNode" enableSelectByExample="false" enableCountByExample="false" enableDeleteByExample="false" enableUpdateByExample="false">
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!