tk.mybatis.mapper.entity.EntityTable Java Examples

The following examples show how to use tk.mybatis.mapper.entity.EntityTable. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EntityHelper.java    From tk-mybatis with MIT License 6 votes vote down vote up
/**
 * 获取默认的orderby语句
 *
 * @param entityClass
 * @return
 */
public static String getOrderByClause(Class<?> entityClass) {
    EntityTable table = getEntityTable(entityClass);
    if (table.getOrderByClause() != null) {
        return table.getOrderByClause();
    }
    StringBuilder orderBy = new StringBuilder();
    for (EntityColumn column : table.getEntityClassColumns()) {
        if (column.getOrderBy() != null) {
            if (orderBy.length() != 0) {
                orderBy.append(",");
            }
            orderBy.append(column.getColumn()).append(" ").append(column.getOrderBy());
        }
    }
    table.setOrderByClause(orderBy.toString());
    return table.getOrderByClause();
}
 
Example #2
Source File: EntityHelper.java    From tk-mybatis with MIT License 6 votes vote down vote up
/**
 * 获取查询的Select
 *
 * @param entityClass
 * @return
 */
public static String getSelectColumns(Class<?> entityClass) {
    EntityTable entityTable = getEntityTable(entityClass);
    if (entityTable.getBaseSelect() != null) {
        return entityTable.getBaseSelect();
    }
    Set<EntityColumn> columnList = getColumns(entityClass);
    StringBuilder selectBuilder = new StringBuilder();
    boolean skipAlias = Map.class.isAssignableFrom(entityClass);
    for (EntityColumn entityColumn : columnList) {
        selectBuilder.append(entityColumn.getColumn());
        if (!skipAlias && !entityColumn.getColumn().equalsIgnoreCase(entityColumn.getProperty())) {
            //不等的时候分几种情况,例如`DESC`
            if (entityColumn.getColumn().substring(1, entityColumn.getColumn().length() - 1).equalsIgnoreCase(entityColumn.getProperty())) {
                selectBuilder.append(",");
            } else {
                selectBuilder.append(" AS ").append(entityColumn.getProperty()).append(",");
            }
        } else {
            selectBuilder.append(",");
        }
    }
    entityTable.setBaseSelect(selectBuilder.substring(0, selectBuilder.length() - 1));
    return entityTable.getBaseSelect();
}
 
Example #3
Source File: AggregationProvider.java    From Mapper with MIT License 6 votes vote down vote up
public static String aggregationSelectClause(Class<?> entityClass, String wrapKeyword, AggregateCondition condition) {
    Assert.notEmpty(condition.getAggregateProperty(), "aggregateProperty must have length; it must not be null or empty");
    Assert.notNull(condition.getAggregateType(), "aggregateType is required; it must not be null");
    EntityTable entityTable = EntityHelper.getEntityTable(entityClass);
    Map<String, EntityColumn> propertyMap = entityTable.getPropertyMap();
    StringBuilder selectBuilder = new StringBuilder();
    selectBuilder.append(condition.getAggregateType().name());
    String columnName = propertyMap.get(condition.getAggregateProperty()).getColumn();
    selectBuilder.append("(").append(columnName).append(")");
    selectBuilder.append(" AS ");
    if (StringUtil.isNotEmpty(condition.getAggregateAliasName())) {
        selectBuilder.append(condition.getAggregateAliasName());
    } else {
        selectBuilder.append(wrapKeyword(wrapKeyword, columnName));
    }
    if (condition.getGroupByProperties() != null && condition.getGroupByProperties().size() > 0) {
        for (String property : condition.getGroupByProperties()) {
            selectBuilder.append(", ");
            columnName = propertyMap.get(property).getColumn();
            selectBuilder.append(columnName).append(" AS ").append(wrapKeyword(wrapKeyword, columnName));
        }
    }
    return selectBuilder.toString();
}
 
Example #4
Source File: AggregationProvider.java    From Mapper with MIT License 6 votes vote down vote up
public static String aggregationGroupBy(Class<?> entityClass, String wrapKeyword, AggregateCondition condition) {
    if (condition.getGroupByProperties() != null && condition.getGroupByProperties().size() > 0) {
        EntityTable entityTable = EntityHelper.getEntityTable(entityClass);
        Map<String, EntityColumn> propertyMap = entityTable.getPropertyMap();
        StringBuilder groupByBuilder = new StringBuilder();
        for (String property : condition.getGroupByProperties()) {
            if (groupByBuilder.length() == 0) {
                groupByBuilder.append(" GROUP BY ");
            } else {
                groupByBuilder.append(", ");
            }
            groupByBuilder.append(propertyMap.get(property).getColumn());
        }
        return groupByBuilder.toString();
    }
    return "";
}
 
