use of com.baomidou.mybatisplus.core.metadata.TableInfo in project mybatis-plus-samples by baomidou.
the class MySqlInjector method getMethodList.
@Override
public List<AbstractMethod> getMethodList(Class<?> mapperClass, TableInfo tableInfo) {
List<AbstractMethod> methodList = super.getMethodList(mapperClass, tableInfo);
// 增加自定义方法
methodList.add(new DeleteAll());
methodList.add(new FindOne());
/**
* 以下 3 个为内置选装件
* 头 2 个支持字段筛选函数
*/
// 例: 不要指定了 update 填充的字段
methodList.add(new InsertBatchSomeColumn(i -> i.getFieldFill() != FieldFill.UPDATE));
methodList.add(new AlwaysUpdateSomeColumnById());
methodList.add(new LogicDeleteByIdWithFill());
return methodList;
}
use of com.baomidou.mybatisplus.core.metadata.TableInfo in project chao-cloud by chaojunzi.
the class DynamicDataSourceInterceptor method getDsName.
private String getDsName(IService<?> service) {
Class<?> entityClass = service.getEntityClass();
TableInfo tableInfo = TableInfoHelper.getTableInfo(entityClass);
String tableName = tableInfo.getTableName();
if (tableDataSourceMap.containsKey(tableName)) {
return tableDataSourceMap.get(tableName);
}
return properties.getPrimary();
}
use of com.baomidou.mybatisplus.core.metadata.TableInfo in project chao-cloud by chaojunzi.
the class MybatisUtil method buildSql.
/**
* 根据wrapper生成sql 如:a=1 and b=2
*
* @param <T> 实体类型
* @param wrapper 条件
* @param beanClass bean类型
* @return sql条件
*/
static <T> String buildSql(LambdaQueryWrapper<T> wrapper, Class<T> beanClass) {
// 判断bean是否加载
TableInfo info = FunctionUtil.buildTableInfo(beanClass);
Assert.notNull(info, "无效的数据实体 beanClass={}", beanClass.getName());
String targetSql = wrapper.getTargetSql();
if (StrUtil.isBlank(targetSql)) {
return null;
}
int count = StrUtil.count(targetSql, MybatisConstant.ASK);
Map<String, Object> valuePairs = wrapper.getParamNameValuePairs();
Object[] vals = //
IntStream.range(1, count + 1).mapToObj(i -> {
Object obj = valuePairs.get("MPGENVAL" + i);
if (obj instanceof Date) {
obj = DateUtil.formatDateTime((Date) obj);
}
return obj;
}).toArray();
// 去掉首尾括号
return StrUtil.format(StrUtil.replace(targetSql, MybatisConstant.ASK, "'{}'"), vals);
}
use of com.baomidou.mybatisplus.core.metadata.TableInfo in project chao-cloud by chaojunzi.
the class MybatisUtil method parseQueryCondition.
/**
* 解析 {@link QueryCondition}
*
* @param <T> 数据实体
* @param param 参数
* @param beanClass 实体类型
* @param wrapper 条件构造器
* @return {@link AbstractWrapper}
*/
static <T> AbstractWrapper<T, ?, ?> parseQueryCondition(Object param, Class<T> beanClass, AbstractWrapper<T, ?, ?> wrapper) {
if (param == null) {
return wrapper;
}
Field[] fields = ReflectUtil.getFields(param.getClass());
if (ArrayUtil.isEmpty(fields)) {
return wrapper;
}
// 获取字段名
TableInfo info = TableInfoHelper.getTableInfo(beanClass);
Assert.notNull(info, "无效的数据实体 beanClass={}", beanClass.getName());
Map<String, String> propColumnMap = info.getFieldList().stream().collect(Collectors.toMap(TableFieldInfo::getProperty, TableFieldInfo::getColumn));
for (Field field : fields) {
// 判断model中是否存在 @QueryCondition 注解
QueryCondition q = field.getAnnotation(QueryCondition.class);
if (q == null) {
continue;
}
SqlTemplate template = q.value();
// 实体字段名字
Class<? extends FuncExps> funcClass = q.funcClass();
String fieldName = field.getName();
SFunction<?, ?> func = null;
if (funcClass != FuncExps.class) {
// 调用sfunction
func = getSFunction(funcClass);
String name = FunctionUtil.getFieldName(func);
if (StrUtil.isNotBlank(name)) {
fieldName = name;
}
}
//
String column = propColumnMap.get(fieldName);
//
boolean sqlSegment = template == SqlTemplate.SEGMENT;
boolean isSql = template == SqlTemplate.APPLY_SQL;
if (!sqlSegment && !isSql) {
Assert.notBlank(column, "数据库字段不匹配;paramName={}", field.getName());
}
// 获取属性值
Object v = ReflectUtil.getFieldValue(param, field);
if (v == null) {
continue;
}
// 处理自定义sql
if (isSql) {
parseApplySql(wrapper, v);
continue;
}
// 排序单独处理
if (template == SqlTemplate.ORDER_BY && func != null) {
Assert.state(v instanceof Boolean, "排序请使用 Boolean 类型(B大写)");
// 反射方法
Method method = ReflectUtil.getMethodByName(wrapper.getClass(), template.getTemplate());
ReflectUtil.invoke(wrapper, method, true, v, func);
continue;
}
// 处理in查询-占位符
if (template == SqlTemplate.IN) {
Assert.state(v instanceof Collection, "in 查询 请传入 集合");
// 反射方法
Method method = ReflectUtil.getMethodByName(wrapper.getClass(), template.getTemplate());
ReflectUtil.invoke(wrapper, method, true, func, v);
continue;
}
//
if (sqlSegment) {
// 必须是String类型
if (v instanceof List) {
List<String> strList = (List<String>) v;
strList.forEach(wrapper::apply);
}
if (v instanceof String) {
wrapper.apply(v.toString());
}
} else {
wrapper.apply(template.getApplySql(column), template.formatValue(v));
}
}
return wrapper;
}
use of com.baomidou.mybatisplus.core.metadata.TableInfo in project chao-cloud by chaojunzi.
the class DateTableNameHandler method parseDate.
private List<Date> parseDate(SqlCommandType type, String table, Object parameter) {
String column = rule.getColumn();
List<Date> dateList = CollUtil.newArrayList();
if (type == SqlCommandType.INSERT) {
// insert默认解析对象
TableInfo tableInfo = TableInfoHelper.getTableInfo(table);
if (tableInfo == null) {
return dateList;
}
if (tableInfo.getEntityType() == ClassUtil.getClass(parameter)) {
List<TableFieldInfo> fieldList = tableInfo.getFieldList();
TableFieldInfo field = CollUtil.findOne(fieldList, f -> StrUtil.equals(f.getColumn(), column));
if (field == null) {
log.warn("[{}]: 分片字段无效 {}", table, column);
return dateList;
}
Object v = BeanUtil.getFieldValue(parameter, field.getProperty());
dateList.add((Date) v);
return dateList;
}
}
// 参数解析
if (parameter instanceof ParamMap) {
ParamMap paramMap = (ParamMap) parameter;
Collection values = paramMap.values();
for (Object val : values) {
if (val instanceof AbstractWrapper) {
AbstractWrapper wrapper = (AbstractWrapper) val;
MergeSegments segments = wrapper.getExpression();
if (segments == null || segments.getNormal() == null) {
continue;
}
NormalSegmentList segmentList = segments.getNormal();
for (int i = 0; i < segmentList.size(); i++) {
String dbColumn = segmentList.get(i).getSqlSegment();
if (StrUtil.equalsIgnoreCase(dbColumn, column)) {
// 匹配到参数
i++;
ISqlSegment keyword = CollUtil.get(segmentList, i);
if (CollUtil.contains(supportSqlKeywords, keyword)) {
// 获取到值索引
i++;
ISqlSegment keySegment = CollUtil.get(segmentList, i);
// 索引越界
if (keySegment == null) {
continue;
}
// 构造dateList
dateList.addAll(getDateByParam(parameter, keySegment));
// between ? and ? -> (跳2格)
i = i + 2;
if (keyword == SqlKeyword.BETWEEN) {
keySegment = CollUtil.get(segmentList, i);
dateList.addAll(getDateByParam(parameter, keySegment));
}
}
}
}
break;
}
}
}
return dateList;
}
Aggregations