Search in sources :

Example 1 with SQL

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

the class SqlProvider method delete.

public ReturnValueWrapper delete(MappedStatement ms, SqlInfo sqlInfo) {
    checkPrimaryKey(sqlInfo);
    String sql = new SQL().DELETE_FROM(sqlInfo.getTableName()).WHERE(sqlInfo.getPrimaryKeyCondition()).toString();
    return primaryKeyResolver(sqlInfo.getPrimaryKey(), ms.getConfiguration(), sql);
}
Also used : SQL(org.apache.ibatis.jdbc.SQL)

Example 2 with SQL

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

the class SqlProvider method update.

public ReturnValueWrapper update(MappedStatement ms, 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(ms.getConfiguration(), pcm.getJavaClassPropertyName(), pcm.getValue().getClass()).build());
            }
            sql.WHERE(sqlInfo.getPrimaryKey().getDatabaseColumnName() + " = ?");
            parameterMappings.add(new ParameterMapping.Builder(ms.getConfiguration(), sqlInfo.getPrimaryKey().getJavaClassPropertyName(), sqlInfo.getPrimaryKey().getTypeHandler()).build());
            return new ReturnValueWrapper(new StaticTextSqlNode(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) StaticTextSqlNode(org.apache.ibatis.scripting.xmltags.StaticTextSqlNode) SQL(org.apache.ibatis.jdbc.SQL)

Example 3 with SQL

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

the class SqlProvider method insert.

public ReturnValueWrapper insert(MappedStatement ms, SqlInfo sqlInfo) {
    // 不建议以下这种方式创建SQL,这样会增加编译之后的字节码文件数量
    // 因为这样会创建一个匿名内部类
    /*SQL sql = new SQL() {
      {
        INSERT_INTO(sqlInfo.getTableName());
        INTO_COLUMNS(sqlInfo.getAllColumnsWithoutAlias());
        String[] prepareColumnValues = new String[sqlInfo.getFiledCount()];
        Arrays.fill(prepareColumnValues, PREPARE_FLAG);
        INTO_VALUES(prepareColumnValues);
      }
    };*/
    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 = ms.getConfiguration().getTypeHandlerRegistry();
    ParameterMapping.Builder parameterBuilder;
    for (PropertyColumnMapping mapping : sqlInfo.getPropertyColumnMappings()) {
        parameterBuilder = new ParameterMapping.Builder(ms.getConfiguration(), mapping.getJavaClassPropertyName(), mapping.getTypeHandler());
        parameterMappings.add(parameterBuilder.build());
    }
    return new ReturnValueWrapper(new StaticTextSqlNode(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) StaticTextSqlNode(org.apache.ibatis.scripting.xmltags.StaticTextSqlNode) SQL(org.apache.ibatis.jdbc.SQL)

Example 4 with SQL

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

the class SqlProvider method selectList.

public ReturnValueWrapper selectList(MappedStatement ms, 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(ms.getConfiguration(), pcm.getJavaClassPropertyName(), pcm.getValue().getClass()).build());
            sql.WHERE(pcm.getDatabaseColumnName() + " = ?");
        }
        return new ReturnValueWrapper(new StaticTextSqlNode(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) StaticTextSqlNode(org.apache.ibatis.scripting.xmltags.StaticTextSqlNode) SQL(org.apache.ibatis.jdbc.SQL)

Example 5 with SQL

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

the class GSUserSqlProvider method insertSelective.

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

Aggregations

SQL (org.apache.ibatis.jdbc.SQL)12 PropertyColumnMapping (com.qiuyj.mybatis.PropertyColumnMapping)3 ArrayList (java.util.ArrayList)3 ParameterMapping (org.apache.ibatis.mapping.ParameterMapping)3 StaticTextSqlNode (org.apache.ibatis.scripting.xmltags.StaticTextSqlNode)3 BeanExampleResolver (com.qiuyj.mybatis.BeanExampleResolver)2 TypeHandlerRegistry (org.apache.ibatis.type.TypeHandlerRegistry)1