Example #5
Source File: IdTest.java    From Mapper with MIT License 6 votes vote down vote up
@Test
public void testSingleId(){
    EntityHelper.initEntityNameMap(UserSingleId.class, config);
    EntityTable entityTable = EntityHelper.getEntityTable(UserSingleId.class);
    Assert.assertNotNull(entityTable);

    Set<EntityColumn> columns = entityTable.getEntityClassColumns();
    Assert.assertEquals(1, columns.size());

    for (EntityColumn column : columns) {
        Assert.assertTrue(column.isId());
    }

    ResultMap resultMap = entityTable.getResultMap(configuration);
    Assert.assertEquals(1, resultMap.getResultMappings().size());
    Assert.assertTrue(resultMap.getResultMappings().get(0).getFlags().contains(ResultFlag.ID));

    Assert.assertEquals("<where> AND name = #{name}</where>", SqlHelper.wherePKColumns(UserSingleId.class));
}
 
Example #6
Source File: IdTest.java    From Mapper with MIT License 6 votes vote down vote up
@Test
public void testCompositeKeys(){
    EntityHelper.initEntityNameMap(UserCompositeKeys.class, config);
    EntityTable entityTable = EntityHelper.getEntityTable(UserCompositeKeys.class);
    Assert.assertNotNull(entityTable);

    Set<EntityColumn> columns = entityTable.getEntityClassColumns();
    Assert.assertEquals(2, columns.size());
    Assert.assertEquals(2, entityTable.getEntityClassPKColumns().size());

    for (EntityColumn column : columns) {
        Assert.assertTrue(column.isId());
    }

    ResultMap resultMap = entityTable.getResultMap(configuration);
    Assert.assertEquals(2, resultMap.getResultMappings().size());
    Assert.assertTrue(resultMap.getResultMappings().get(0).getFlags().contains(ResultFlag.ID));
    Assert.assertTrue(resultMap.getResultMappings().get(1).getFlags().contains(ResultFlag.ID));

    Assert.assertEquals("<where> AND name = #{name} AND orgId = #{orgId}</where>", SqlHelper.wherePKColumns(UserCompositeKeys.class));
}
 
Example #7
Source File: EntityHelper.java    From Mapper with MIT License 6 votes vote down vote up
/**
 * 获取查询的Select
 *
 * @param entityClass
 * @return
 */
public static String getSelectColumns(Class<?> entityClass) {
    EntityTable entityTable = getEntityTable(entityClass);
    if (entityTable.getBaseSelect() != null) {
        return entityTable.getBaseSelect();
    }
    Set<EntityColumn> columnList = getColumns(entityClass);
    StringBuilder selectBuilder = new StringBuilder();
    boolean skipAlias = Map.class.isAssignableFrom(entityClass);
    for (EntityColumn entityColumn : columnList) {
        selectBuilder.append(entityColumn.getColumn());
        if (!skipAlias && !entityColumn.getColumn().equalsIgnoreCase(entityColumn.getProperty())) {
            //不等的时候分几种情况,例如`DESC`
            if (entityColumn.getColumn().substring(1, entityColumn.getColumn().length() - 1).equalsIgnoreCase(entityColumn.getProperty())) {
                selectBuilder.append(",");
            } else {
                selectBuilder.append(" AS ").append(entityColumn.getProperty()).append(",");
            }
        } else {
            selectBuilder.append(",");
        }
    }
    entityTable.setBaseSelect(selectBuilder.substring(0, selectBuilder.length() - 1));
    return entityTable.getBaseSelect();
}
 
Example #8
Source File: DefaultEntityResolve.java    From Mapper with MIT License 6 votes vote down vote up
/**
 * 处理排序
 *
 * @param entityTable
 * @param field
 * @param entityColumn
 */
protected void processOrderBy(EntityTable entityTable, EntityField field, EntityColumn entityColumn) {
    String orderBy = "";
    if(field.isAnnotationPresent(OrderBy.class)){
        orderBy = field.getAnnotation(OrderBy.class).value();
        if ("".equals(orderBy)) {
            orderBy = "ASC";
        }
        log.warn(OrderBy.class + " is outdated, use " + Order.class + " instead!");
    }
    if (field.isAnnotationPresent(Order.class)) {
        Order order = field.getAnnotation(Order.class);
        if ("".equals(order.value()) && "".equals(orderBy)) {
            orderBy = "ASC";
        } else {
            orderBy = order.value();
        }
        entityColumn.setOrderPriority(order.priority());
    }
    if (StringUtil.isNotEmpty(orderBy)) {
        entityColumn.setOrderBy(orderBy);
    }
}
 
