Search in sources :

Example 6 with SQL

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

the class AbstractCommonSqlBuilder method selectList.

@Override
public Object selectList(SqlInfo sqlInfo, Object args) {
    checkBeanType(sqlInfo.getBeanType(), args);
    BeanExampleResolver resolver = new BeanExampleResolver(args, sqlInfo.getJavaProperties(), sqlInfo.getDatabaseColumns());
    List<PropertyColumnMapping> exampleSelectList = resolver.selectExample();
    if (exampleSelectList.isEmpty()) {
        throw new IllegalStateException("Please specify at least one condition.");
    } else {
        SQL sql = new SQL().SELECT(sqlInfo.getAllColumnsWithAlias()).FROM(sqlInfo.getTableName());
        List<ParameterMapping> parameterMappings = new ArrayList<>(exampleSelectList.size());
        for (PropertyColumnMapping pcm : exampleSelectList) {
            parameterMappings.add(new ParameterMapping.Builder(sqlInfo.getConfiguration(), pcm.getJavaClassPropertyName(), pcm.getValue().getClass()).build());
            sql.WHERE(pcm.getDatabaseColumnName() + " = ?");
        }
        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)

Example 7 with SQL

use of org.apache.ibatis.jdbc.SQL in project QFGame by porschan.

the class GSUserSqlProvider method updateByPrimaryKeySelective.

public String updateByPrimaryKeySelective(GSUser record) {
    SQL sql = new SQL();
    sql.UPDATE("gs_user");
    if (record.getUsername() != null) {
        sql.SET("userName = #{username,jdbcType=VARCHAR}");
    }
    if (record.getUserpassword() != null) {
        sql.SET("userPassword = #{userpassword,jdbcType=VARCHAR}");
    }
    if (record.getUserphone() != null) {
        sql.SET("userPhone = #{userphone,jdbcType=VARCHAR}");
    }
    sql.WHERE("userID = #{userid,jdbcType=BIGINT}");
    return sql.toString();
}
Also used : SQL(org.apache.ibatis.jdbc.SQL)

Example 8 with SQL

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

the class OurSqlBuilder method buildSelectByIdAndNameMultipleParamAndProviderContextWithAtParam.

public String buildSelectByIdAndNameMultipleParamAndProviderContextWithAtParam(@Param("id") final Integer id, ProviderContext context, @Param("name") final String name) {
    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 = #{id}");
            }
            if (name != null) {
                WHERE("name like #{name} || '%'");
            }
            if (!containsLogicalDelete) {
                WHERE("logical_delete = false");
            }
        }
    }.toString();
}
Also used : SQL(org.apache.ibatis.jdbc.SQL)

Example 9 with SQL

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

the class OurSqlBuilder method buildGetUsersByNameUsingMap.

public String buildGetUsersByNameUsingMap(Map<String, Object> params) {
    final String name = String.class.cast(params.get("param1"));
    final String orderByColumn = String.class.cast(params.get("param2"));
    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 10 with SQL

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

the class OurSqlBuilder method buildSelectByNameOneParamAndProviderContext.

public String buildSelectByNameOneParamAndProviderContext(ProviderContext context, final String name) {
    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 (name != null) {
                WHERE("name like #{name} || '%'");
            }
            if (!containsLogicalDelete) {
                WHERE("logical_delete = ${LOGICAL_DELETE_OFF:0}");
            }
        }
    }.toString();
}
Also used : 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