@@ -50,8 +50,11 @@ public class CodeBuildHandler extends AbstractHandler { | |||||
buildCodeJavaPath(pageId); | buildCodeJavaPath(pageId); | ||||
for (Map<String, Object> map : tables) { | for (Map<String, Object> map : tables) { | ||||
final boolean needBean = MapUtil.readBool(map, "bean"); | |||||
final boolean needCache = MapUtil.readBool(map, "cache"); | |||||
if (!needBean && !needCache) continue; | |||||
long tableId = MapUtil.readLong(map, "tableId"); | long tableId = MapUtil.readLong(map, "tableId"); | ||||
buildJavaTable(tableId, MapUtil.readBool(map, "bean"), MapUtil.readBool(map, "cache")); | |||||
buildJavaTable(tableId, needBean, needCache); | |||||
} | } | ||||
if (needBuildService) buildJavaService(pageId); | if (needBuildService) buildJavaService(pageId); | ||||
return R.success(); | return R.success(); | ||||
@@ -0,0 +1,278 @@ | |||||
package cc.smtweb.framework.core.db; | |||||
import cc.smtweb.framework.core.annotation.SwColumn; | |||||
import cc.smtweb.framework.core.db.dao.AbstractBeanEntityDao; | |||||
import cc.smtweb.framework.core.db.dao.AbstractEntityDao; | |||||
import cc.smtweb.framework.core.db.dao.EntityColumn; | |||||
import cc.smtweb.framework.core.db.jdbc.JdbcEngine; | |||||
import cc.smtweb.framework.core.util.DateUtil; | |||||
import lombok.Getter; | |||||
import java.util.ArrayList; | |||||
import java.util.List; | |||||
/** | |||||
* 提供数据对象Dao操作,不是通过平台构建的表的dao | |||||
* @author xkliu | |||||
*/ | |||||
public class BeanEntityDao<T> extends AbstractBeanEntityDao<T> { | |||||
@Getter | |||||
protected JdbcEngine jdbcEngine; | |||||
public BeanEntityDao(Class<T> type, JdbcEngine jdbcEngine) { | |||||
super(type); | |||||
this.jdbcEngine = jdbcEngine; | |||||
} | |||||
/** | |||||
* 获取数据库唯一id | |||||
* @return 返回ID值 | |||||
*/ | |||||
public long nextId() { | |||||
return this.jdbcEngine.nextId(); | |||||
} | |||||
/** | |||||
* 根据PO对象的ID值更新其余所有字段 | |||||
* @param entity PO对象 | |||||
* @return 更新数量 | |||||
*/ | |||||
public int updateEntity(T entity) { | |||||
return updateEntity(entity, null,null); | |||||
} | |||||
/** | |||||
* 使用ID字段更新单行数据 | |||||
* @param entity PO值对象,对象属性是需要更新的值 | |||||
* @param fields 需要更新额字段列表,逗号分隔 | |||||
* @return 更新数量 | |||||
*/ | |||||
public int updateEntity(T entity, String fields) { | |||||
return updateEntity(entity, fields, null); | |||||
} | |||||
/** | |||||
* 指定自定义条件更新对象 | |||||
* @param entity PO值对象,对象属性是需要更新的值和更新条件值 | |||||
* @param fields 需要更新额字段列表,逗号分隔 | |||||
* @param whereFields 更新条件字段列表,逗号分隔 | |||||
* @return 更新数量 | |||||
*/ | |||||
public int updateEntity(T entity, String fields, String whereFields) { | |||||
StringBuilder sb = new StringBuilder(); | |||||
Object[] params = this.handleUpdate(entity, sb, fields, whereFields); | |||||
return jdbcEngine.update(sb.toString(), params); | |||||
} | |||||
/** | |||||
* 用PO对象所有字段入单行数据 | |||||
* @param type PO对象字段范围的类,是entity的父类 | |||||
* @param entity PO对象 | |||||
* @return 更新数量 | |||||
*/ | |||||
public int insertEntity(Class<? super T> type, T entity) { | |||||
return insertEntity(type, entity, null); | |||||
} | |||||
/** | |||||
* 用PO对象所有字段入单行数据 | |||||
* @param entity PO对象 | |||||
* @return 更新数量 | |||||
*/ | |||||
public int insertEntity(T entity) { | |||||
return insertEntity(entity.getClass(), entity, null); | |||||
} | |||||
/** | |||||
* 插入单行数据 | |||||
* @param entity PO对象 | |||||
* @param fields 逗号分隔的字段列表 | |||||
* @return 更新数量 | |||||
*/ | |||||
public int insertEntity(T entity, String fields) { | |||||
return insertEntity(entity.getClass(), entity, fields); | |||||
} | |||||
private int insertEntity(Class<?> type, T entity, String fields) { | |||||
StringBuilder sb = new StringBuilder(); | |||||
Object[] params = handleInsert(entity, sb, fields); | |||||
return jdbcEngine.update(sb.toString(), params); | |||||
} | |||||
/** | |||||
* 批量插入单行数据 | |||||
* @param entities PO对象列表 | |||||
* @return 更新数量 | |||||
*/ | |||||
public int[] batchInsertEntity(List<T> entities) { | |||||
return batchInsertEntity(entities, null); | |||||
} | |||||
/** | |||||
* 批量插入单行数据 | |||||
* @param entities PO对象列表 | |||||
* @param fields 逗号分隔的字段列表 | |||||
* @return 更新数量 | |||||
*/ | |||||
public int[] batchInsertEntity(List<T> entities, String fields) { | |||||
StringBuilder sql = new StringBuilder(); | |||||
sql.append("insert into ").append(tableName).append("("); | |||||
Long now = DateUtil.nowDateTimeLong(); | |||||
EntityColumn createTimeColumn = typeColumns.get(SwColumn.Type.CREATE_TIME); | |||||
EntityColumn lastTimeColumn = typeColumns.get(SwColumn.Type.LAST_TIME); | |||||
boolean includeCreateTimeColumn = false; | |||||
boolean includeLastTimeColumn = false; | |||||
List<EntityColumn> insertColumns = new ArrayList<>(this.columns.size()); | |||||
if (fields == null) { | |||||
for (EntityColumn column: this.columns.values()) { | |||||
sql.append(column.getFieldName()).append(","); | |||||
insertColumns.add(column); | |||||
} | |||||
} else { | |||||
String[] fieldNames = fields.split(","); | |||||
for (String name: fieldNames) { | |||||
EntityColumn column = this.columns.get(name.trim()); | |||||
sql.append(column.getFieldName()).append(","); | |||||
if (column == createTimeColumn) { | |||||
includeCreateTimeColumn = true; | |||||
} else if (column == lastTimeColumn) { | |||||
includeLastTimeColumn = true; | |||||
} | |||||
insertColumns.add(column); | |||||
} | |||||
if (createTimeColumn != null && !includeCreateTimeColumn) { | |||||
sql.append(createTimeColumn.getFieldName()).append(","); | |||||
} | |||||
if (lastTimeColumn != null && !includeLastTimeColumn) { | |||||
sql.append(lastTimeColumn.getFieldName()).append(","); | |||||
} | |||||
} | |||||
sql.setCharAt(sql.length() - 1, ')'); | |||||
// values(?,?) | |||||
sql.append(" values("); | |||||
for (int i = insertColumns.size(); i > 0; i--) { | |||||
sql.append("?,"); | |||||
} | |||||
sql.setCharAt(sql.length() - 1, ')'); | |||||
// 参数列表 | |||||
List<Object[]> listParams = new ArrayList<>(entities.size()); | |||||
for (T obj: entities) { | |||||
List<Object> params = new ArrayList<>(insertColumns.size()); | |||||
for (EntityColumn column : insertColumns) { | |||||
if (column == createTimeColumn) { | |||||
column.writeValue(obj, now); | |||||
params.add(now); | |||||
} else if (column == lastTimeColumn) { | |||||
column.writeValue(obj, now); | |||||
params.add(now); | |||||
} else { | |||||
params.add(column.readValue(obj)); | |||||
} | |||||
} | |||||
listParams.add(params.toArray()); | |||||
} | |||||
return jdbcEngine.batchUpdate(sql.toString(), listParams); | |||||
} | |||||
/** | |||||
* 根据ID值删除单行数据 | |||||
* @param entity PO对象 | |||||
* @return 删除数量 | |||||
*/ | |||||
public int deleteEntity(T entity) { | |||||
StringBuilder sb = new StringBuilder(); | |||||
Object[] params = handleDelete(entity, sb); | |||||
return jdbcEngine.update(sb.toString(), params); | |||||
} | |||||
/** | |||||
* 根据ID值删除单行数据 | |||||
* @param id 记录主建值 | |||||
* @return 删除数量 | |||||
*/ | |||||
public int deleteEntity(Long id) { | |||||
StringBuilder sb = new StringBuilder(); | |||||
handleDelete(sb); | |||||
return jdbcEngine.update(sb.toString(), id); | |||||
} | |||||
/** | |||||
* 根据ID值删除单行数据 | |||||
* @param whereSql Where条件SQL语句,以where开头 | |||||
* @param params 条件的值,可以多个 | |||||
* @return 删除数量 | |||||
*/ | |||||
public int deleteEntity(String whereSql, Object...params) { | |||||
StringBuilder sb = new StringBuilder("DELETE FROM "); | |||||
sb.append(getTableName()).append(" ").append(whereSql); | |||||
return jdbcEngine.update(sb.toString(), params); | |||||
} | |||||
/** | |||||
* 读取实体对象ID值 | |||||
* @param entity | |||||
* @return | |||||
*/ | |||||
public Long readEntityId(T entity) { | |||||
return (Long)readId(entity); | |||||
} | |||||
/** 查询单行数据,返回bean */ | |||||
public T queryEntity(Long id) { | |||||
return queryEntity(id, null); | |||||
} | |||||
public T queryEntity(Long id, String fields) { | |||||
StringBuilder sb = new StringBuilder(); | |||||
handleSelectOne(sb, fields); | |||||
List<T> list = jdbcEngine.query(sb.toString(), type, id); | |||||
if (list != null && !list.isEmpty()) { | |||||
return list.get(0); | |||||
} | |||||
return null; | |||||
} | |||||
/** 查询对象所有数据,返回列表 */ | |||||
public List<T> query() { | |||||
return query(null); | |||||
} | |||||
/** 查询对象所有数据,返回列表 */ | |||||
public List<T> query(String fields) { | |||||
StringBuilder sb = new StringBuilder(); | |||||
handleSelect(sb, fields); | |||||
return jdbcEngine.query(sb.toString(), type); | |||||
} | |||||
/** 查询对象所有数据,返回列表 */ | |||||
public List<T> queryWhere(String sqlWhere, Object...params) { | |||||
StringBuilder sb = new StringBuilder(); | |||||
handleSelect(sb, null); | |||||
sb.append(" where ").append(sqlWhere); | |||||
return jdbcEngine.query(sb.toString(), type, params); | |||||
} | |||||
} |
@@ -23,6 +23,7 @@ import java.util.concurrent.ConcurrentHashMap; | |||||
@Slf4j | @Slf4j | ||||
public class DbEngine extends JdbcEngine { | public class DbEngine extends JdbcEngine { | ||||
private String dbSchema; | private String dbSchema; | ||||
private final Map<Class<?>, BeanEntityDao<?>> beanDaoMap = new ConcurrentHashMap<>(); | |||||
private final Map<Class<?>, EntityDao<?>> daoMap = new ConcurrentHashMap<>(); | private final Map<Class<?>, EntityDao<?>> daoMap = new ConcurrentHashMap<>(); | ||||
private final Map<String, EntityDao<DefaultEntity>> tableDaoMap = new ConcurrentHashMap<>(); | private final Map<String, EntityDao<DefaultEntity>> tableDaoMap = new ConcurrentHashMap<>(); | ||||
@@ -35,6 +36,25 @@ public class DbEngine extends JdbcEngine { | |||||
} | } | ||||
/** | /** | ||||
* 不是通过平台构建的表的dao | |||||
* @param type | |||||
* @param <T> | |||||
* @return | |||||
*/ | |||||
public <T> BeanEntityDao<T> findBeanDao(Class<T> type) { | |||||
BeanEntityDao<T> handler = (BeanEntityDao<T>) beanDaoMap.get(type); | |||||
if (handler == null) { | |||||
synchronized (daoMap) { | |||||
handler = new BeanEntityDao<>(type, this); | |||||
beanDaoMap.put(type, handler); | |||||
} | |||||
} | |||||
return handler; | |||||
} | |||||
/** | |||||
* bean为Data格式,调用此方法 | * bean为Data格式,调用此方法 | ||||
* @param type bean类 | * @param type bean类 | ||||
* @param <T> | * @param <T> | ||||
@@ -0,0 +1,453 @@ | |||||
package cc.smtweb.framework.core.db.dao; | |||||
import cc.smtweb.framework.core.annotation.SwColumn; | |||||
import cc.smtweb.framework.core.annotation.SwColumnForeign; | |||||
import cc.smtweb.framework.core.annotation.SwTable; | |||||
import cc.smtweb.framework.core.exception.DbException; | |||||
import cc.smtweb.framework.core.util.DateUtil; | |||||
import cc.smtweb.framework.core.util.VariableUtil; | |||||
import org.apache.commons.lang3.StringUtils; | |||||
import org.springframework.util.ClassUtils; | |||||
import java.beans.IntrospectionException; | |||||
import java.beans.PropertyDescriptor; | |||||
import java.lang.reflect.Field; | |||||
import java.lang.reflect.Method; | |||||
import java.util.ArrayList; | |||||
import java.util.HashMap; | |||||
import java.util.List; | |||||
import java.util.Map; | |||||
/** | |||||
* 抽象的值对象数据库访问 | |||||
* 不是通过平台构建的表的dao | |||||
* @param <T> 数据库值对象类型 | |||||
*/ | |||||
public class AbstractBeanEntityDao<T> { | |||||
protected String tableName; | |||||
protected Map<String, EntityColumn> columns = new HashMap<>(); | |||||
protected Map<SwColumn.Type, EntityColumn> typeColumns = new HashMap<>(); | |||||
protected Class<T> type; | |||||
// public AbstractEntityDao(String tableName) { | |||||
// this.tableName = tableName; | |||||
// } | |||||
/** | |||||
* 通过值对象类型构造值对象数据库访问 | |||||
* @param type 值对象类型 | |||||
*/ | |||||
public AbstractBeanEntityDao(Class<T> type) { | |||||
this.type = type; | |||||
// type.isAnnotationPresent(Table.class); | |||||
SwTable table = type.getAnnotation(SwTable.class); | |||||
Class<? super T> superclass = type.getSuperclass(); | |||||
if (table == null && superclass != null) { | |||||
table = superclass.getAnnotation(SwTable.class); | |||||
} | |||||
if (table == null) { | |||||
throw new IllegalAccessError("not find annotation @SwTable"); | |||||
} | |||||
List<Field> fields; | |||||
if (superclass == null) { | |||||
fields = appendField(type.getFields(), type.getDeclaredFields()); | |||||
} else { | |||||
fields = appendField(type.getFields(), type.getDeclaredFields(), superclass.getFields(), | |||||
superclass.getDeclaredFields()); | |||||
} | |||||
// Map<String, String> columnToPropertyOverrides = new HashMap<>(); | |||||
tableName = table.value(); | |||||
for (Field field : fields) { | |||||
SwColumn column = field.getAnnotation(SwColumn.class); | |||||
// 获取name | |||||
String columnName; | |||||
if (column != null && StringUtils.isNotBlank(column.value())) { | |||||
columnName = column.value(); | |||||
} else { | |||||
columnName = VariableUtil.humpToUnderline(field.getName()); | |||||
} | |||||
// 处理get method | |||||
try { | |||||
PropertyDescriptor pd = new PropertyDescriptor(field.getName(), type); | |||||
// 获得get方法 | |||||
Method readMethod = pd.getReadMethod(); | |||||
Method writeMethod = pd.getWriteMethod(); | |||||
if (readMethod != null && writeMethod != null) { | |||||
Class<?>[] parameterTypes = writeMethod.getParameterTypes(); | |||||
if (parameterTypes.length == 1 && ClassUtils.isAssignable(parameterTypes[0], readMethod.getReturnType())) { | |||||
add(columnName, readMethod, writeMethod, column); | |||||
} | |||||
} | |||||
} catch (IntrospectionException ignore) { | |||||
} | |||||
// TODO 判断是否Public | |||||
} | |||||
} | |||||
private List<Field> appendField(Field[]... fields) { | |||||
List<Field> list = new ArrayList<>(); | |||||
if (fields == null) { | |||||
return list; | |||||
} | |||||
for (Field[] field : fields) { | |||||
if (field == null) { | |||||
continue; | |||||
} | |||||
for (Field temp : field) { | |||||
list.add(temp); | |||||
} | |||||
} | |||||
return list; | |||||
} | |||||
/** | |||||
* 添加值对象的属性访问方法何注解值 | |||||
* @param fieldName 字段名 | |||||
* @param readMethod 读值对象属性方法 | |||||
* @param writeMethod 写值值对象属性方法 | |||||
* @param column 字段注解 | |||||
*/ | |||||
public void add(String fieldName, Method readMethod, Method writeMethod, SwColumn column) { | |||||
EntityColumnForeign foreign = null; | |||||
EntityColumn beanColumn = new EntityColumn(fieldName, readMethod, writeMethod); | |||||
columns.put(fieldName, beanColumn); | |||||
if (column != null) { | |||||
for (SwColumn.Type type : column.type()) { | |||||
typeColumns.put(type, beanColumn); | |||||
} | |||||
} | |||||
} | |||||
/** | |||||
* 拼接插入SQL语句 | |||||
* @param obj 值对象 | |||||
* @param sql 记录sql的字符缓存 | |||||
* @param fields 逗号分割字段列表,设置为null表示使用所有字段 | |||||
* @return SQL参数列表 | |||||
*/ | |||||
protected Object[] handleInsert(T obj, StringBuilder sql, String fields) { | |||||
List<Object> result; | |||||
sql.append("insert into ").append(tableName).append("("); | |||||
Long now = DateUtil.nowDateTimeLong(); | |||||
EntityColumn createTimeColumn = typeColumns.get(SwColumn.Type.CREATE_TIME); | |||||
EntityColumn lastTimeColumn = typeColumns.get(SwColumn.Type.LAST_TIME); | |||||
if (fields == null) { | |||||
// len = this.columns.size(); | |||||
result = new ArrayList<>(this.columns.size()); | |||||
// int index = 0; | |||||
for (EntityColumn column: this.columns.values()) { | |||||
Object value = column.readValue(obj); | |||||
if (column == createTimeColumn) { | |||||
if (value == null) { | |||||
column.writeValue(obj, now); | |||||
value = now; | |||||
} | |||||
createTimeColumn = null; | |||||
} else if (column == lastTimeColumn) { | |||||
if (value == null) { | |||||
column.writeValue(obj, now); | |||||
value = now; | |||||
} | |||||
lastTimeColumn = null; | |||||
} | |||||
result.add(value); | |||||
sql.append(column.getFieldName()).append(","); | |||||
} | |||||
} else { | |||||
String[] fieldNames = fields.split(","); | |||||
result = new ArrayList<>(fieldNames.length + 2); | |||||
for (String name: fieldNames) { | |||||
EntityColumn column = this.columns.get(name.trim()); | |||||
if (column == null) { | |||||
throw new IllegalArgumentException("not define column: " + name); | |||||
} | |||||
Object value = column.readValue(obj); | |||||
if (column == createTimeColumn) { | |||||
if (value == null) { | |||||
column.writeValue(obj, now); | |||||
value = now; | |||||
} | |||||
createTimeColumn = null; | |||||
} else if (column == lastTimeColumn) { | |||||
if (value == null) { | |||||
column.writeValue(obj, now); | |||||
value = now; | |||||
} | |||||
lastTimeColumn = null; | |||||
} | |||||
result.add(value); | |||||
sql.append(column.getFieldName()).append(","); | |||||
} | |||||
// 自动更新fields未设置 createTime | |||||
if (createTimeColumn != null) { | |||||
createTimeColumn.writeValue(obj, now); | |||||
result.add(now); | |||||
sql.append(createTimeColumn.getFieldName()).append(","); | |||||
} | |||||
// 自动更新fields未设置 createTime | |||||
if (lastTimeColumn != null) { | |||||
lastTimeColumn.writeValue(obj, now); | |||||
result.add(now); | |||||
sql.append(lastTimeColumn.getFieldName()).append(","); | |||||
} | |||||
} | |||||
sql.setCharAt(sql.length() - 1, ')'); | |||||
// values(?,?) | |||||
sql.append(" values("); | |||||
for (int i = result.size(); i > 0; i--) { | |||||
sql.append("?,"); | |||||
} | |||||
sql.setCharAt(sql.length() - 1, ')'); | |||||
return result.toArray(); | |||||
} | |||||
/** | |||||
* 拼接更新SQL语句 | |||||
* @param obj 值对象 | |||||
* @param sql 记录sql的字符缓存 | |||||
* @param fields 逗号分割更新字段列表,设置为null表示使用所有字段 | |||||
* @param whereFields 逗号分割查询字段列表,设置为null表示使用ID字段作为查询条件 | |||||
* @return SQL参数列表 | |||||
*/ | |||||
protected Object[] handleUpdate(T obj, StringBuilder sql, String fields, String whereFields) { | |||||
EntityColumn idColumn = findIdColumn(); | |||||
EntityColumn lastTimeColumn = typeColumns.get(SwColumn.Type.LAST_TIME); | |||||
Long now = DateUtil.nowDateTimeLong(); | |||||
sql.append("update ").append(tableName).append(" set "); | |||||
List<Object> result = new ArrayList<>(); | |||||
if (fields == null) { | |||||
for (EntityColumn column: this.columns.values()) { | |||||
if (idColumn != column) { | |||||
Object value = column.readValue(obj); | |||||
if (lastTimeColumn == column) { | |||||
if (value == null) { | |||||
value = now; | |||||
} else { | |||||
lastTimeColumn = null; | |||||
} | |||||
} | |||||
result.add(value); | |||||
sql.append(column.getFieldName()).append("=?,"); | |||||
} | |||||
} | |||||
sql.setCharAt(sql.length() - 1, ' '); | |||||
// 默认使用Id字段条件 | |||||
result.add(idColumn.readValue(obj)); | |||||
sql.append("where ").append(idColumn.getFieldName()).append("=?"); | |||||
} else { | |||||
String[] fieldNames = fields.split(","); | |||||
for (String name: fieldNames) { | |||||
name = name.trim(); | |||||
EntityColumn beanColumn = getBeanColumn(name); | |||||
Object value = beanColumn.readValue(obj); | |||||
if (lastTimeColumn == beanColumn) { | |||||
if (value == null) { | |||||
value = now; | |||||
} else { | |||||
lastTimeColumn = null; | |||||
} | |||||
} | |||||
result.add(value); | |||||
sql.append(name).append("=?,"); | |||||
} | |||||
if (lastTimeColumn != null) { | |||||
result.add(now); | |||||
sql.append(lastTimeColumn.getFieldName()).append("=?,"); | |||||
} | |||||
sql.setCharAt(sql.length() - 1, ' '); | |||||
if (StringUtils.isNotBlank(whereFields)) { | |||||
sql.append("where "); | |||||
boolean first = true; | |||||
for (String name: whereFields.split(",")) { | |||||
name = name.trim(); | |||||
if (first) { | |||||
first = false; | |||||
} else { | |||||
sql.append(" and "); | |||||
} | |||||
sql.append(name).append("=?"); | |||||
result.add(readValue(obj, name)); | |||||
} | |||||
} else { | |||||
// 默认使用Id字段条件 | |||||
result.add(idColumn.readValue(obj)); | |||||
sql.append("where ").append(idColumn.getFieldName()).append("=?"); | |||||
} | |||||
} | |||||
// 自动更新对象的值 | |||||
if (lastTimeColumn != null) { | |||||
lastTimeColumn.writeValue(obj, now); | |||||
} | |||||
return result.toArray(); | |||||
} | |||||
private EntityColumn findIdColumn() { | |||||
EntityColumn idColumn = typeColumns.get(SwColumn.Type.ID); | |||||
if (idColumn == null) { | |||||
throw new DbException(tableName + " not define id column"); | |||||
} | |||||
return idColumn; | |||||
} | |||||
/** | |||||
* 拼接删除值对象语句 | |||||
* @param obj 值对象 | |||||
* @param sql 记录sql的字符缓存 | |||||
* @return SQL参数列表 | |||||
*/ | |||||
protected Object[] handleDelete(T obj, StringBuilder sql) { | |||||
EntityColumn idColumn = findIdColumn(); | |||||
sql.append("DELETE FROM ").append(tableName).append(" WHERE ").append(idColumn.getFieldName()).append("=?"); | |||||
return new Object[]{idColumn.readValue(obj)}; | |||||
} | |||||
/** | |||||
* 拼接删除值对象语句,条件由外部设置 | |||||
* @param sql 记录sql的字符缓存 | |||||
*/ | |||||
protected void handleDelete(StringBuilder sql) { | |||||
EntityColumn idColumn = findIdColumn(); | |||||
sql.append("DELETE FROM ").append(tableName).append(" WHERE ").append(idColumn.getFieldName()).append("=?"); | |||||
} | |||||
private Object readValue(T obj, String fieldName) { | |||||
EntityColumn beanColumn = getBeanColumn(fieldName); | |||||
return beanColumn.readValue(obj); | |||||
} | |||||
private EntityColumn getBeanColumn(String fieldName) { | |||||
EntityColumn beanColumn = this.columns.get(fieldName); | |||||
if (beanColumn == null) { | |||||
throw new DbException("not define column:" + fieldName); | |||||
} | |||||
return beanColumn; | |||||
} | |||||
/** | |||||
* 获取表名 | |||||
* @return 表名 | |||||
*/ | |||||
protected String getTableName() { | |||||
return tableName; | |||||
} | |||||
/** | |||||
* 拼接查询SQL语句 | |||||
* @param sql SQL字符缓存 | |||||
* @param fields 逗号分割的查询字段列表,传入null表示全部值对象字段 | |||||
*/ | |||||
public void handleSelect(StringBuilder sql, String fields) { | |||||
sql.append("select "); | |||||
if (fields != null) { | |||||
sql.append(fields).append(' '); | |||||
} else { | |||||
for (String fieldName : columns.keySet()) { | |||||
sql.append(fieldName).append(','); | |||||
} | |||||
sql.setCharAt(sql.length() - 1, ' '); | |||||
} | |||||
sql.append("from ").append(tableName); | |||||
} | |||||
/** | |||||
* 拼接SQL排序字段 | |||||
* @param sql SQL字符缓存 | |||||
*/ | |||||
public void handleOrderBy(StringBuilder sql) { | |||||
EntityColumn entityColumn = this.typeColumns.get(SwColumn.Type.ORDER); | |||||
if (entityColumn != null) { | |||||
sql.append(" order by ").append(entityColumn.getFieldName()); | |||||
EntityColumn idColumn = this.typeColumns.get(SwColumn.Type.ID); | |||||
if (idColumn != null) { | |||||
sql.append(", ").append(idColumn.getFieldName()); | |||||
} | |||||
} | |||||
} | |||||
protected void handleSelectOne(StringBuilder sql, String fields) { | |||||
EntityColumn idColumn = findIdColumn(); | |||||
sql.append("select "); | |||||
if (fields != null) { | |||||
sql.append(fields).append(' '); | |||||
} else { | |||||
for (EntityColumn field : columns.values()) { | |||||
sql.append(field.getFieldName()).append(","); | |||||
} | |||||
sql.setCharAt(sql.length() - 1, ' '); | |||||
} | |||||
sql.append("from ").append(tableName).append(" where ").append(idColumn.getFieldName()).append("=?"); | |||||
} | |||||
public EntityColumn getColumn(SwColumn.Type type) { | |||||
return this.typeColumns.get(type); | |||||
} | |||||
/** | |||||
* 读取ID值 | |||||
* @param entity | |||||
* @return | |||||
*/ | |||||
public Long readId(T entity) { | |||||
EntityColumn idColumn = findIdColumn(); | |||||
return (Long)idColumn.readValue(entity); | |||||
} | |||||
} |
@@ -1,5 +1,7 @@ | |||||
package cc.smtweb.framework.core.db.dao; | package cc.smtweb.framework.core.db.dao; | ||||
import cc.smtweb.framework.core.annotation.SwColumn; | |||||
import cc.smtweb.framework.core.annotation.SwColumnForeign; | |||||
import cc.smtweb.framework.core.annotation.SwTable; | import cc.smtweb.framework.core.annotation.SwTable; | ||||
import cc.smtweb.framework.core.cache.CacheManager; | import cc.smtweb.framework.core.cache.CacheManager; | ||||
import cc.smtweb.framework.core.common.SwEnum; | import cc.smtweb.framework.core.common.SwEnum; | ||||
@@ -17,6 +19,7 @@ import org.springframework.util.ClassUtils; | |||||
import java.beans.IntrospectionException; | import java.beans.IntrospectionException; | ||||
import java.beans.PropertyDescriptor; | import java.beans.PropertyDescriptor; | ||||
import java.lang.reflect.Field; | |||||
import java.lang.reflect.Method; | import java.lang.reflect.Method; | ||||
import java.util.ArrayList; | import java.util.ArrayList; | ||||
import java.util.HashMap; | import java.util.HashMap; | ||||
@@ -153,7 +156,7 @@ public abstract class AbstractEntityDao<T> { | |||||
for (String name : fieldNames) { | for (String name : fieldNames) { | ||||
EntityColumn column = this.columns.get(name.trim()); | EntityColumn column = this.columns.get(name.trim()); | ||||
for (int i = 0, len = types.length; i < len; i++) { | for (int i = 0, len = types.length; i < len; i++) { | ||||
if (types[i]== column.getField().getFieldType()) { | |||||
if (types[i] == column.getField().getFieldType()) { | |||||
includeTypes[i] = true; | includeTypes[i] = true; | ||||
} | } | ||||
} | } | ||||
@@ -386,7 +389,7 @@ public abstract class AbstractEntityDao<T> { | |||||
try { | try { | ||||
T bean = this.type.newInstance(); | T bean = this.type.newInstance(); | ||||
if (bean instanceof DefaultEntity) { | if (bean instanceof DefaultEntity) { | ||||
DefaultEntity b = (DefaultEntity)bean; | |||||
DefaultEntity b = (DefaultEntity) bean; | |||||
b.init(); | b.init(); | ||||
b.setTableName(this.tableName); | b.setTableName(this.tableName); | ||||
} | } | ||||
@@ -10,57 +10,69 @@ import java.lang.reflect.Method; | |||||
/** | /** | ||||
* 值对象字段处理类 | * 值对象字段处理类 | ||||
* | |||||
* @author xkliu | * @author xkliu | ||||
*/ | */ | ||||
@Getter | @Getter | ||||
public class EntityColumn { | public class EntityColumn { | ||||
private ModelField field; | |||||
private final Method readMethod; | |||||
private final Method writeMethod; | |||||
//非平台实体定义 | |||||
private String fieldName; | |||||
private ModelField field; | |||||
private final Method readMethod; | |||||
private final Method writeMethod; | |||||
/** | |||||
* 构建值对象字段 | |||||
* @param field 字段名 | |||||
* @param readMethod 读值方法 | |||||
* @param writeMethod 写值方法 | |||||
*/ | |||||
public EntityColumn(ModelField field, Method readMethod, Method writeMethod) { | |||||
this.field = field; | |||||
this.readMethod = readMethod; | |||||
this.writeMethod = writeMethod; | |||||
} | |||||
/** | |||||
* 构建值对象字段 | |||||
* | |||||
* @param field 字段名 | |||||
* @param readMethod 读值方法 | |||||
* @param writeMethod 写值方法 | |||||
*/ | |||||
public EntityColumn(ModelField field, Method readMethod, Method writeMethod) { | |||||
this.field = field; | |||||
this.readMethod = readMethod; | |||||
this.writeMethod = writeMethod; | |||||
} | |||||
/** | |||||
* 从对象中读取字段对应的属性值 | |||||
* @param obj 值对象 | |||||
* @return 属性值 | |||||
*/ | |||||
public Object readValue(Object obj) { | |||||
if (readMethod != null) { | |||||
try { | |||||
return readMethod.invoke(obj); | |||||
} catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException e) { | |||||
throw new DbException(e); | |||||
} | |||||
} else { | |||||
return ((DefaultEntity)obj).get(field.getName()); | |||||
} | |||||
} | |||||
public EntityColumn(String fieldName, Method readMethod, Method writeMethod) { | |||||
this.fieldName = fieldName; | |||||
this.readMethod = readMethod; | |||||
this.writeMethod = writeMethod; | |||||
} | |||||
/** | |||||
* 写入值到对象字段对象属性 | |||||
* @param obj 值对象 | |||||
* @param value 属性值 | |||||
*/ | |||||
public void writeValue(Object obj, Object value) { | |||||
if (readMethod != null) { | |||||
try { | |||||
writeMethod.invoke(obj, value); | |||||
} catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException e) { | |||||
throw new DbException(e); | |||||
} | |||||
} else { | |||||
((DefaultEntity)obj).put(field.getName(), value); | |||||
} | |||||
} | |||||
/** | |||||
* 从对象中读取字段对应的属性值 | |||||
* | |||||
* @param obj 值对象 | |||||
* @return 属性值 | |||||
*/ | |||||
public Object readValue(Object obj) { | |||||
if (readMethod != null) { | |||||
try { | |||||
return readMethod.invoke(obj); | |||||
} catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException e) { | |||||
throw new DbException(e); | |||||
} | |||||
} else { | |||||
return ((DefaultEntity) obj).get(field.getName()); | |||||
} | |||||
} | |||||
/** | |||||
* 写入值到对象字段对象属性 | |||||
* | |||||
* @param obj 值对象 | |||||
* @param value 属性值 | |||||
*/ | |||||
public void writeValue(Object obj, Object value) { | |||||
if (readMethod != null) { | |||||
try { | |||||
writeMethod.invoke(obj, value); | |||||
} catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException e) { | |||||
throw new DbException(e); | |||||
} | |||||
} else { | |||||
((DefaultEntity) obj).put(field.getName(), value); | |||||
} | |||||
} | |||||
} | } |