Example #9
Source File: NameStyleTest.java    From Mapper with MIT License 5 votes vote down vote up
@Test
public void testUppercase(){
    EntityHelper.initEntityNameMap(UserUppercase.class, config);
    EntityTable entityTable = EntityHelper.getEntityTable(UserUppercase.class);
    Assert.assertNotNull(entityTable);
    Assert.assertEquals("USERUPPERCASE", entityTable.getName());

    Set<EntityColumn> columns = entityTable.getEntityClassColumns();
    Assert.assertEquals(1, columns.size());

    for (EntityColumn column : columns) {
        Assert.assertEquals("USERNAME", column.getColumn());
        Assert.assertEquals("userName", column.getProperty());

        Assert.assertEquals("USERNAME = #{userName}", column.getColumnEqualsHolder());
        Assert.assertEquals("USERNAME = #{record.userName}", column.getColumnEqualsHolder("record"));
        Assert.assertEquals("#{userName}", column.getColumnHolder());
        Assert.assertEquals("#{record.userName}", column.getColumnHolder("record"));
        Assert.assertEquals("#{record.userName}", column.getColumnHolder("record", "suffix"));
        Assert.assertEquals("#{record.userNamesuffix},", column.getColumnHolder("record", "suffix", ","));
        Assert.assertNull(column.getTypeHandler());
    }

    ResultMap resultMap = entityTable.getResultMap(configuration);
    Assert.assertEquals("[USERNAME]", resultMap.getMappedColumns().toString());

    Assert.assertEquals(1, resultMap.getResultMappings().size());

    ResultMapping resultMapping = resultMap.getResultMappings().get(0);
    Assert.assertEquals("USERNAME", resultMapping.getColumn());
    Assert.assertEquals("userName", resultMapping.getProperty());
    Assert.assertNull(resultMapping.getJdbcType());
    Assert.assertEquals(StringTypeHandler.class, resultMapping.getTypeHandler().getClass());
}
 
Example #10
Source File: MapperTemplate.java    From tk-mybatis with MIT License 5 votes vote down vote up
/**
 * 获取实体类的表名
 *
 * @param entityClass
 * @return
 */
protected String tableName(Class<?> entityClass) {
    EntityTable entityTable = EntityHelper.getEntityTable(entityClass);
    String prefix = entityTable.getPrefix();
    if (StringUtil.isEmpty(prefix)) {
        //使用全局配置
        prefix = mapperHelper.getConfig().getPrefix();
    }
    if (StringUtil.isNotEmpty(prefix)) {
        return prefix + "." + entityTable.getName();
    }
    return entityTable.getName();
}
 
Example #11
Source File: NameStyleTest.java    From Mapper with MIT License 5 votes vote down vote up
@Test
public void testLowercase(){
    EntityHelper.initEntityNameMap(UserLowercase.class, config);
    EntityTable entityTable = EntityHelper.getEntityTable(UserLowercase.class);
    Assert.assertNotNull(entityTable);
    Assert.assertEquals("userlowercase", entityTable.getName());

    Set<EntityColumn> columns = entityTable.getEntityClassColumns();
    Assert.assertEquals(1, columns.size());

    for (EntityColumn column : columns) {
        Assert.assertEquals("username", column.getColumn());
        Assert.assertEquals("userName", column.getProperty());

        Assert.assertEquals("username = #{userName}", column.getColumnEqualsHolder());
        Assert.assertEquals("username = #{record.userName}", column.getColumnEqualsHolder("record"));
        Assert.assertEquals("#{userName}", column.getColumnHolder());
        Assert.assertEquals("#{record.userName}", column.getColumnHolder("record"));
        Assert.assertEquals("#{record.userName}", column.getColumnHolder("record", "suffix"));
        Assert.assertEquals("#{record.userNamesuffix},", column.getColumnHolder("record", "suffix", ","));
        Assert.assertNull(column.getTypeHandler());
    }

    ResultMap resultMap = entityTable.getResultMap(configuration);
    Assert.assertEquals("[USERNAME]", resultMap.getMappedColumns().toString());

    Assert.assertEquals(1, resultMap.getResultMappings().size());

    ResultMapping resultMapping = resultMap.getResultMappings().get(0);
    Assert.assertEquals("username", resultMapping.getColumn());
    Assert.assertEquals("userName", resultMapping.getProperty());
    Assert.assertNull(resultMapping.getJdbcType());
    Assert.assertEquals(StringTypeHandler.class, resultMapping.getTypeHandler().getClass());
}
 
