Search in sources :

Example 1 with BeanExampleResolver

use of com.qiuyj.mybatis.BeanExampleResolver 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 2 with BeanExampleResolver

use of com.qiuyj.mybatis.BeanExampleResolver 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)

Aggregations

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