Search in sources :

Example 1 with EntityMetadata

use of com.mendmix.mybatis.metadata.EntityMetadata in project jeesuite-libs by vakinge.

the class UpdateWithVersionProvider method updateByPrimaryKeyWithVersion.

public String updateByPrimaryKeyWithVersion(Object example) throws Exception {
    EntityMetadata entityMapper = MetadataHelper.getEntityMapper(example.getClass());
    Set<ColumnMetadata> columns = entityMapper.getColumns();
    SQL sql = new SQL().UPDATE(entityMapper.getTable().getName());
    StringBuilder setBuilder = new StringBuilder();
    StringBuilder whereBuilder = new StringBuilder();
    // 主键
    ColumnMetadata idColumn = entityMapper.getIdColumn();
    appendWhere(whereBuilder, idColumn);
    Object value;
    for (ColumnMetadata column : columns) {
        if (column.isId() || !column.isUpdatable())
            continue;
        value = MetadataHelper.getEntityField(entityMapper.getTable().getName(), column.getProperty()).get(example);
        if (value == null)
            continue;
        if (column.isVersionField()) {
            appendWhere(whereBuilder, column);
            // 乐观锁新值
            value = getNewVersionVal(column.getJavaType(), value);
            appendUpdateSet(setBuilder, column, value);
        } else {
            appendUpdateSet(setBuilder, column);
        }
    }
    sql.SET(setBuilder.toString());
    sql.WHERE(whereBuilder.toString());
    return sql.toString();
}
Also used : ColumnMetadata(com.mendmix.mybatis.metadata.ColumnMetadata) EntityMetadata(com.mendmix.mybatis.metadata.EntityMetadata) SQL(org.apache.ibatis.jdbc.SQL)

Example 2 with EntityMetadata

use of com.mendmix.mybatis.metadata.EntityMetadata in project jeesuite-libs by vakinge.

the class CountByExampleProvider method countByExample.

public String countByExample(Object example) throws Exception {
    EntityMetadata entityMapper = MetadataHelper.getEntityMapper(example.getClass());
    Set<ColumnMetadata> columns = entityMapper.getColumns();
    SQL sql = new SQL().SELECT("COUNT(1)").FROM(entityMapper.getTable().getName());
    Object value;
    StringBuilder whereBuilder = new StringBuilder();
    for (ColumnMetadata column : columns) {
        value = MetadataHelper.getEntityField(entityMapper.getTable().getName(), column.getProperty()).get(example);
        if (value == null)
            continue;
        appendWhere(whereBuilder, column);
    }
    if (whereBuilder.length() == 0)
        throw new IllegalArgumentException("至少包含一个查询条件");
    sql.WHERE(whereBuilder.toString());
    return sql.toString();
}
Also used : ColumnMetadata(com.mendmix.mybatis.metadata.ColumnMetadata) EntityMetadata(com.mendmix.mybatis.metadata.EntityMetadata) SQL(org.apache.ibatis.jdbc.SQL)

Example 3 with EntityMetadata

use of com.mendmix.mybatis.metadata.EntityMetadata in project jeesuite-libs by vakinge.

the class SelectByExampleProvider method selectByExample.

public String selectByExample(Object example) throws Exception {
    EntityMetadata entityMapper = MetadataHelper.getEntityMapper(example.getClass());
    Set<ColumnMetadata> columns = entityMapper.getColumns();
    SQL sql = new SQL().SELECT("*").FROM(entityMapper.getTable().getName());
    Object value;
    StringBuilder whereBuilder = new StringBuilder();
    for (ColumnMetadata column : columns) {
        value = MetadataHelper.getEntityField(entityMapper.getTable().getName(), column.getProperty()).get(example);
        if (value == null)
            continue;
        appendWhere(whereBuilder, column);
    }
    if (whereBuilder.length() == 0)
        throw new IllegalArgumentException("至少包含一个查询条件");
    // 
    // if(DbType.MYSQL.name().equalsIgnoreCase(MybatisConfigs.getDbType("default"))){
    // whereBuilder.append(" LIMIT 20000");
    // }
    sql.WHERE(whereBuilder.toString());
    return sql.toString();
}
Also used : ColumnMetadata(com.mendmix.mybatis.metadata.ColumnMetadata) EntityMetadata(com.mendmix.mybatis.metadata.EntityMetadata) SQL(org.apache.ibatis.jdbc.SQL)

Example 4 with EntityMetadata

use of com.mendmix.mybatis.metadata.EntityMetadata in project jeesuite-libs by vakinge.

the class AbstractMethodBuilder method build.

public void build(Configuration configuration, LanguageDriver languageDriver, MapperMetadata mapperMeta) {
    for (String name : methodNames()) {
        String msId = mapperMeta.getMapperClass().getName() + "." + name;
        // 从参数对象里提取注解信息
        EntityMetadata entityMapper = mapperMeta.getEntityMetadata();
        // 生成sql
        String sql = buildSQL(entityMapper, name.endsWith("Selective"));
        if (scriptWrapper()) {
            sql = String.format(SqlTemplate.SCRIPT_TEMAPLATE, sql);
        }
        SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, mapperMeta.getEntityClass());
        MappedStatement.Builder statementBuilder = new MappedStatement.Builder(configuration, msId, sqlSource, sqlCommandType());
        // 主键策略
        if (sqlCommandType() == SqlCommandType.INSERT) {
            KeyGenerator keyGenerator = entityMapper.autoId() ? new Jdbc3KeyGenerator() : new NoKeyGenerator();
            // 
            statementBuilder.keyGenerator(keyGenerator).keyProperty(// 
            entityMapper.getIdColumn().getProperty()).keyColumn(entityMapper.getIdColumn().getColumn());
        }
        MappedStatement statement = statementBuilder.build();
        // 
        setResultType(configuration, statement, mapperMeta.getEntityClass());
        configuration.addMappedStatement(statement);
    }
}
Also used : SqlSource(org.apache.ibatis.mapping.SqlSource) EntityMetadata(com.mendmix.mybatis.metadata.EntityMetadata) Jdbc3KeyGenerator(org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator) NoKeyGenerator(org.apache.ibatis.executor.keygen.NoKeyGenerator) MappedStatement(org.apache.ibatis.mapping.MappedStatement) NoKeyGenerator(org.apache.ibatis.executor.keygen.NoKeyGenerator) Jdbc3KeyGenerator(org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator) KeyGenerator(org.apache.ibatis.executor.keygen.KeyGenerator)

Aggregations

EntityMetadata (com.mendmix.mybatis.metadata.EntityMetadata)4 ColumnMetadata (com.mendmix.mybatis.metadata.ColumnMetadata)3 SQL (org.apache.ibatis.jdbc.SQL)3 Jdbc3KeyGenerator (org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator)1 KeyGenerator (org.apache.ibatis.executor.keygen.KeyGenerator)1 NoKeyGenerator (org.apache.ibatis.executor.keygen.NoKeyGenerator)1 MappedStatement (org.apache.ibatis.mapping.MappedStatement)1 SqlSource (org.apache.ibatis.mapping.SqlSource)1