Example #12
Source File: KeySqlTest.java    From Mapper with MIT License 5 votes vote down vote up
@Test
public void testUseGeneratedKeys(){
    EntityHelper.initEntityNameMap(UserJDBC.class, config);
    EntityTable entityTable = EntityHelper.getEntityTable(UserJDBC.class);
    Assert.assertNotNull(entityTable);

    Set<EntityColumn> columns = entityTable.getEntityClassColumns();
    Assert.assertEquals(1, columns.size());

    for (EntityColumn column : columns) {
        Assert.assertEquals("JDBC", column.getGenerator());
        Assert.assertTrue(column.isIdentity());
    }
}
 
Example #13
Source File: KeySqlTest.java    From Mapper with MIT License 5 votes vote down vote up
@Test
public void testDialect(){
    EntityHelper.initEntityNameMap(UserDialect.class, config);
    EntityTable entityTable = EntityHelper.getEntityTable(UserDialect.class);
    Assert.assertNotNull(entityTable);

    Set<EntityColumn> columns = entityTable.getEntityClassColumns();
    Assert.assertEquals(1, columns.size());

    for (EntityColumn column : columns) {
        Assert.assertEquals("SELECT LAST_INSERT_ID()", column.getGenerator());
        Assert.assertEquals(ORDER.AFTER, column.getOrder());
        Assert.assertTrue(column.isIdentity());
    }
}
 
Example #14
Source File: KeySqlTest.java    From Mapper with MIT License 5 votes vote down vote up
@Test
public void testSql(){
    EntityHelper.initEntityNameMap(UserSql.class, config);
    EntityTable entityTable = EntityHelper.getEntityTable(UserSql.class);
    Assert.assertNotNull(entityTable);

    Set<EntityColumn> columns = entityTable.getEntityClassColumns();
    Assert.assertEquals(1, columns.size());

    for (EntityColumn column : columns) {
        Assert.assertEquals("select seq.nextval from dual", column.getGenerator());
        Assert.assertEquals(ORDER.BEFORE, column.getOrder());
        Assert.assertTrue(column.isIdentity());
    }
}
 
Example #15
Source File: KeySqlTest.java    From Mapper with MIT License 5 votes vote down vote up
@Test
public void testAll(){
    EntityHelper.initEntityNameMap(UserAll.class, config);
    EntityTable entityTable = EntityHelper.getEntityTable(UserAll.class);
    Assert.assertNotNull(entityTable);

    Set<EntityColumn> columns = entityTable.getEntityClassColumns();
    Assert.assertEquals(1, columns.size());

    for (EntityColumn column : columns) {
        Assert.assertEquals("JDBC", column.getGenerator());
        Assert.assertTrue(column.isIdentity());
    }
}
 
Example #16
Source File: KeySqlTest.java    From Mapper with MIT License 5 votes vote down vote up
@Test
public void testAll2(){
    EntityHelper.initEntityNameMap(UserAll2.class, config);
    EntityTable entityTable = EntityHelper.getEntityTable(UserAll2.class);
    Assert.assertNotNull(entityTable);

    Set<EntityColumn> columns = entityTable.getEntityClassColumns();
    Assert.assertEquals(1, columns.size());

    for (EntityColumn column : columns) {
        Assert.assertEquals("SELECT LAST_INSERT_ID()", column.getGenerator());
        Assert.assertEquals(ORDER.AFTER, column.getOrder());
        Assert.assertTrue(column.isIdentity());
    }
}
 
Example #17
Source File: VersionTest.java    From Mapper with MIT License 5 votes vote down vote up
@Test
public void testVersion(){
    EntityHelper.initEntityNameMap(UserVersion.class, config);
    EntityTable entityTable = EntityHelper.getEntityTable(UserVersion.class);
    Assert.assertNotNull(entityTable);

    Set<EntityColumn> columns = entityTable.getEntityClassColumns();
    Assert.assertEquals(1, columns.size());

    for (EntityColumn column : columns) {
        Assert.assertTrue(column.getEntityField().isAnnotationPresent(Version.class));
    }
}
 
