Commit 820d4d73 authored by yanzg's avatar yanzg

修改实例化关系

parent 3ef1b5b3
...@@ -296,6 +296,10 @@ public class DaoConst { ...@@ -296,6 +296,10 @@ public class DaoConst {
* 根据字段添加统计 * 根据字段添加统计
*/ */
public static final int FIELD_ADD_GROUP = 8; public static final int FIELD_ADD_GROUP = 8;
/**
* 根据字段替换统计
*/
public static final int FIELD_REPLACE_GROUP = 9;
/** /**
* SQL语句类型-普通语句 * SQL语句类型-普通语句
......
...@@ -4,6 +4,7 @@ package com.yanzuoguang.dao.impl; ...@@ -4,6 +4,7 @@ package com.yanzuoguang.dao.impl;
* 累加接口 * 累加接口
* *
* @param <T> * @param <T>
* @author 颜佐光
*/ */
public interface GroupAdd<T extends GroupAdd> { public interface GroupAdd<T extends GroupAdd> {
/*** /***
......
...@@ -119,6 +119,15 @@ public class TableSqlCache { ...@@ -119,6 +119,15 @@ public class TableSqlCache {
this.table.addGroupSql(this, tableWhereField, addField); this.table.addGroupSql(this, tableWhereField, addField);
} }
/**
* 生成GroupSQL语句
*
* @param tableWhereField
* @param addField
*/
public void addGroup(TableFieldString tableWhereField, TableFieldString replaceField, TableFieldString addField) {
this.table.addGroupSql(this, tableWhereField, replaceField, addField);
}
/** /**
* 生成GroupSQL语句 * 生成GroupSQL语句
......
...@@ -258,9 +258,11 @@ public class TableStruct { ...@@ -258,9 +258,11 @@ public class TableStruct {
* @return 获取统计纬度列 * @return 获取统计纬度列
*/ */
public List<TableFieldVo> getGroupLatitudeFields() { public List<TableFieldVo> getGroupLatitudeFields() {
List<TableFieldVo> commonField = getFieldActionList(DaoConst.FIELD_COMMON, return getFieldActionList(DaoConst.FIELD_COMMON,
DaoConst.FIELD_ADD_GROUP, DaoConst.FIELD_MD5); DaoConst.FIELD_ADD_GROUP,
return commonField; DaoConst.FIELD_REPLACE_GROUP,
DaoConst.FIELD_MD5
);
} }
/** /**
...@@ -424,8 +426,9 @@ public class TableStruct { ...@@ -424,8 +426,9 @@ public class TableStruct {
*/ */
private void initAddGroup(TableSqlCache table) { private void initAddGroup(TableSqlCache table) {
List<TableFieldVo> addGroupField = getFieldActionList(DaoConst.FIELD_ADD_GROUP); List<TableFieldVo> addGroupField = getFieldActionList(DaoConst.FIELD_ADD_GROUP);
List<TableFieldVo> replaceGroupField = getFieldActionList(DaoConst.FIELD_REPLACE_GROUP);
List<TableFieldVo> commonField = this.getGroupLatitudeFields(); List<TableFieldVo> commonField = this.getGroupLatitudeFields();
this.addGroupSql(table, commonField, addGroupField); this.addGroupSql(table, commonField, replaceGroupField, addGroupField);
} }
/** /**
...@@ -724,8 +727,10 @@ public class TableStruct { ...@@ -724,8 +727,10 @@ public class TableStruct {
*/ */
private List<TableFieldVo> getFieldString(TableFieldString fieldFrom) { private List<TableFieldVo> getFieldString(TableFieldString fieldFrom) {
List<TableFieldVo> list = new ArrayList<>(); List<TableFieldVo> list = new ArrayList<>();
for (String fieldName : fieldFrom.getFields()) { if (fieldFrom != null) {
list.add(this.getField(fieldName)); for (String fieldName : fieldFrom.getFields()) {
list.add(this.getField(fieldName));
}
} }
return list; return list;
} }
...@@ -759,10 +764,10 @@ public class TableStruct { ...@@ -759,10 +764,10 @@ public class TableStruct {
* *
* @param sqlTableData 需要生成的实体 * @param sqlTableData 需要生成的实体
* @param whereFields WHERE字段 * @param whereFields WHERE字段
* @param updateFields 需要增加的值的字段 * @param addFields 需要增加的值的字段
*/ */
public void addGroupSql(TableSqlCache sqlTableData, TableFieldString whereFields, TableFieldString updateFields) { public void addGroupSql(TableSqlCache sqlTableData, TableFieldString whereFields, TableFieldString addFields) {
addGroupSql(sqlTableData, getFieldString(whereFields), getFieldString(updateFields)); addGroupSql(sqlTableData, whereFields, null, addFields);
} }
/** /**
...@@ -770,16 +775,37 @@ public class TableStruct { ...@@ -770,16 +775,37 @@ public class TableStruct {
* *
* @param sqlTableData 需要生成的实体 * @param sqlTableData 需要生成的实体
* @param whereFields WHERE字段 * @param whereFields WHERE字段
* @param updateFields 需要增加的值的字段 * @param addFields 需要增加的值的字段
*/ */
private void addGroupSql(TableSqlCache sqlTableData, List<TableFieldVo> whereFields, List<TableFieldVo> updateFields) { public void addGroupSql(TableSqlCache sqlTableData, TableFieldString whereFields, TableFieldString replaceFields, TableFieldString addFields) {
addGroupSql(sqlTableData, getFieldString(whereFields), getFieldString(replaceFields), getFieldString(addFields));
}
/**
* 生成统计的SQL语句
*
* @param sqlTableData 需要生成的实体
* @param replaceFields WHERE字段
* @param addFields 需要增加的值的字段
*/
private void addGroupSql(TableSqlCache sqlTableData, List<TableFieldVo> whereFields, List<TableFieldVo> replaceFields, List<TableFieldVo> addFields) {
List<TableFieldVo> prmaryKey = this.getFieldActionList(DaoConst.FIELD_PRIMARY); List<TableFieldVo> prmaryKey = this.getFieldActionList(DaoConst.FIELD_PRIMARY);
// 生成统计加载SQL语句
SqlData sqlLoad = this.releaseSql(DaoConst.SQL_TYPE_ADD_GROUP, DaoConst.GROUP_QUERY, DaoConst.SQL_LOAD, SqlData sqlLoad = this.releaseSql(DaoConst.SQL_TYPE_ADD_GROUP, DaoConst.GROUP_QUERY, DaoConst.SQL_LOAD,
StringHelper.EMPTY, new ArrayList<>(), whereFields); StringHelper.EMPTY, new ArrayList<>(), whereFields);
sqlLoad.addParaConst(DaoConst.CODE_FIELD_DEFAULT_NAME, DaoConst.CODE_FIELD, DaoConst.CODE_FIELD_DEFAULT); sqlLoad.addParaConst(DaoConst.CODE_FIELD_DEFAULT_NAME, DaoConst.CODE_FIELD, DaoConst.CODE_FIELD_DEFAULT);
// 生成统计累加SQL语句
SqlData sqlGroupAdd = this.releaseSql(DaoConst.SQL_TYPE_ADD_GROUP, DaoConst.GROUP_ADD, DaoConst.SQL_UPDATE, SqlData sqlGroupAdd = this.releaseSql(DaoConst.SQL_TYPE_ADD_GROUP, DaoConst.GROUP_ADD, DaoConst.SQL_UPDATE,
DaoConst.CODE_GROUP_ADD, updateFields, prmaryKey); DaoConst.CODE_GROUP_ADD, addFields, prmaryKey);
if (updateFields.isEmpty()) { // 生成覆盖值
if (replaceFields != null) {
for (TableFieldVo field : replaceFields) {
sqlGroupAdd.addPara(field.inputName, DaoConst.CODE_FIELD, String.format("a.%s=?", field.name));
}
}
// 当没有字段时,直接修改主键
if (addFields.isEmpty()) {
sqlGroupAdd.addCode(DaoConst.CODE_FIELD, String.format("%s=%s", prmaryKey.get(0).inputName, prmaryKey.get(0).inputName)); sqlGroupAdd.addCode(DaoConst.CODE_FIELD, String.format("%s=%s", prmaryKey.get(0).inputName, prmaryKey.get(0).inputName));
} }
sqlTableData.add(sqlLoad); sqlTableData.add(sqlLoad);
...@@ -856,8 +882,8 @@ public class TableStruct { ...@@ -856,8 +882,8 @@ public class TableStruct {
tos.add(from); tos.add(from);
mapFrom.put(key, from); mapFrom.put(key, from);
} else { } else {
T histor = mapFrom.get(key); T history = mapFrom.get(key);
histor.add(from); history.add(from);
} }
} }
......
import com.yanzuoguang.dao.DaoConst;
import com.yanzuoguang.dao.impl.SqlData;
import com.yanzuoguang.dao.impl.TableSqlCache;
import com.yanzuoguang.dao.impl.TableStruct;
import com.yanzuoguang.db.impl.DbPrintSql; import com.yanzuoguang.db.impl.DbPrintSql;
import com.yanzuoguang.util.helper.JsonHelper;
import org.junit.Test; import org.junit.Test;
public class TestDbPrintSql { public class TestDbPrintSql {
@Test @Test
public void test(){ public void test() {
DbPrintSql printSql = new DbPrintSql(); DbPrintSql printSql = new DbPrintSql();
String stringSql = printSql.getStringSql("select * from where id = ? AND name = ? and removeFlag = 0 ", "1"); String stringSql = printSql.getStringSql("select * from where id = ? AND name = ? and removeFlag = 0 ", "1");
System.out.println(stringSql); System.out.println(stringSql);
} }
@Test
public void testTableStruct() {
TableSqlCache cache = new TableSqlCache();
TableStruct table = new TableStruct("test", TestTableGroupVo.class);
table.init(cache);
SqlData sqlData = cache.getNameCache().get(DaoConst.GROUP_ADD);
System.out.println(JsonHelper.serialize(sqlData, true));
}
} }
import com.yanzuoguang.dao.DaoConst;
import com.yanzuoguang.dao.TableAnnotation;
@TableAnnotation("db_time")
public class TestTableGroupVo {
@TableAnnotation("id")
private String id;
@TableAnnotation("createDate")
private String createDate;
@TableAnnotation(value = "total", type = DaoConst.FIELD_REPLACE_GROUP)
private int total;
@TableAnnotation(value = "has", type = DaoConst.FIELD_ADD_GROUP)
private int has;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getCreateDate() {
return createDate;
}
public void setCreateDate(String createDate) {
this.createDate = createDate;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public int getHas() {
return has;
}
public void setHas(int has) {
this.has = has;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment