Search in sources :

Example 1 with ColumnMapper

use of com.jeesuite.mybatis.crud.helper.ColumnMapper 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 ColumnMapper

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

the class GetByPrimaryKeyBuilder method getResultMap.

/**
 * 生成当前实体的resultMap对象
 *
 * @param configuration
 * @return
 */
public static ResultMap getResultMap(Configuration configuration, Class<?> entityClass) {
    List<ResultMapping> resultMappings = new ArrayList<ResultMapping>();
    Set<ColumnMapper> entityClassColumns = EntityHelper.getEntityMapper(entityClass).getColumnsMapper();
    for (ColumnMapper entityColumn : entityClassColumns) {
        ResultMapping.Builder builder = new ResultMapping.Builder(configuration, entityColumn.getProperty(), entityColumn.getColumn(), entityColumn.getJavaType());
        if (entityColumn.getJdbcType() != null) {
            builder.jdbcType(entityColumn.getJdbcType());
        }
        List<ResultFlag> flags = new ArrayList<ResultFlag>();
        if (entityColumn.isId()) {
            flags.add(ResultFlag.ID);
        }
        builder.flags(flags);
        builder.lazy(false);
        resultMappings.add(builder.build());
    }
    ResultMap.Builder builder = new ResultMap.Builder(configuration, "BaseResultMap", entityClass, resultMappings, true);
    return builder.build();
}
Also used : ResultMap(org.apache.ibatis.mapping.ResultMap) ResultMapping(org.apache.ibatis.mapping.ResultMapping) ArrayList(java.util.ArrayList) ResultFlag(org.apache.ibatis.mapping.ResultFlag) ColumnMapper(com.jeesuite.mybatis.crud.helper.ColumnMapper)

Example 3 with ColumnMapper

use of com.jeesuite.mybatis.crud.helper.ColumnMapper 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 4 with ColumnMapper

use of com.jeesuite.mybatis.crud.helper.ColumnMapper 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 5 with ColumnMapper

use of com.jeesuite.mybatis.crud.helper.ColumnMapper 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)5 TableMapper (com.jeesuite.mybatis.crud.helper.TableMapper)4 ArrayList (java.util.ArrayList)1 ResultFlag (org.apache.ibatis.mapping.ResultFlag)1 ResultMap (org.apache.ibatis.mapping.ResultMap)1 ResultMapping (org.apache.ibatis.mapping.ResultMapping)1