Example #18
Source File: VersionTest.java    From Mapper with MIT License 5 votes vote down vote up
@Test(expected = VersionException.class)
public void testVersionError(){
    EntityHelper.initEntityNameMap(UserVersionError.class, config);
    EntityTable entityTable = EntityHelper.getEntityTable(UserVersionError.class);
    Assert.assertNotNull(entityTable);
    SqlHelper.wherePKColumns(UserVersionError.class, true);
}
 
Example #19
Source File: ColumnTypeTest.java    From Mapper with MIT License 5 votes vote down vote up
@Test
public void testColumn(){
    EntityHelper.initEntityNameMap(UserColumn.class, config);
    EntityTable entityTable = EntityHelper.getEntityTable(UserColumn.class);
    Assert.assertNotNull(entityTable);

    Set<EntityColumn> columns = entityTable.getEntityClassColumns();
    Assert.assertEquals(1, columns.size());

    for (EntityColumn column : columns) {
        Assert.assertEquals("user_name", column.getColumn());
        Assert.assertEquals("name", column.getProperty());

        Assert.assertEquals("user_name = #{name}", column.getColumnEqualsHolder());
        Assert.assertEquals("user_name = #{record.name}", column.getColumnEqualsHolder("record"));
        Assert.assertEquals("#{name}", column.getColumnHolder());
        Assert.assertEquals("#{record.name}", column.getColumnHolder("record"));
        Assert.assertEquals("#{record.name}", column.getColumnHolder("record", "suffix"));
        Assert.assertEquals("#{record.namesuffix},", column.getColumnHolder("record", "suffix", ","));
        Assert.assertNull(column.getTypeHandler());
    }

    ResultMap resultMap = entityTable.getResultMap(configuration);
    Assert.assertEquals("[USER_NAME]", resultMap.getMappedColumns().toString());

    Assert.assertEquals(1, resultMap.getResultMappings().size());

    ResultMapping resultMapping = resultMap.getResultMappings().get(0);
    Assert.assertEquals("user_name", resultMapping.getColumn());
    Assert.assertEquals("name", resultMapping.getProperty());
    Assert.assertNull(resultMapping.getJdbcType());
    Assert.assertEquals(StringTypeHandler.class, resultMapping.getTypeHandler().getClass());
}
 
Example #20
Source File: ColumnTypeTest.java    From Mapper with MIT License 5 votes vote down vote up
@Test
public void testJdbcTypeVarchar(){
    EntityHelper.initEntityNameMap(UserJdbcTypeVarchar.class, config);
    EntityTable entityTable = EntityHelper.getEntityTable(UserJdbcTypeVarchar.class);
    Assert.assertNotNull(entityTable);

    Set<EntityColumn> columns = entityTable.getEntityClassColumns();
    Assert.assertEquals(1, columns.size());

    for (EntityColumn column : columns) {
        Assert.assertEquals("name", column.getColumn());
        Assert.assertEquals("name", column.getProperty());

        Assert.assertEquals("name = #{name, jdbcType=VARCHAR}", column.getColumnEqualsHolder());
        Assert.assertEquals("name = #{record.name, jdbcType=VARCHAR}", column.getColumnEqualsHolder("record"));
        Assert.assertEquals("#{name, jdbcType=VARCHAR}", column.getColumnHolder());
        Assert.assertEquals("#{record.name, jdbcType=VARCHAR}", column.getColumnHolder("record"));
        Assert.assertEquals("#{record.name, jdbcType=VARCHAR}", column.getColumnHolder("record", "suffix"));
        Assert.assertEquals("#{record.namesuffix, jdbcType=VARCHAR},", column.getColumnHolder("record", "suffix", ","));
        Assert.assertNull(column.getTypeHandler());
    }

    ResultMap resultMap = entityTable.getResultMap(configuration);
    Assert.assertEquals("[NAME]", resultMap.getMappedColumns().toString());

    Assert.assertEquals(1, resultMap.getResultMappings().size());

    ResultMapping resultMapping = resultMap.getResultMappings().get(0);
    Assert.assertEquals("name", resultMapping.getColumn());
    Assert.assertEquals("name", resultMapping.getProperty());
    Assert.assertNotNull(resultMapping.getJdbcType());
    Assert.assertEquals(JdbcType.VARCHAR, resultMapping.getJdbcType());
    Assert.assertEquals(StringTypeHandler.class, resultMapping.getTypeHandler().getClass());
}
 
