diff --git a/src/org/redkale/source/DataJdbcSource.java b/src/org/redkale/source/DataJdbcSource.java index 2c1b81725..bb015da02 100644 --- a/src/org/redkale/source/DataJdbcSource.java +++ b/src/org/redkale/source/DataJdbcSource.java @@ -84,7 +84,7 @@ public class DataJdbcSource extends AbstractService implements DataSource, DataC if (writeprop.isEmpty()) writeprop = readprop; this.initByProperties(unitName, readprop, writeprop); } - + //构造前调用 protected void preConstruct(String unitName, Properties readprop, Properties writeprop) { } @@ -1495,7 +1495,7 @@ public class DataJdbcSource extends AbstractService implements DataSource, DataC final Connection conn = createReadSQLConnection(); try { final SelectColumn sels = selects; - final String sql = "SELECT * FROM " + info.getTable(pk) + " WHERE " + info.getPrimarySQLColumn() + " = " + FilterNode.formatToString(pk); + final String sql = "SELECT " + info.getQueryColumns(null, selects) + " FROM " + info.getTable(pk) + " WHERE " + info.getPrimarySQLColumn() + " = " + FilterNode.formatToString(pk); if (info.isLoggable(logger, Level.FINEST)) logger.finest(clazz.getSimpleName() + " find sql=" + sql); conn.setReadOnly(true); final PreparedStatement ps = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); @@ -1572,7 +1572,7 @@ public class DataJdbcSource extends AbstractService implements DataSource, DataC final Map joinTabalis = node == null ? null : node.getJoinTabalis(); final CharSequence join = node == null ? null : node.createSQLJoin(this, false, joinTabalis, new HashSet<>(), info); final CharSequence where = node == null ? null : node.createSQLExpress(info, joinTabalis); - final String sql = "SELECT a.* FROM " + info.getTable(node) + " a" + (join == null ? "" : join) + ((where == null || where.length() == 0) ? "" : (" WHERE " + where)); + final String sql = "SELECT " + info.getQueryColumns("a", selects) + " FROM " + info.getTable(node) + " a" + (join == null ? "" : join) + ((where == null || where.length() == 0) ? "" : (" WHERE " + where)); if (info.isLoggable(logger, Level.FINEST)) logger.finest(clazz.getSimpleName() + " find sql=" + sql); conn.setReadOnly(true); final PreparedStatement ps = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); @@ -2298,7 +2298,7 @@ public class DataJdbcSource extends AbstractService implements DataSource, DataC final Map joinTabalis = node == null ? null : node.getJoinTabalis(); final CharSequence join = node == null ? null : node.createSQLJoin(this, false, joinTabalis, new HashSet<>(), info); final CharSequence where = node == null ? null : node.createSQLExpress(info, joinTabalis); - final String sql = "SELECT a.* FROM " + info.getTable(node) + " a" + (join == null ? "" : join) + final String sql = "SELECT " + info.getQueryColumns("a", selects) + " FROM " + info.getTable(node) + " a" + (join == null ? "" : join) + ((where == null || where.length() == 0) ? "" : (" WHERE " + where)) + info.createSQLOrderby(flipper); if (info.isLoggable(logger, Level.FINEST)) { logger.finest(clazz.getSimpleName() + " query sql=" + sql + (flipper == null || flipper.getLimit() < 1 ? "" : (" LIMIT " + flipper.getOffset() + "," + flipper.getLimit()))); diff --git a/src/org/redkale/source/EntityInfo.java b/src/org/redkale/source/EntityInfo.java index bd4265019..c6c0d034a 100644 --- a/src/org/redkale/source/EntityInfo.java +++ b/src/org/redkale/source/EntityInfo.java @@ -434,6 +434,26 @@ public final class EntityInfo { return deleteSQL.replace("${newtable}", getTable(bean)); } + /** + * 获取查询字段列表 + * + * @param tabalis 表别名 + * @param selects 过滤字段 + * + * @return String + */ + public CharSequence getQueryColumns(String tabalis, SelectColumn selects) { + if (selects == null) return tabalis == null ? "*" : (tabalis + ".*"); + StringBuilder sb = new StringBuilder(); + for (Attribute attr : this.attributes) { + if (!selects.test(attr.field())) continue; + if (sb.length() > 0) sb.append(','); + sb.append(getSQLColumn(tabalis, attr.field())); + } + if (sb.length() == 0) sb.append('*'); + return sb; + } + /** * 根据主键值获取Entity的表名 *