Search in sources :

Example 11 with PropertyColumnMapping

use of com.qiuyj.mybatis.PropertyColumnMapping 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 12 with PropertyColumnMapping

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

Example 13 with PropertyColumnMapping

use of com.qiuyj.mybatis.PropertyColumnMapping in project qiuyj-code by qiuyuanjun.

the class MysqlSqlBuilder method batchInsert.

@Override
public Object batchInsert(SqlInfo sqlInfo, Object args) {
    List list = (List) ParameterResolver.resolveParameter(args).getParameterValues()[0];
    if (list.isEmpty()) {
        throw new IllegalArgumentException("Method batchInsert parameter can not be an empty list");
    } else {
        // 由于批量增加的sql可能会很长,所以这里给一个较大的初始化值
        StringBuilder sqlBuilder = new StringBuilder(256);
        sqlBuilder.append("INSERT INTO ");
        sqlBuilder.append(sqlInfo.getTableName());
        StringJoiner joiner = new StringJoiner(",", "(", ")");
        for (String column : sqlInfo.getAllColumnsWithoutAlias()) {
            joiner.add(column);
        }
        sqlBuilder.append(joiner.toString());
        sqlBuilder.append(" VALUES ");
        StringJoiner grammaJoiner = new StringJoiner(",");
        StringJoiner valueJoiner = new StringJoiner(",", "(", ")");
        List<ParameterMapping> singleInstanceParameterMapping = new ArrayList<>(sqlInfo.getFieldCount());
        BatchInsertTypeHandler typeHandler = new BatchInsertTypeHandler(sqlInfo);
        ParameterMapping canonic = new ParameterMapping.Builder(sqlInfo.getConfiguration(), "list", typeHandler).build();
        for (PropertyColumnMapping propertyColumnMapping : sqlInfo.getPropertyColumnMappings()) {
            valueJoiner.add(AbstractCommonSqlBuilder.PREPARE_FLAG);
            singleInstanceParameterMapping.add(canonic);
        }
        List<ParameterMapping> allParameterMappings = new ArrayList<>(sqlInfo.getFieldCount() * list.size());
        String value = valueJoiner.toString();
        for (int i = 0; i < list.size(); i++) {
            grammaJoiner.add(value);
            allParameterMappings.addAll(singleInstanceParameterMapping);
        }
        sqlBuilder.append(grammaJoiner.toString());
        return new StaticSqlSource(sqlInfo.getConfiguration(), sqlBuilder.toString(), allParameterMappings);
    }
}
Also used : BatchInsertTypeHandler(com.qiuyj.mybatis.sqlbuild.typehandler.BatchInsertTypeHandler) ArrayList(java.util.ArrayList) PropertyColumnMapping(com.qiuyj.mybatis.PropertyColumnMapping) ParameterMapping(org.apache.ibatis.mapping.ParameterMapping) List(java.util.List) ArrayList(java.util.ArrayList) StaticSqlSource(org.apache.ibatis.builder.StaticSqlSource) StringJoiner(java.util.StringJoiner)

Example 14 with PropertyColumnMapping

use of com.qiuyj.mybatis.PropertyColumnMapping in project qiuyj-code by qiuyuanjun.

the class BatchInsertTypeHandler method setNonNullParameter.

@SuppressWarnings("unchecked")
@Override
public void setNonNullParameter(PreparedStatement ps, int i, List parameter, JdbcType jdbcType) throws SQLException {
    // 首先判断当前占位符是属于List集合的第几个元素
    int idx = i - 1;
    int instanceIdx = idx / sqlInfo.getFieldCount();
    // 得到当前设置的值是实体类里面属性的第几个位置
    int parameterIdx = idx % sqlInfo.getFieldCount();
    // 得到当前设置的实体类
    Object obj = parameter.get(instanceIdx);
    // 得到对应实体类的反射对象(MetaObject)方便后面获取值
    MetaObject objMeta = sqlInfo.getConfiguration().newMetaObject(obj);
    PropertyColumnMapping pcm = sqlInfo.getPropertyColumnMappings().get(parameterIdx);
    Object value = objMeta.getValue(pcm.getJavaClassPropertyName());
    /*
     * 由于构建这个框架的mysql驱动版本较高,直接使用的是java8构建的
     * 所以mysql驱动原生支持java8的时间日期api的setObject方法参数
     * 但是考虑到有些人的mysql驱动不会这么高,所以这里也一样
     * 对于java8的时间日期的值显示调用对应的TypeHandler处理
     */
    if (pcm.getTypeHandler() instanceof EnumOrdinalTypeHandler || pcm.getTypeHandler() instanceof EnumTypeHandler || isJava8DateTimeApi(value)) {
        pcm.getTypeHandler().setParameter(ps, i, value, pcm.getJdbcType());
    } else // 否则直接setObject即可
    {
        ps.setObject(i, value);
    }
}
Also used : PropertyColumnMapping(com.qiuyj.mybatis.PropertyColumnMapping) MetaObject(org.apache.ibatis.reflection.MetaObject) MetaObject(org.apache.ibatis.reflection.MetaObject) EnumOrdinalTypeHandler(org.apache.ibatis.type.EnumOrdinalTypeHandler) EnumTypeHandler(org.apache.ibatis.type.EnumTypeHandler)

Aggregations

PropertyColumnMapping (com.qiuyj.mybatis.PropertyColumnMapping)14 ArrayList (java.util.ArrayList)8 ParameterMapping (org.apache.ibatis.mapping.ParameterMapping)8 SQL (org.apache.ibatis.jdbc.SQL)6 BeanExampleResolver (com.qiuyj.mybatis.BeanExampleResolver)4 StaticSqlSource (org.apache.ibatis.builder.StaticSqlSource)4 StaticTextSqlNode (org.apache.ibatis.scripting.xmltags.StaticTextSqlNode)3 Column (com.qiuyj.mybatis.annotation.Column)2 List (java.util.List)2 StringJoiner (java.util.StringJoiner)2 EnumOrdinalTypeHandler (org.apache.ibatis.type.EnumOrdinalTypeHandler)2 EnumTypeHandler (org.apache.ibatis.type.EnumTypeHandler)2 TypeHandlerRegistry (org.apache.ibatis.type.TypeHandlerRegistry)2 SqlInfo (com.qiuyj.mybatis.SqlInfo)1 Enumerated (com.qiuyj.mybatis.annotation.Enumerated)1 PrimaryKey (com.qiuyj.mybatis.annotation.PrimaryKey)1 Sequence (com.qiuyj.mybatis.key.Sequence)1 BatchInsertTypeHandler (com.qiuyj.mybatis.sqlbuild.typehandler.BatchInsertTypeHandler)1 MetaObject (org.apache.ibatis.reflection.MetaObject)1 TypeHandler (org.apache.ibatis.type.TypeHandler)1