Example #21
Source File: ColumnTypeTest.java    From Mapper with MIT License 5 votes vote down vote up
@Test
public void testJdbcTypeBlob(){
    EntityHelper.initEntityNameMap(UserJdbcTypeBlob.class, config);
    EntityTable entityTable = EntityHelper.getEntityTable(UserJdbcTypeBlob.class);
    Assert.assertNotNull(entityTable);

    Set<EntityColumn> columns = entityTable.getEntityClassColumns();
    Assert.assertEquals(1, columns.size());

    for (EntityColumn column : columns) {
        Assert.assertEquals("name", column.getColumn());
        Assert.assertEquals("name", column.getProperty());

        Assert.assertEquals("name = #{name, jdbcType=BLOB}", column.getColumnEqualsHolder());
        Assert.assertEquals("name = #{record.name, jdbcType=BLOB}", column.getColumnEqualsHolder("record"));
        Assert.assertEquals("#{name, jdbcType=BLOB}", column.getColumnHolder());
        Assert.assertEquals("#{record.name, jdbcType=BLOB}", column.getColumnHolder("record"));
        Assert.assertEquals("#{record.name, jdbcType=BLOB}", column.getColumnHolder("record", "suffix"));
        Assert.assertEquals("#{record.namesuffix, jdbcType=BLOB},", column.getColumnHolder("record", "suffix", ","));
        Assert.assertNull(column.getTypeHandler());
    }

    ResultMap resultMap = entityTable.getResultMap(configuration);
    Assert.assertEquals("[NAME]", resultMap.getMappedColumns().toString());

    Assert.assertEquals(1, resultMap.getResultMappings().size());

    ResultMapping resultMapping = resultMap.getResultMappings().get(0);
    Assert.assertEquals("name", resultMapping.getColumn());
    Assert.assertEquals("name", resultMapping.getProperty());
    Assert.assertNotNull(resultMapping.getJdbcType());
    Assert.assertEquals(JdbcType.BLOB, resultMapping.getJdbcType());
    Assert.assertEquals(StringTypeHandler.class, resultMapping.getTypeHandler().getClass());
}
 
Example #22
Source File: ColumnTypeTest.java    From Mapper with MIT License 5 votes vote down vote up
@Test
public void testTypehandler(){
    EntityHelper.initEntityNameMap(UserTypehandler.class, config);
    EntityTable entityTable = EntityHelper.getEntityTable(UserTypehandler.class);
    Assert.assertNotNull(entityTable);

    Set<EntityColumn> columns = entityTable.getEntityClassColumns();
    Assert.assertEquals(1, columns.size());

    for (EntityColumn column : columns) {
        Assert.assertEquals("name", column.getColumn());
        Assert.assertEquals("name", column.getProperty());

        Assert.assertEquals("name = #{name, typeHandler=org.apache.ibatis.type.BlobTypeHandler}", column.getColumnEqualsHolder());
        Assert.assertEquals("name = #{record.name, typeHandler=org.apache.ibatis.type.BlobTypeHandler}", column.getColumnEqualsHolder("record"));
        Assert.assertEquals("#{name, typeHandler=org.apache.ibatis.type.BlobTypeHandler}", column.getColumnHolder());
        Assert.assertEquals("#{record.name, typeHandler=org.apache.ibatis.type.BlobTypeHandler}", column.getColumnHolder("record"));
        Assert.assertEquals("#{record.name, typeHandler=org.apache.ibatis.type.BlobTypeHandler}", column.getColumnHolder("record", "suffix"));
        Assert.assertEquals("#{record.namesuffix, typeHandler=org.apache.ibatis.type.BlobTypeHandler},", column.getColumnHolder("record", "suffix", ","));
        Assert.assertNotNull(column.getTypeHandler());
    }

    ResultMap resultMap = entityTable.getResultMap(configuration);
    Assert.assertEquals("[NAME]", resultMap.getMappedColumns().toString());

    Assert.assertEquals(1, resultMap.getResultMappings().size());

    ResultMapping resultMapping = resultMap.getResultMappings().get(0);
    Assert.assertEquals("name", resultMapping.getColumn());
    Assert.assertEquals("name", resultMapping.getProperty());
    Assert.assertNull(resultMapping.getJdbcType());
    Assert.assertEquals(BlobTypeHandler.class, resultMapping.getTypeHandler().getClass());
}
 
