服务端去除JdbcTemplate相关代码
This commit is contained in:
-6
@@ -5,7 +5,6 @@ import com.baomidou.mybatisplus.core.MybatisConfiguration;
|
||||
import com.baomidou.mybatisplus.core.MybatisSqlSessionFactoryBuilder;
|
||||
import com.baomidou.mybatisplus.core.config.GlobalConfig;
|
||||
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
|
||||
import fun.asgc.neutrino.core.db.template.JdbcTemplate;
|
||||
import fun.asgc.neutrino.proxy.server.constant.DbTypeEnum;
|
||||
import org.apache.ibatis.solon.annotation.Db;
|
||||
import org.noear.solon.annotation.Bean;
|
||||
@@ -48,11 +47,6 @@ public class DbConfiguration {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public JdbcTemplate jdbcTemplate(@Inject("db") DataSource dataSource) {
|
||||
return new JdbcTemplate(dataSource);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public void db1_ext(@Db("db") GlobalConfig globalConfig) {
|
||||
MetaObjectHandler metaObjectHandler = new MetaObjectHandlerImpl();
|
||||
|
||||
-49
@@ -1,49 +0,0 @@
|
||||
package fun.asgc.neutrino.proxy.server.base.db;
|
||||
|
||||
import org.noear.solon.core.AopContext;
|
||||
import org.noear.solon.core.Plugin;
|
||||
|
||||
/**
|
||||
* @author: aoshiguchen
|
||||
* @date: 2023/3/9
|
||||
*/
|
||||
public class MapperPlugin implements Plugin {
|
||||
@Override
|
||||
public void start(AopContext context) throws Throwable {
|
||||
// Aop.intercept(ClientConnectRecordMapper.class, SqlMapperInterceptor.class);
|
||||
// Aop.intercept(DataCleanMapper.class, SqlMapperInterceptor.class);
|
||||
// Aop.intercept(FlowReportDayMapper.class, SqlMapperInterceptor.class);
|
||||
// Aop.intercept(FlowReportHourMapper.class, SqlMapperInterceptor.class);
|
||||
// Aop.intercept(FlowReportMinuteMapper.class, SqlMapperInterceptor.class);
|
||||
// Aop.intercept(FlowReportMonthMapper.class, SqlMapperInterceptor.class);
|
||||
// Aop.intercept(JobInfoMapper.class, SqlMapperInterceptor.class);
|
||||
// Aop.intercept(JobLogMapper.class, SqlMapperInterceptor.class);
|
||||
// Aop.intercept(LicenseMapper.class, SqlMapperInterceptor.class);
|
||||
// Aop.intercept(PortMappingMapper.class, SqlMapperInterceptor.class);
|
||||
// Aop.intercept(PortPoolMapper.class, SqlMapperInterceptor.class);
|
||||
// Aop.intercept(UserConnectRecordMapper.class, SqlMapperInterceptor.class);
|
||||
// Aop.intercept(UserLoginRecordMapper.class, SqlMapperInterceptor.class);
|
||||
// Aop.intercept(UserMapper.class, SqlMapperInterceptor.class);
|
||||
// Aop.intercept(UserReportMapper.class, SqlMapperInterceptor.class);
|
||||
// Aop.intercept(UserTokenMapper.class, SqlMapperInterceptor.class);
|
||||
//
|
||||
// context.wrapAndPut(ClientConnectRecordMapper.class, Aop.get(ClientConnectRecordMapper.class));
|
||||
// context.wrapAndPut(DataCleanMapper.class, Aop.get(DataCleanMapper.class));
|
||||
// context.wrapAndPut(FlowReportDayMapper.class, Aop.get(FlowReportDayMapper.class));
|
||||
// context.wrapAndPut(FlowReportHourMapper.class, Aop.get(FlowReportHourMapper.class));
|
||||
// context.wrapAndPut(FlowReportMinuteMapper.class, Aop.get(FlowReportMinuteMapper.class));
|
||||
// context.wrapAndPut(FlowReportMonthMapper.class, Aop.get(FlowReportMonthMapper.class));
|
||||
// context.wrapAndPut(JobInfoMapper.class, Aop.get(JobInfoMapper.class));
|
||||
// context.wrapAndPut(JobLogMapper.class, Aop.get(JobLogMapper.class));
|
||||
// context.wrapAndPut(LicenseMapper.class, Aop.get(LicenseMapper.class));
|
||||
// context.wrapAndPut(PortMappingMapper.class, Aop.get(PortMappingMapper.class));
|
||||
// context.wrapAndPut(PortPoolMapper.class, Aop.get(PortPoolMapper.class));
|
||||
// context.wrapAndPut(UserConnectRecordMapper.class, Aop.get(UserConnectRecordMapper.class));
|
||||
// context.wrapAndPut(UserLoginRecordMapper.class, Aop.get(UserLoginRecordMapper.class));
|
||||
// context.wrapAndPut(UserMapper.class, Aop.get(UserMapper.class));
|
||||
// context.wrapAndPut(UserReportMapper.class, Aop.get(UserReportMapper.class));
|
||||
// context.wrapAndPut(UserTokenMapper.class, Aop.get(UserTokenMapper.class));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
-140
@@ -1,140 +0,0 @@
|
||||
/**
|
||||
* 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.proxy.server.base.db;
|
||||
|
||||
import fun.asgc.neutrino.core.annotation.Component;
|
||||
import fun.asgc.neutrino.core.annotation.NonIntercept;
|
||||
import fun.asgc.neutrino.core.annotation.Param;
|
||||
import fun.asgc.neutrino.core.aop.Invocation;
|
||||
import fun.asgc.neutrino.core.aop.interceptor.Interceptor;
|
||||
import fun.asgc.neutrino.core.db.mapper.SqlParser;
|
||||
import fun.asgc.neutrino.core.db.page.PageInfo;
|
||||
import fun.asgc.neutrino.core.db.template.JdbcTemplate;
|
||||
import fun.asgc.neutrino.core.util.ArrayUtil;
|
||||
import fun.asgc.neutrino.core.util.Assert;
|
||||
import fun.asgc.neutrino.core.util.StringUtil;
|
||||
import fun.asgc.neutrino.core.util.TypeUtil;
|
||||
import org.noear.solon.Solon;
|
||||
|
||||
import java.lang.reflect.Parameter;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* sqlmapper拦截器
|
||||
* @author: aoshiguchen
|
||||
* @date: 2022/6/28
|
||||
*/
|
||||
@NonIntercept
|
||||
@Component
|
||||
public class SqlMapperInterceptor implements Interceptor {
|
||||
@Override
|
||||
public void intercept(Invocation inv) throws Exception {
|
||||
JdbcTemplate jdbcTemplate = Solon.context().getBean(JdbcTemplate.class);
|
||||
Assert.notNull(jdbcTemplate, "JdbcTemplate未注入,调用失败!");
|
||||
SqlParser sqlParser = SqlParser.getInstance(inv.getTargetMethod());
|
||||
if (null == sqlParser) {
|
||||
return;
|
||||
}
|
||||
|
||||
String sql = sqlParser.getSql();
|
||||
Class<?> resultType = sqlParser.getResultType();
|
||||
Class<?> resultComponentType = sqlParser.getResultComponentType();
|
||||
Object res = null;
|
||||
if (sqlParser.isSelect()) {
|
||||
if (sqlParser.isReturnCollection()) {
|
||||
Object params = getParams(inv);
|
||||
if (null == params || params.getClass().isArray()) {
|
||||
res = jdbcTemplate.queryForList(resultComponentType, sql, inv.getArgs());
|
||||
} else {
|
||||
res = jdbcTemplate.queryForListByMap(resultComponentType, sql, (Map)params);
|
||||
}
|
||||
} else {
|
||||
if (PageInfo.class.isAssignableFrom(inv.getTargetMethod().getParameters()[0].getType())) {
|
||||
// 分页查询 TODO 此处暂时临时处理,假设后面的参数是一个DO对象
|
||||
PageInfo pageInfo = (PageInfo) inv.getArgs()[0];
|
||||
int offset = (pageInfo.getCurrent() - 1) * pageInfo.getSize();
|
||||
long total = jdbcTemplate.queryForLongByModel(String.format("select count(1) from (%s) T", sql), inv.getArgs()[1]);
|
||||
List resultList = jdbcTemplate.queryForListByModel(resultComponentType, String.format("%s limit %s,%s", sql, offset, pageInfo.getSize()), inv.getArgs()[1]);
|
||||
pageInfo.setTotal(total);
|
||||
pageInfo.setRecords(resultList);
|
||||
} else {
|
||||
Object params = getParams(inv);
|
||||
if (null == params || params.getClass().isArray()) {
|
||||
res = jdbcTemplate.query(resultType, sql, inv.getArgs());
|
||||
} else {
|
||||
res = jdbcTemplate.queryByMap(resultType, sql, (Map)params);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (sqlParser.isInsert() || sqlParser.isDelete() || sqlParser.isUpdate()) {
|
||||
int argsCount = ArrayUtil.isEmpty(inv.getArgs()) ? 0 : inv.getArgs().length;
|
||||
if (argsCount == 1 && inv.getArgs()[0] instanceof Map) {
|
||||
res = jdbcTemplate.updateByMap(sql, (Map)inv.getArgs()[0]);
|
||||
// 临时解决只有一个参数,且参数类型不是实体类型时,参数未起作用的情况
|
||||
} else if (argsCount == 1 && !TypeUtil.isNormalBasicType(inv.getArgs()[0].getClass()) && !(inv.getArgs()[0] instanceof Date)) {
|
||||
res = jdbcTemplate.updateByModel(sql, inv.getArgs()[0]);
|
||||
} else {
|
||||
Object params = getParams(inv);
|
||||
if (null == params) {
|
||||
res = jdbcTemplate.update(sql);
|
||||
} else if (params.getClass().isArray()){
|
||||
res = jdbcTemplate.update(sql, (Object[])params);
|
||||
} else if (params instanceof Map) {
|
||||
res = jdbcTemplate.updateByMap(sql, (Map)params);
|
||||
}
|
||||
}
|
||||
}
|
||||
inv.setReturnValue(TypeUtil.conversion(res, resultType));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取请求参数
|
||||
* @param inv
|
||||
* @return
|
||||
*/
|
||||
private Object getParams(Invocation inv) {
|
||||
if (inv.getTargetMethod().getParameterCount() == 0) {
|
||||
return null;
|
||||
}
|
||||
if (!inv.getTargetMethod().getParameters()[0].isAnnotationPresent(Param.class)) {
|
||||
return inv.getArgs();
|
||||
}
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
for (int i = 0; i < inv.getTargetMethod().getParameterCount(); i++) {
|
||||
Parameter parameter = inv.getTargetMethod().getParameters()[i];
|
||||
Param param = parameter.getAnnotation(Param.class);
|
||||
if (null == param) {
|
||||
continue;
|
||||
}
|
||||
String key = param.value();
|
||||
Object val = inv.getArgs()[i];
|
||||
if (!StringUtil.isEmpty(key)) {
|
||||
params.put(key, val);
|
||||
}
|
||||
|
||||
}
|
||||
return params;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user