Search in sources :

Example 1 with TableMapper

use of com.jeesuite.mybatis.crud.helper.TableMapper in project jeesuite-libs by vakinge.

the class GetByPrimaryKeyBuilder method buildGetByIdSql.

private static String buildGetByIdSql(EntityMapper entityMapper) {
    // 从表注解里获取表名等信息
    TableMapper tableMapper = entityMapper.getTableMapper();
    Set<ColumnMapper> columnsMapper = entityMapper.getColumnsMapper();
    // 根据字段注解和属性值联合生成sql语句
    BEGIN();
    FROM(tableMapper.getName());
    for (ColumnMapper columnMapper : columnsMapper) {
        if (columnMapper.isId()) {
            WHERE(columnMapper.getColumn() + "=#{" + columnMapper.getProperty() + "}");
        }
        SELECT(columnMapper.getColumn());
    }
    return String.format(SqlTemplate.SCRIPT_TEMAPLATE, SQL());
}
Also used : TableMapper(com.jeesuite.mybatis.crud.helper.TableMapper) ColumnMapper(com.jeesuite.mybatis.crud.helper.ColumnMapper)

Example 2 with TableMapper

use of com.jeesuite.mybatis.crud.helper.TableMapper in project jeesuite-libs by vakinge.

the class InsertBuilder method buildInsertSql.

public static String buildInsertSql(EntityMapper entityMapper, boolean selective) {
    // 从表注解里获取表名等信息
    TableMapper table = entityMapper.getTableMapper();
    Set<ColumnMapper> columns = entityMapper.getColumnsMapper();
    StringBuilder fieldBuilder = new StringBuilder(SqlTemplate.TRIM_PREFIX);
    StringBuilder prppertyBuilder = new StringBuilder(SqlTemplate.TRIM_PREFIX);
    if (!entityMapper.autoId()) {
        /* 用户输入自定义ID */
        fieldBuilder.append(entityMapper.getIdColumn().getColumn()).append(",");
        prppertyBuilder.append("#{").append(entityMapper.getIdColumn().getProperty()).append("},");
    }
    for (ColumnMapper column : columns) {
        if (column.isId() || !column.isInsertable()) {
            continue;
        }
        String fieldExpr = SqlTemplate.wrapIfTag(column.getProperty(), column.getColumn(), !selective);
        String propertyExpr = SqlTemplate.wrapIfTag(column.getProperty(), "#{" + column.getProperty() + "}", !selective);
        fieldBuilder.append(fieldExpr);
        fieldBuilder.append(selective ? "\n" : ",");
        prppertyBuilder.append(propertyExpr);
        prppertyBuilder.append(selective ? "\n" : ",");
    }
    if (!selective) {
        fieldBuilder.deleteCharAt(fieldBuilder.length() - 1);
        prppertyBuilder.deleteCharAt(prppertyBuilder.length() - 1);
    }
    fieldBuilder.append(SqlTemplate.TRIM_SUFFIX);
    prppertyBuilder.append(SqlTemplate.TRIM_SUFFIX);
    String sql = String.format(SqlTemplate.INSERT, table.getName(), fieldBuilder.toString(), prppertyBuilder.toString());
    return String.format(SqlTemplate.SCRIPT_TEMAPLATE, sql);
}
Also used : TableMapper(com.jeesuite.mybatis.crud.helper.TableMapper) ColumnMapper(com.jeesuite.mybatis.crud.helper.ColumnMapper)

Example 3 with TableMapper

use of com.jeesuite.mybatis.crud.helper.TableMapper in project jeesuite-libs by vakinge.

the class UpdateBuilder method buildUpdateSql.

public static String buildUpdateSql(EntityMapper entityMapper, boolean selective) {
    // 从表注解里获取表名等信息
    TableMapper tableMapper = entityMapper.getTableMapper();
    Set<ColumnMapper> columnMappers = entityMapper.getColumnsMapper();
    String idColumn = null;
    String idProperty = null;
    StringBuilder set = new StringBuilder();
    set.append("<trim prefix=\"SET\" suffixOverrides=\",\">");
    for (ColumnMapper column : columnMappers) {
        if (!column.isUpdatable()) {
            continue;
        }
        if (column.isId()) {
            idColumn = column.getColumn();
            idProperty = column.getProperty();
        }
        String expr = SqlTemplate.wrapIfTag(column.getProperty(), column.getColumn() + "=#{" + column.getProperty() + "}", !selective);
        set.append(expr);
        if (!selective)
            set.append(",");
    }
    if (!selective)
        set.deleteCharAt(set.length() - 1);
    set.append("</trim>");
    String sql = String.format(SqlTemplate.UPDATE_BY_KEY, tableMapper.getName(), set.toString(), idColumn, idProperty);
    return String.format(SqlTemplate.SCRIPT_TEMAPLATE, sql);
}
Also used : TableMapper(com.jeesuite.mybatis.crud.helper.TableMapper) ColumnMapper(com.jeesuite.mybatis.crud.helper.ColumnMapper)

Example 4 with TableMapper

use of com.jeesuite.mybatis.crud.helper.TableMapper in project jeesuite-libs by vakinge.

the class DeleteBuilder method buildDeleteSql.

public static String buildDeleteSql(EntityMapper entityMapper) {
    // 从表注解里获取表名等信息
    TableMapper tableMapper = entityMapper.getTableMapper();
    ColumnMapper idColumn = entityMapper.getIdColumn();
    // 根据字段注解和属性值联合生成sql语句
    BEGIN();
    DELETE_FROM(tableMapper.getName());
    WHERE(idColumn.getColumn() + "=#{" + idColumn.getProperty() + "}");
    return String.format(SqlTemplate.SCRIPT_TEMAPLATE, SQL());
}
Also used : TableMapper(com.jeesuite.mybatis.crud.helper.TableMapper) ColumnMapper(com.jeesuite.mybatis.crud.helper.ColumnMapper)

Aggregations

ColumnMapper (com.jeesuite.mybatis.crud.helper.ColumnMapper)4 TableMapper (com.jeesuite.mybatis.crud.helper.TableMapper)4