Example #23
Source File: ColumnTypeTest.java    From Mapper with MIT License 5 votes vote down vote up
@Test
public void testAll(){
    EntityHelper.initEntityNameMap(UserAll.class, config);
    EntityTable entityTable = EntityHelper.getEntityTable(UserAll.class);
    Assert.assertNotNull(entityTable);

    Set<EntityColumn> columns = entityTable.getEntityClassColumns();
    Assert.assertEquals(1, columns.size());

    for (EntityColumn column : columns) {
        Assert.assertEquals("user_name", column.getColumn());
        Assert.assertEquals("name", column.getProperty());

        Assert.assertEquals("user_name = #{name, jdbcType=BLOB, typeHandler=org.apache.ibatis.type.BlobTypeHandler}", column.getColumnEqualsHolder());
        Assert.assertEquals("user_name = #{record.name, jdbcType=BLOB, typeHandler=org.apache.ibatis.type.BlobTypeHandler}", column.getColumnEqualsHolder("record"));
        Assert.assertEquals("#{name, jdbcType=BLOB, typeHandler=org.apache.ibatis.type.BlobTypeHandler}", column.getColumnHolder());
        Assert.assertEquals("#{record.name, jdbcType=BLOB, typeHandler=org.apache.ibatis.type.BlobTypeHandler}", column.getColumnHolder("record"));
        Assert.assertEquals("#{record.name, jdbcType=BLOB, typeHandler=org.apache.ibatis.type.BlobTypeHandler}", column.getColumnHolder("record", "suffix"));
        Assert.assertEquals("#{record.namesuffix, jdbcType=BLOB, typeHandler=org.apache.ibatis.type.BlobTypeHandler},", column.getColumnHolder("record", "suffix", ","));
        Assert.assertNotNull(column.getTypeHandler());
    }

    ResultMap resultMap = entityTable.getResultMap(configuration);
    Assert.assertEquals("[USER_NAME]", resultMap.getMappedColumns().toString());

    Assert.assertEquals(1, resultMap.getResultMappings().size());

    ResultMapping resultMapping = resultMap.getResultMappings().get(0);
    Assert.assertEquals("user_name", resultMapping.getColumn());
    Assert.assertEquals("name", resultMapping.getProperty());
    Assert.assertNotNull(resultMapping.getJdbcType());
    Assert.assertEquals(BlobTypeHandler.class, resultMapping.getTypeHandler().getClass());
}
 
Example #24
Source File: ColumnTest.java    From Mapper with MIT License 5 votes vote down vote up
@Test
public void testColumn(){
    EntityHelper.initEntityNameMap(UserColumn.class, config);
    EntityTable entityTable = EntityHelper.getEntityTable(UserColumn.class);
    Assert.assertNotNull(entityTable);

    Set<EntityColumn> columns = entityTable.getEntityClassColumns();
    Assert.assertEquals(1, columns.size());

    for (EntityColumn column : columns) {
        Assert.assertEquals("user_name", column.getColumn());
        Assert.assertEquals("name", column.getProperty());

        Assert.assertEquals("user_name = #{name}", column.getColumnEqualsHolder());
        Assert.assertEquals("user_name = #{record.name}", column.getColumnEqualsHolder("record"));
        Assert.assertEquals("#{name}", column.getColumnHolder());
        Assert.assertEquals("#{record.name}", column.getColumnHolder("record"));
        Assert.assertEquals("#{record.name}", column.getColumnHolder("record", "suffix"));
        Assert.assertEquals("#{record.namesuffix},", column.getColumnHolder("record", "suffix", ","));
        Assert.assertNull(column.getTypeHandler());
    }

    ResultMap resultMap = entityTable.getResultMap(configuration);
    Assert.assertEquals("[USER_NAME]", resultMap.getMappedColumns().toString());

    Assert.assertEquals(1, resultMap.getResultMappings().size());

    ResultMapping resultMapping = resultMap.getResultMappings().get(0);
    Assert.assertEquals("user_name", resultMapping.getColumn());
    Assert.assertEquals("name", resultMapping.getProperty());
    Assert.assertNull(resultMapping.getJdbcType());
    Assert.assertEquals(StringTypeHandler.class, resultMapping.getTypeHandler().getClass());
}
 
