Commit 07b32ba1 authored by yanzg's avatar yanzg

接口文档的支持

parent fd7eaa48
......@@ -24,6 +24,14 @@ public interface BaseDao {
*/
String create(Object model);
/**
* 主键存在则更新,否则替换
*
* @param model
* @return
*/
String replace(Object model);
/**
* 修改数据
*
......@@ -133,7 +141,7 @@ public interface BaseDao {
* @param model 需要创建的数据
* @return 创建的主键编号
*/
List<String> createList(List model);
List<String> createList(Collection model);
/**
* 创建数据,当不传入了主键时,则会自动生成主键,传入时不会生成。
......@@ -143,13 +151,29 @@ public interface BaseDao {
*/
List<String> createArray(Object... model);
/**
* 创建数据,当不传入了主键时,则会自动生成主键,传入时不会生成。
*
* @param model 需要创建的数据
* @return 创建的主键编号
*/
List<String> replaceList(Collection model);
/**
* 创建数据,当不传入了主键时,则会自动生成主键,传入时不会生成。
*
* @param model 需要创建的数据
* @return 创建的主键编号
*/
List<String> replaceArray(Object... model);
/**
* 保存数据,有主键时修改,无主键时创建
*
* @param model 需要保存的数据
* @return 保存的主键编号
*/
List<String> saveList(List model);
List<String> saveList(Collection model);
/**
* 保存数据,有主键时修改,无主键时创建
......@@ -166,7 +190,7 @@ public interface BaseDao {
* @param model 需要删除的数据,可以是主键字符串(Int),或者是包含主键的实体,或者是包含其他非主键的实体完全匹配.
* @return 删除的记录数量
*/
int removeList(List model);
int removeList(Collection model);
/**
* 删除数据,可以用于父子表删除,如通过订单删除游客信息 visitorDao.remove({orderId:1});
......
......@@ -22,6 +22,10 @@ public class DaoConst {
* 加载操作
*/
public static final int OPERATOR_TYPE_LOAD = 3;
/**
* 主键存在则修改,否则创建
*/
public static final String REPLACE = "replace";
/**
* 创建
*/
......@@ -75,6 +79,10 @@ public class DaoConst {
*/
public static final int CODE_UNIT = 2;
/**
* 插入SQL语句模板
*/
public static final String SQL_REPLACE = "REPLACE INTO {TABLE}({FIELD}) VALUES({VALUES})";
/**
* 插入SQL语句模板
*/
......
......@@ -101,14 +101,7 @@ public abstract class BaseDaoImpl extends BaseDaoSql implements BaseDao {
return key;
}
/**
* 创建数据,当不传入了主键时,则会自动生成主键,传入时不会生成。
*
* @param model 需要创建的数据
* @return 创建的主键编号, 创建成功,返回主键,否则为空
*/
@Override
public String create(Object model) {
protected String createReplace(String sqlName, Object model) {
// 判断主键是字符串和需要生成主键
boolean isKeyString = this.table.getTable().getKeyType() == String.class;
String keyString = this.getKeyString(model);
......@@ -123,7 +116,7 @@ public abstract class BaseDaoImpl extends BaseDaoSql implements BaseDao {
this.check(DaoConst.OPERATOR_TYPE_CREATE, keyString, model);
// 执行创建的SQL语句
int ret = updateSql(DaoConst.CREATE, model);
int ret = updateSql(sqlName, model);
// 判断是否需要获取自增编号(主键为整形)
if (StringHelper.isEmpty(keyString) && !isKeyString) {
keyString = this.getIdentity();
......@@ -138,6 +131,29 @@ public abstract class BaseDaoImpl extends BaseDaoSql implements BaseDao {
return retVal;
}
/**
* 创建数据,当不传入了主键时,则会自动生成主键,传入时不会生成。
*
* @param model 需要创建的数据
* @return 创建的主键编号, 创建成功,返回主键,否则为空
*/
@Override
public String create(Object model) {
return createReplace(DaoConst.CREATE, model);
}
/**
* 创建数据,当不传入了主键时,则会自动生成主键,传入时不会生成。
*
* @param model 需要创建的数据
* @return 创建的主键编号, 创建成功,返回主键,否则为空
*/
@Override
public String replace(Object model) {
return createReplace(DaoConst.REPLACE, model);
}
/**
* 修改数据
*
......@@ -214,7 +230,7 @@ public abstract class BaseDaoImpl extends BaseDaoSql implements BaseDao {
* @return 创建的主键编号
*/
@Override
public List<String> createList(List model) {
public List<String> createList(Collection model) {
List<String> ret = new ArrayList<>();
for (Object item : model) {
ret.add(this.create(item));
......@@ -249,6 +265,33 @@ public abstract class BaseDaoImpl extends BaseDaoSql implements BaseDao {
}
/**
* 创建数据,当不传入了主键时,则会自动生成主键,传入时不会生成。
*
* @param model 需要创建的数据
* @return 创建的主键编号
*/
@Override
public List<String> replaceArray(Object... model) {
return replaceList(Arrays.asList(model));
}
/**
* 修改数据
*
* @param model 需要修改的数据
* @return 删除的主键编号
*/
@Override
public List<String> replaceList(Collection model) {
List<String> ret = new ArrayList<>();
for (Object item : model) {
ret.add(this.replace(item));
}
return ret;
}
/**
* 修改数据
*
......@@ -267,7 +310,7 @@ public abstract class BaseDaoImpl extends BaseDaoSql implements BaseDao {
* @return 保存的主键编号
*/
@Override
public List<String> saveList(List model) {
public List<String> saveList(Collection model) {
List<String> ret = new ArrayList<>();
for (Object item : model) {
ret.add(this.save(item));
......@@ -295,7 +338,7 @@ public abstract class BaseDaoImpl extends BaseDaoSql implements BaseDao {
* @return 删除的记录数量
*/
@Override
public int removeList(List model) {
public int removeList(Collection model) {
int ret = 0;
for (Object item : model) {
ret += this.remove(item);
......@@ -678,6 +721,7 @@ public abstract class BaseDaoImpl extends BaseDaoSql implements BaseDao {
T from = null;
if (!StringHelper.isEmpty(key)) {
from = this.load(request, cls);
return saveFromCreate(cls, from, request);
}
return saveFromCreate(cls, from, request);
}
......
......@@ -362,7 +362,7 @@ public class TableStruct {
public void init(TableSqlCache table) {
table.setTable(this);
if (!StringHelper.isEmpty(this.name)) {
table.add(releaseSqlCreate(), releaseSqlUpdate(), releaseSqlRemove(), releaseSqlLoad());
table.add(releaseSqlReplace(), releaseSqlCreate(), releaseSqlUpdate(), releaseSqlRemove(), releaseSqlLoad());
}
initSaveWith(table);
initAddGroup(table);
......@@ -395,16 +395,29 @@ public class TableStruct {
this.addGroupSql(table, commonField, addGroupField);
}
/**
* 生成创建的SQL语句
*
* @return 生成的语句
*/
private SqlData releaseSqlReplace() {
return releaseSqlCreateReplace(DaoConst.SQL_REPLACE, DaoConst.REPLACE, DaoConst.SQL_TYPE_CREATE);
}
/**
* 生成创建的SQL语句
*
* @return 生成的语句
*/
private SqlData releaseSqlCreate() {
return releaseSqlCreateReplace(DaoConst.SQL_INSERT, DaoConst.CREATE, DaoConst.SQL_TYPE_CREATE);
}
private SqlData releaseSqlCreateReplace(String sqlModel, String sqlName, int sqlType) {
// 生成添加的SQL语句
String text = DaoConst.SQL_INSERT.replace(DaoConst.CODE_TABLE, this.name);
SqlData sql = new SqlData(DaoConst.CREATE, text);
sql.setSqlType(DaoConst.SQL_TYPE_CREATE);
String text = sqlModel.replace(DaoConst.CODE_TABLE, this.name);
SqlData sql = new SqlData(sqlName, text);
sql.setSqlType(sqlType);
// 第一个增加的字段,不需要增加 ","
String flag = StringHelper.EMPTY;
if (this.getKeyType() == String.class) {
......
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