新增CrispDbKit相关代码结构

This commit is contained in:
aoshiguchen
2022-11-03 21:06:34 +08:00
parent 2f841ad7a5
commit a212056a46
9 changed files with 480 additions and 2 deletions
@@ -0,0 +1,66 @@
/**
* Copyright (c) 2022 aoshiguchen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package fun.asgc.neutrino.core.db.crisp;
import javax.sql.DataSource;
/**
* @author: aoshiguchen
* @date: 2022/11/3
*/
public class CrispDbConfig {
/**
* 数据库名称
*/
private String name;
/**
* 数据源提供者
*/
private IDataSourceProvider dataSourceProvider;
/**
* 是否显示sql
*/
private Boolean showSql;
/**
* 是否开启调试模式
*/
private Boolean debugMode;
public CrispDbConfig(String name, IDataSourceProvider dataSourceProvider) {
this.name = name;
this.dataSourceProvider = dataSourceProvider;
this.showSql = Boolean.FALSE;
this.debugMode = Boolean.FALSE;
}
public CrispDbConfig(String name, DataSource dataSource) {
this(name, new DefaultDataSourceProvider(dataSource));
}
public CrispDbConfig(IDataSourceProvider dataSourceProvider) {
this("unknown", dataSourceProvider);
}
public CrispDbConfig(DataSource dataSource) {
this(new DefaultDataSourceProvider(dataSource));
}
}
@@ -0,0 +1,32 @@
/**
* Copyright (c) 2022 aoshiguchen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package fun.asgc.neutrino.core.db.crisp;
import java.sql.Connection;
/**
* @author: aoshiguchen
* @date: 2022/11/3
*/
public class CrispDbContext {
private final ThreadLocal<Connection> connectionHolder = new ThreadLocal<>();
}
@@ -0,0 +1,86 @@
/**
* Copyright (c) 2022 aoshiguchen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package fun.asgc.neutrino.core.db.crisp;
import javax.sql.DataSource;
import java.util.List;
/**
* @author: aoshiguchen
* @date: 2022/11/3
*/
class CrispDbExecutor implements CrispDbOperator {
/**
* 配置
*/
private CrispDbConfig config;
/**
* 上下文
*/
private CrispDbContext context;
public CrispDbExecutor(CrispDbConfig config) {
this.config = config;
this.context = new CrispDbContext();
}
public CrispDbExecutor(IDataSourceProvider dataSourceProvider) {
this(new CrispDbConfig(dataSourceProvider));
}
public CrispDbExecutor(String name, IDataSourceProvider dataSourceProvider) {
this(new CrispDbConfig(name, dataSourceProvider));
}
public CrispDbExecutor(DataSource dataSource) {
this(new CrispDbConfig(dataSource));
}
public CrispDbExecutor(String name, DataSource dataSource) {
this(new CrispDbConfig(name, dataSource));
}
@Override
public <T> List<T> queryList(Class<T> resultType, String sql, Object... params) {
return null;
}
@Override
public <T> T query(Class<T> resultType, String sql, Object... params) {
return null;
}
@Override
public CrispDbUpdateResult update(String sql, Object... params) {
return null;
}
@Override
public CrispDbUpdateResult insert(String sql, Object... params) {
return null;
}
@Override
public CrispDbUpdateResult delete(String sql, Object... params) {
return null;
}
}
@@ -0,0 +1,109 @@
/**
* Copyright (c) 2022 aoshiguchen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package fun.asgc.neutrino.core.db.crisp;
import fun.asgc.neutrino.core.util.LockUtil;
import java.util.HashMap;
import java.util.Map;
/**
* 清爽的数据库工具
* @author: aoshiguchen
* @date: 2022/11/3
*/
public class CrispDbKit {
/**
* 数据源缓存
*/
private static Map<String, CrispDbConfig> crispDbConfigMap = new HashMap<>();
/**
* 数据库操作实例
*/
private static Map<String, CrispDbExecutor> crispDbExecutorMap = new HashMap<>();
/**
* 默认数据库的key
*/
private static final String DEFAULT_KEY = "default";
/**
* 设置默认数据库配置
* @param config 数据库配置
*/
public static void setConfig(CrispDbConfig config) {
setConfig(DEFAULT_KEY, config);
}
/**
* 设置数据库配置
* @param key 键
* @param config 数据库配置
*/
public static void setConfig(String key, CrispDbConfig config) {
crispDbConfigMap.put(key, config);
}
/**
* 根据名称获取数据库配置
* @param key 键
* @return 数据库配置
*/
private static CrispDbConfig getConfig(String key) {
return crispDbConfigMap.get(key);
}
/**
* 获取默认数据库操作实例
* @return
*/
private static CrispDbExecutor getDbExecutor() {
return getDbExecutor(DEFAULT_KEY);
}
/**
* 获取数据库操作实例
* @param key 键
* @return 数据库操作实例
*/
private static CrispDbExecutor getDbExecutor(String key) {
return LockUtil.doubleCheckProcessForNoException(
() -> !crispDbExecutorMap.containsKey(key),
key,
() -> {
CrispDbConfig config = getConfig(key);
if (null != config) {
crispDbExecutorMap.put(key, new CrispDbExecutor(config));
}
},
() -> crispDbExecutorMap.get(crispDbExecutorMap)
);
}
/**
* 切换数据源
* @param name 数据源名称
* @return 数据库操作实例
*/
public static CrispDbExecutor use(String name) {
return getDbExecutor(name);
}
}
@@ -0,0 +1,74 @@
/**
* Copyright (c) 2022 aoshiguchen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package fun.asgc.neutrino.core.db.crisp;
import java.util.List;
/**
* @author: aoshiguchen
* @date: 2022/11/3
*/
public interface CrispDbOperator {
/**
* 查询数据列表
* @param resultType 结果类型
* @param sql sql
* @param params 参数列表
* @return 查询结果列表
* @param <T>
*/
<T> List<T> queryList(Class<T> resultType, String sql, Object... params);
/**
* 查询单条记录
* @param resultType 结果类型
* @param sql sql
* @param params 参数列表
* @return 查询结果
* @param <T>
*/
<T> T query(Class<T> resultType, String sql, Object... params);
/**
* 更新
* @param sql sql
* @param params 参数列表
* @return 更新结果
*/
CrispDbUpdateResult update(String sql, Object... params);
/**
* 新增
* @param sql sql
* @param params 参数列表
* @return 新增结果
*/
CrispDbUpdateResult insert(String sql, Object... params);
/**
* 删除
* @param sql sql
* @param params 参数列表
* @return 删除结果
*/
CrispDbUpdateResult delete(String sql, Object... params);
}
@@ -0,0 +1,30 @@
/**
* Copyright (c) 2022 aoshiguchen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package fun.asgc.neutrino.core.db.crisp;
/**
* @author: aoshiguchen
* @date: 2022/11/3
*/
public class CrispDbUpdateResult {
}
@@ -0,0 +1,44 @@
/**
* Copyright (c) 2022 aoshiguchen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package fun.asgc.neutrino.core.db.crisp;
import javax.sql.DataSource;
/**
* @author: aoshiguchen
* @date: 2022/11/3
*/
public class DefaultDataSourceProvider implements IDataSourceProvider {
/**
* 数据源
*/
private DataSource dataSource;
public DefaultDataSourceProvider(DataSource dataSource) {
this.dataSource = dataSource;
}
@Override
public DataSource getDataSource() {
return dataSource;
}
}
@@ -0,0 +1,37 @@
/**
* Copyright (c) 2022 aoshiguchen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package fun.asgc.neutrino.core.db.crisp;
import javax.sql.DataSource;
/**
* 数据源提供商
* @author: aoshiguchen
* @date: 2022/11/3
*/
public interface IDataSourceProvider {
/**
* 获取数据源
* @return
*/
DataSource getDataSource();
}
@@ -56,7 +56,7 @@ public class FlowReportForMonthJob implements IJobHandler {
@Autowired
private FlowReportDayMapper flowReportDayMapper;
@Autowired
private FlowReportMonthMapper flowReportHourDOList;
private FlowReportMonthMapper flowReportMonthMapper;
@Override
public void execute(String param) throws Exception {
@@ -96,7 +96,7 @@ public class FlowReportForMonthJob implements IJobHandler {
}
for (FlowReportMonthDO item : map.values()) {
flowReportHourDOList.add(item);
flowReportMonthMapper.add(item);
}
}