Example #25
Source File: NameStyleTest.java    From Mapper with MIT License 5 votes vote down vote up
@Test
public void testCamelhumpAndLowercase(){
    EntityHelper.initEntityNameMap(UserCamelhumpAndLowercase.class, config);
    EntityTable entityTable = EntityHelper.getEntityTable(UserCamelhumpAndLowercase.class);
    Assert.assertNotNull(entityTable);
    Assert.assertEquals("user_camelhump_and_lowercase", entityTable.getName());

    Set<EntityColumn> columns = entityTable.getEntityClassColumns();
    Assert.assertEquals(1, columns.size());

    for (EntityColumn column : columns) {
        Assert.assertEquals("user_name", column.getColumn());
        Assert.assertEquals("userName", column.getProperty());

        Assert.assertEquals("user_name = #{userName}", column.getColumnEqualsHolder());
        Assert.assertEquals("user_name = #{record.userName}", column.getColumnEqualsHolder("record"));
        Assert.assertEquals("#{userName}", column.getColumnHolder());
        Assert.assertEquals("#{record.userName}", column.getColumnHolder("record"));
        Assert.assertEquals("#{record.userName}", column.getColumnHolder("record", "suffix"));
        Assert.assertEquals("#{record.userNamesuffix},", column.getColumnHolder("record", "suffix", ","));
        Assert.assertNull(column.getTypeHandler());
    }

    ResultMap resultMap = entityTable.getResultMap(configuration);
    Assert.assertEquals("[USER_NAME]", resultMap.getMappedColumns().toString());

    Assert.assertEquals(1, resultMap.getResultMappings().size());

    ResultMapping resultMapping = resultMap.getResultMappings().get(0);
    Assert.assertEquals("user_name", resultMapping.getColumn());
    Assert.assertEquals("userName", resultMapping.getProperty());
    Assert.assertNull(resultMapping.getJdbcType());
    Assert.assertEquals(StringTypeHandler.class, resultMapping.getTypeHandler().getClass());
}
 
Example #26
Source File: MapperTemplate.java    From tk-mybatis with MIT License 5 votes vote down vote up
/**
 * 设置返回值类型 - 为了让typeHandler在select时有效,改为设置resultMap
 *
 * @param ms
 * @param entityClass
 */
protected void setResultType(MappedStatement ms, Class<?> entityClass) {
    EntityTable entityTable = EntityHelper.getEntityTable(entityClass);
    List<ResultMap> resultMaps = new ArrayList<ResultMap>();
    resultMaps.add(entityTable.getResultMap(ms.getConfiguration()));
    MetaObject metaObject = SystemMetaObject.forObject(ms);
    metaObject.setValue("resultMaps", Collections.unmodifiableList(resultMaps));
}
 
Example #27
Source File: EntityHelper.java    From tk-mybatis with MIT License 5 votes vote down vote up
/**
 * 获取表对象
 *
 * @param entityClass
 * @return
 */
public static EntityTable getEntityTable(Class<?> entityClass) {
    EntityTable entityTable = entityTableMap.get(entityClass);
    if (entityTable == null) {
        throw new MapperException("无法获取实体类" + entityClass.getCanonicalName() + "对应的表名!");
    }
    return entityTable;
}
 
Example #28
Source File: DefaultEntityResolve.java    From Mapper with MIT License 5 votes vote down vote up
/**
 * 处理主键策略
 *
 * @param entityTable
 * @param field
 * @param entityColumn
 */
protected void processKeyGenerator(EntityTable entityTable, EntityField field, EntityColumn entityColumn) {
    //KeySql 优先级最高
    if (field.isAnnotationPresent(KeySql.class)) {
        processKeySql(entityTable, entityColumn, field.getAnnotation(KeySql.class));
    } else if (field.isAnnotationPresent(GeneratedValue.class)) {
        //执行 sql - selectKey
        processGeneratedValue(entityTable, entityColumn, field.getAnnotation(GeneratedValue.class));
    }
}
 
Example #29
Source File: MapperTemplate.java    From Mapper with MIT License 5 votes vote down vote up
/**
 * 设置返回值类型 - 为了让typeHandler在select时有效,改为设置resultMap
 *
 * @param ms
 * @param entityClass
 */
protected void setResultType(MappedStatement ms, Class<?> entityClass) {
    EntityTable entityTable = EntityHelper.getEntityTable(entityClass);
    List<ResultMap> resultMaps = new ArrayList<ResultMap>();
    resultMaps.add(entityTable.getResultMap(ms.getConfiguration()));
    MetaObject metaObject = MetaObjectUtil.forObject(ms);
    metaObject.setValue("resultMaps", Collections.unmodifiableList(resultMaps));
}
 
Example #30
Source File: MapperTemplate.java    From Mapper with MIT License 5 votes vote down vote up
/**
 * 获取实体类的表名
 *
 * @param entityClass
 * @return
 */
protected String tableName(Class<?> entityClass) {
    EntityTable entityTable = EntityHelper.getEntityTable(entityClass);
    String prefix = entityTable.getPrefix();
    if (StringUtil.isEmpty(prefix)) {
        //使用全局配置
        prefix = mapperHelper.getConfig().getPrefix();
    }
    if (StringUtil.isNotEmpty(prefix)) {
        return prefix + "." + entityTable.getName();
    }
    return entityTable.getName();
}