Search in sources :

Example 11 with SQL

use of org.apache.ibatis.jdbc.SQL in project mybatis-3 by mybatis.

the class OurSqlBuilder method buildSelectByIdAndNameMultipleParamAndProviderContext.

public String buildSelectByIdAndNameMultipleParamAndProviderContext(final Integer id, final String name, ProviderContext context) {
    final boolean containsLogicalDelete = context.getMapperMethod().getAnnotation(BaseMapper.ContainsLogicalDelete.class) != null;
    final String tableName = context.getMapperType().getAnnotation(BaseMapper.Meta.class).tableName();
    return new SQL() {

        {
            SELECT("*");
            FROM(tableName);
            if (id != null) {
                WHERE("id = #{param1}");
            }
            if (name != null) {
                WHERE("name like #{param2} || '%'");
            }
            if (!containsLogicalDelete) {
                WHERE("logical_delete = false");
            }
        }
    }.toString();
}
Also used : SQL(org.apache.ibatis.jdbc.SQL)

Example 12 with SQL

use of org.apache.ibatis.jdbc.SQL in project mybatis-3 by mybatis.

the class OurSqlBuilder method buildSelectByIdProviderContextOnly.

public String buildSelectByIdProviderContextOnly(ProviderContext context) {
    final boolean containsLogicalDelete = context.getMapperMethod().getAnnotation(BaseMapper.ContainsLogicalDelete.class) != null;
    final String tableName = context.getMapperType().getAnnotation(BaseMapper.Meta.class).tableName();
    return new SQL() {

        {
            SELECT("*");
            FROM(tableName);
            WHERE("id = #{id}");
            if (!containsLogicalDelete) {
                WHERE("logical_delete = ${Constants.LOGICAL_DELETE_OFF}");
            }
        }
    }.toString();
}
Also used : SQL(org.apache.ibatis.jdbc.SQL)

Example 13 with SQL

use of org.apache.ibatis.jdbc.SQL in project mybatis-3 by mybatis.

the class OurSqlBuilder method buildGetUsersByNameWithParamNameQueryUsingMap.

public String buildGetUsersByNameWithParamNameQueryUsingMap(Map<String, Object> params) {
    final String name = String.class.cast(params.get("name"));
    final String orderByColumn = String.class.cast(params.get("orderByColumn"));
    return new SQL() {

        {
            SELECT("*");
            FROM("users");
            if (name != null) {
                WHERE("name like #{param1} || '%'");
            }
            ORDER_BY(orderByColumn);
        }
    }.toString();
}
Also used : SQL(org.apache.ibatis.jdbc.SQL)

Example 14 with SQL

use of org.apache.ibatis.jdbc.SQL in project qiuyj-code by qiuyuanjun.

the class AbstractCommonSqlBuilder method insert.

@Override
public Object insert(SqlInfo sqlInfo, Object args) {
    String[] prepareColumnValues = new String[sqlInfo.getFieldCount()];
    Arrays.fill(prepareColumnValues, PREPARE_FLAG);
    String sql = new SQL().INSERT_INTO(sqlInfo.getTableName()).INTO_COLUMNS(sqlInfo.getAllColumnsWithoutAlias()).INTO_VALUES(prepareColumnValues).toString();
    List<ParameterMapping> parameterMappings = new ArrayList<>(sqlInfo.getFieldCount());
    TypeHandlerRegistry reg = sqlInfo.getConfiguration().getTypeHandlerRegistry();
    ParameterMapping.Builder parameterBuilder;
    for (PropertyColumnMapping mapping : sqlInfo.getPropertyColumnMappings()) {
        parameterBuilder = new ParameterMapping.Builder(sqlInfo.getConfiguration(), mapping.getJavaClassPropertyName(), mapping.getTypeHandler());
        parameterMappings.add(parameterBuilder.build());
    }
    return new StaticSqlSource(sqlInfo.getConfiguration(), sql, parameterMappings);
}
Also used : PropertyColumnMapping(com.qiuyj.mybatis.PropertyColumnMapping) TypeHandlerRegistry(org.apache.ibatis.type.TypeHandlerRegistry) ParameterMapping(org.apache.ibatis.mapping.ParameterMapping) ArrayList(java.util.ArrayList) StaticSqlSource(org.apache.ibatis.builder.StaticSqlSource) SQL(org.apache.ibatis.jdbc.SQL)

Example 15 with SQL

use of org.apache.ibatis.jdbc.SQL in project qiuyj-code by qiuyuanjun.

the class AbstractCommonSqlBuilder method update.

@Override
public Object update(SqlInfo sqlInfo, Object args) {
    checkPrimaryKey(sqlInfo);
    checkBeanType(sqlInfo.getBeanType(), args);
    BeanExampleResolver exampleResolver = new BeanExampleResolver(args, sqlInfo.getJavaProperties(), sqlInfo.getDatabaseColumns());
    if (!exampleResolver.hasPrimaryKeyAndNotDefault()) {
        throw new NoPrimaryKeyException("Primary key is default value.");
    } else {
        List<PropertyColumnMapping> nonNullColumns = exampleResolver.getWithoutPrimaryKey();
        if (nonNullColumns.isEmpty()) {
            throw new IllegalStateException("Please update at least one column");
        } else {
            List<ParameterMapping> parameterMappings = new ArrayList<>(nonNullColumns.size() + 1);
            SQL sql = new SQL();
            sql.UPDATE(sqlInfo.getTableName());
            for (PropertyColumnMapping pcm : nonNullColumns) {
                sql.SET(pcm.getDatabaseColumnName() + " = ?");
                parameterMappings.add(new ParameterMapping.Builder(sqlInfo.getConfiguration(), pcm.getJavaClassPropertyName(), pcm.getValue().getClass()).build());
            }
            sql.WHERE(sqlInfo.getPrimaryKey().getDatabaseColumnName() + " = ?");
            parameterMappings.add(new ParameterMapping.Builder(sqlInfo.getConfiguration(), sqlInfo.getPrimaryKey().getJavaClassPropertyName(), sqlInfo.getPrimaryKey().getTypeHandler()).build());
            return new StaticSqlSource(sqlInfo.getConfiguration(), sql.toString(), parameterMappings);
        }
    }
}
Also used : PropertyColumnMapping(com.qiuyj.mybatis.PropertyColumnMapping) BeanExampleResolver(com.qiuyj.mybatis.BeanExampleResolver) ParameterMapping(org.apache.ibatis.mapping.ParameterMapping) ArrayList(java.util.ArrayList) StaticSqlSource(org.apache.ibatis.builder.StaticSqlSource) SQL(org.apache.ibatis.jdbc.SQL)

Aggregations

SQL (org.apache.ibatis.jdbc.SQL)15 PropertyColumnMapping (com.qiuyj.mybatis.PropertyColumnMapping)6 ArrayList (java.util.ArrayList)6 ParameterMapping (org.apache.ibatis.mapping.ParameterMapping)6 BeanExampleResolver (com.qiuyj.mybatis.BeanExampleResolver)4 StaticSqlSource (org.apache.ibatis.builder.StaticSqlSource)3 StaticTextSqlNode (org.apache.ibatis.scripting.xmltags.StaticTextSqlNode)3 TypeHandlerRegistry (org.apache.ibatis.type.TypeHandlerRegistry)2