tk.mybatis.mapper.entity.EntityColumn Java Examples

The following examples show how to use tk.mybatis.mapper.entity.EntityColumn. 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: UpdateByDifferProvider.java    From Mapper with MIT License 6 votes vote down vote up
/**
 * 乐观锁字段条件
 *
 * @param entityClass
 * @return
 */
public String whereVersion(Class<?> entityClass) {
    Set<EntityColumn> columnSet = EntityHelper.getColumns(entityClass);
    boolean hasVersion = false;
    String result = "";
    for (EntityColumn column : columnSet) {
        if (column.getEntityField().isAnnotationPresent(Version.class)) {
            if (hasVersion) {
                throw new VersionException(entityClass.getCanonicalName() + " 中包含多个带有 @Version 注解的字段,一个类中只能存在一个带有 @Version 注解的字段!");
            }
            hasVersion = true;
            result = " AND " + column.getColumnEqualsHolder(NEWER);
        }
    }
    return result;
}
 
Example #2
Source File: SqlHelper.java    From Mapper with MIT License 6 votes vote down vote up
/**
 * 获取逻辑删除注解的列,若没有返回null
 *
 * @param entityClass
 * @return
 */
public static EntityColumn getLogicDeleteColumn(Class<?> entityClass) {
    EntityColumn logicDeleteColumn = null;
    Set<EntityColumn> columnSet = EntityHelper.getColumns(entityClass);
    boolean hasLogicDelete = false;
    for (EntityColumn column : columnSet) {
        if (column.getEntityField().isAnnotationPresent(LogicDelete.class)) {
            if (hasLogicDelete) {
                throw new LogicDeleteException(entityClass.getCanonicalName() + " 中包含多个带有 @LogicDelete 注解的字段,一个类中只能存在一个带有 @LogicDelete 注解的字段!");
            }
            hasLogicDelete = true;
            logicDeleteColumn = column;
        }
    }
    return logicDeleteColumn;
}
 
Example #3
Source File: SelectPartialMapperProvider.java    From BlogManagePlatform with Apache License 2.0 6 votes vote down vote up
public String partialByIds(MappedStatement ms) {
	final Class<?> entityClass = getEntityClass(ms);
	String tableName = tableName(entityClass);
	StringBuilder sql = new StringBuilder();
	sql.append("SELECT ");
	sql.append(tableName).append(".${fieldName} ");
	sql.append(SqlHelper.fromTable(entityClass, tableName));
	Set<EntityColumn> columnList = EntityHelper.getPKColumns(entityClass);
	if (columnList.size() == 1) {
		EntityColumn column = columnList.iterator().next();
		sql.append(" where ");
		sql.append(column.getColumn());
		sql.append(" in ");
		sql.append("<foreach collection=\"ids\" item=\"item\" index=\"index\" open=\"(\" close=\")\" separator=\",\">");
		sql.append(" #{item} ");
		sql.append("</foreach>");
	} else {
		throw new MapperException("继承 selectByIds 方法的实体类[" + entityClass.getCanonicalName() + "]中必须有且只有一个带有 @Id 注解的字段");
	}
	return sql.toString();
}
 
Example #4
Source File: SqlHelper.java    From Mapper with MIT License 6 votes vote down vote up
/**
 * 乐观锁字段条件
 *
 * @param entityClass
 * @return
 */
public static String whereVersion(Class<?> entityClass) {
    Set<EntityColumn> columnSet = EntityHelper.getColumns(entityClass);
    boolean hasVersion = false;
    String result = "";
    for (EntityColumn column : columnSet) {
        if (column.getEntityField().isAnnotationPresent(Version.class)) {
            if (hasVersion) {
                throw new VersionException(entityClass.getCanonicalName() + " 中包含多个带有 @Version 注解的字段,一个类中只能存在一个带有 @Version 注解的字段!");
            }
            hasVersion = true;
            result = " AND " + column.getColumnEqualsHolder();
        }
    }
    return result;
}
 
Example #5
Source File: IdsMapperProvider.java    From BlogManagePlatform with Apache License 2.0 6 votes vote down vote up
/**
 * 批量删除
 * @author Frodez
 * @date 2019-12-25
 */
public String deleteByIds(MappedStatement ms) {
	final Class<?> entityClass = getEntityClass(ms);
	StringBuilder sql = new StringBuilder();
	sql.append(SqlHelper.deleteFromTable(entityClass, tableName(entityClass)));
	Set<EntityColumn> columnList = EntityHelper.getPKColumns(entityClass);
	if (columnList.size() == 1) {
		EntityColumn column = columnList.iterator().next();
		sql.append(" where ");
		sql.append(column.getColumn());
		sql.append(" in ");
		sql.append("<foreach collection=\"ids\" item=\"item\" index=\"index\" open=\"(\" close=\")\" separator=\",\">");
		sql.append(" #{item} ");
		sql.append("</foreach>");
	} else {
		throw new MapperException("继承 deleteByIds 方法的实体类[" + entityClass.getCanonicalName() + "]中必须有且只有一个带有 @Id 注解的字段");
	}
	return sql.toString();
}
 
Example #6
Source File: SqlHelper.java    From Mapper with MIT License 6 votes vote down vote up
/**
 * 判断自动==null的条件结构
 *
 * @param entityName
 * @param column
 * @param contents
 * @param empty
 * @return
 */
public static String getIfIsNull(String entityName, EntityColumn column, String contents, boolean empty) {
    StringBuilder sql = new StringBuilder();
    sql.append("<if test=\"");
    if (StringUtil.isNotEmpty(entityName)) {
        sql.append(entityName).append(".");
    }
    sql.append(column.getProperty()).append(" == null");
    if (empty && column.getJavaType().equals(String.class)) {
        sql.append(" or ");
        if (StringUtil.isNotEmpty(entityName)) {
            sql.append(entityName).append(".");
        }
        sql.append(column.getProperty()).append(" == '' ");
    }
    sql.append("\">");
    sql.append(contents);
    sql.append("</if>");
    return sql.toString();
}
 
Example #7
Source File: SqlHelper.java    From Mapper with MIT License 6 votes vote down vote up
/**
 * insert-values()列
 *
 * @param entityClass
 * @param skipId      是否从列中忽略id类型
 * @param notNull     是否判断!=null
 * @param notEmpty    是否判断String类型!=''
 * @return
 */
public static String insertValuesColumns(Class<?> entityClass, boolean skipId, boolean notNull, boolean notEmpty) {
    StringBuilder sql = new StringBuilder();
    sql.append("<trim prefix=\"VALUES (\" suffix=\")\" suffixOverrides=\",\">");
    //获取全部列
    Set<EntityColumn> columnSet = EntityHelper.getColumns(entityClass);
    //当某个列有主键策略时,不需要考虑他的属性是否为空,因为如果为空,一定会根据主键策略给他生成一个值
    for (EntityColumn column : columnSet) {
        if (!column.isInsertable()) {
            continue;
        }
        if (skipId && column.isId()) {
            continue;
        }
        if (notNull) {
            sql.append(SqlHelper.getIfNotNull(column, column.getColumnHolder() + ",", notEmpty));
        } else {
            sql.append(column.getColumnHolder() + ",");
        }
    }
    sql.append("</trim>");
    return sql.toString();
}
 
Example #8
Source File: SqlHelper.java    From Mapper with MIT License 6 votes vote down vote up
/**
 * insert table()列
 *
 * @param entityClass
 * @param skipId      是否从列中忽略id类型
 * @param notNull     是否判断!=null
 * @param notEmpty    是否判断String类型!=''
 * @return
 */
public static String insertColumns(Class<?> entityClass, boolean skipId, boolean notNull, boolean notEmpty) {
    StringBuilder sql = new StringBuilder();
    sql.append("<trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\">");
    //获取全部列
    Set<EntityColumn> columnSet = EntityHelper.getColumns(entityClass);
    //当某个列有主键策略时,不需要考虑他的属性是否为空,因为如果为空,一定会根据主键策略给他生成一个值
    for (EntityColumn column : columnSet) {
        if (!column.isInsertable()) {
            continue;
        }
        if (skipId && column.isId()) {
            continue;
        }
        if (notNull) {
            sql.append(SqlHelper.getIfNotNull(column, column.getColumn() + ",", notEmpty));
        } else {
            sql.append(column.getColumn() + ",");
        }
    }
    sql.append("</trim>");
    return sql.toString();
}
 
Example #9
Source File: IdsProvider.java    From Mapper with MIT License 6 votes vote down vote up
/**
 * 根据主键字符串进行查询,类中只有存在一个带有@Id注解的字段
 *
 * @param ms
 * @return
 */
public String selectByIds(MappedStatement ms) {
    final Class<?> entityClass = getEntityClass(ms);
    //将返回值修改为实体类型
    setResultType(ms, entityClass);
    StringBuilder sql = new StringBuilder();
    sql.append(SqlHelper.selectAllColumns(entityClass));
    sql.append(SqlHelper.fromTable(entityClass, tableName(entityClass)));
    Set<EntityColumn> columnList = EntityHelper.getPKColumns(entityClass);
    if (columnList.size() == 1) {
        EntityColumn column = columnList.iterator().next();
        sql.append(" where ");
        sql.append(column.getColumn());
        sql.append(" in (${_parameter})");
    } else {
        throw new MapperException("继承 selectByIds 方法的实体类[" + entityClass.getCanonicalName() + "]中必须只有一个带有 @Id 注解的字段");
    }
    return sql.toString();
}
 
Example #10
Source File: CountMapperProvider.java    From BlogManagePlatform with Apache License 2.0 6 votes vote down vote up
/**
 * 批量删除
 * @author Frodez
 * @date 2019-12-25
 */
public String countByIds(MappedStatement ms) {
	final Class<?> entityClass = getEntityClass(ms);
	StringBuilder sql = new StringBuilder();
	sql.append(SqlHelper.selectCount(entityClass));
	sql.append(SqlHelper.fromTable(entityClass, tableName(entityClass)));
	Set<EntityColumn> columnList = EntityHelper.getPKColumns(entityClass);
	if (columnList.size() == 1) {
		EntityColumn column = columnList.iterator().next();
		sql.append(" where ");
		sql.append(column.getColumn());
		sql.append(" in ");
		sql.append("<foreach collection=\"ids\" item=\"item\" index=\"index\" open=\"(\" close=\")\" separator=\",\">");
		sql.append(" #{item} ");
		sql.append("</foreach>");
	} else {
		throw new MapperException("继承 deleteByIds 方法的实体类[" + entityClass.getCanonicalName() + "]中必须有且只有一个带有 @Id 注解的字段");
	}
	return sql.toString();
}
 
Example #11
Source File: SqlHelper.java    From tk-mybatis with MIT License 6 votes vote down vote up
/**
 * 判断自动==null的条件结构
 *
 * @param entityName
 * @param column
 * @param contents
 * @param empty
 * @return
 */
public static String getIfIsNull(String entityName, EntityColumn column, String contents, boolean empty) {
    StringBuilder sql = new StringBuilder();
    sql.append("<if test=\"");
    if (StringUtil.isNotEmpty(entityName)) {
        sql.append(entityName).append(".");
    }
    sql.append(column.getProperty()).append(" == null");
    if (empty && column.getJavaType().equals(String.class)) {
        sql.append(" or ");
        if (StringUtil.isNotEmpty(entityName)) {
            sql.append(entityName).append(".");
        }
        sql.append(column.getProperty()).append(" == '' ");
    }
    sql.append("\">");
    sql.append(contents);
    sql.append("</if>");
    return sql.toString();
}
 
Example #12
Source File: IdsMapperProvider.java    From BlogManagePlatform with Apache License 2.0 6 votes vote down vote up
/**
 * 批量查询
 * @author Frodez
 * @date 2019-12-25
 */
public String selectByIds(MappedStatement ms) {
	final Class<?> entityClass = getEntityClass(ms);
	//将返回值修改为实体类型
	setResultType(ms, entityClass);
	StringBuilder sql = new StringBuilder();
	sql.append(SqlHelper.selectAllColumns(entityClass));
	sql.append(SqlHelper.fromTable(entityClass, tableName(entityClass)));
	Set<EntityColumn> columnList = EntityHelper.getPKColumns(entityClass);
	if (columnList.size() == 1) {
		EntityColumn column = columnList.iterator().next();
		sql.append(" where ");
		sql.append(column.getColumn());
		sql.append(" in ");
		sql.append("<foreach collection=\"ids\" item=\"item\" index=\"index\" open=\"(\" close=\")\" separator=\",\">");
		sql.append(" #{item} ");
		sql.append("</foreach>");
	} else {
		throw new MapperException("继承 selectByIds 方法的实体类[" + entityClass.getCanonicalName() + "]中必须有且只有一个带有 @Id 注解的字段");
	}
	return sql.toString();
}
 
Example #13
Source File: SqlHelper.java    From Mapper with MIT License 6 votes vote down vote up
/**
 * where主键条件
 *
 * @param entityClass
 * @param entityName
 * @param useVersion
 * @return
 */
public static String wherePKColumns(Class<?> entityClass, String entityName, boolean useVersion) {
    StringBuilder sql = new StringBuilder();
    boolean hasLogicDelete = hasLogicDeleteColumn(entityClass);

    sql.append("<where>");
    //获取全部列
    Set<EntityColumn> columnSet = EntityHelper.getPKColumns(entityClass);
    //当某个列有主键策略时,不需要考虑他的属性是否为空,因为如果为空,一定会根据主键策略给他生成一个值
    for (EntityColumn column : columnSet) {
        sql.append(" AND ").append(column.getColumnEqualsHolder(entityName));
    }
    if (useVersion) {
        sql.append(whereVersion(entityClass));
    }

    if (hasLogicDelete) {
        sql.append(whereLogicDelete(entityClass, false));
    }

    sql.append("</where>");
    return sql.toString();
}
 
Example #14
Source File: SqlHelper.java    From tk-mybatis with MIT License 6 votes vote down vote up
/**
 * insert table()列
 *
 * @param entityClass
 * @param skipId      是否从列中忽略id类型
 * @param notNull     是否判断!=null
 * @param notEmpty    是否判断String类型!=''
 * @return
 */
public static String insertColumns(Class<?> entityClass, boolean skipId, boolean notNull, boolean notEmpty) {
    StringBuilder sql = new StringBuilder();
    sql.append("<trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\">");
    //获取全部列
    Set<EntityColumn> columnList = EntityHelper.getColumns(entityClass);
    //当某个列有主键策略时,不需要考虑他的属性是否为空,因为如果为空,一定会根据主键策略给他生成一个值
    for (EntityColumn column : columnList) {
        if (!column.isInsertable()) {
            continue;
        }
        if (skipId && column.isId()) {
            continue;
        }
        if (notNull) {
            sql.append(SqlHelper.getIfNotNull(column, column.getColumn() + ",", notEmpty));
        } else {
            sql.append(column.getColumn() + ",");
        }
    }
    sql.append("</trim>");
    return sql.toString();
}
 
Example #15
Source File: SqlHelper.java    From tk-mybatis with MIT License 6 votes vote down vote up
/**
 * insert-values()列
 *
 * @param entityClass
 * @param skipId      是否从列中忽略id类型
 * @param notNull     是否判断!=null
 * @param notEmpty    是否判断String类型!=''
 * @return
 */
public static String insertValuesColumns(Class<?> entityClass, boolean skipId, boolean notNull, boolean notEmpty) {
    StringBuilder sql = new StringBuilder();
    sql.append("<trim prefix=\"VALUES (\" suffix=\")\" suffixOverrides=\",\">");
    //获取全部列
    Set<EntityColumn> columnList = EntityHelper.getColumns(entityClass);
    //当某个列有主键策略时,不需要考虑他的属性是否为空,因为如果为空,一定会根据主键策略给他生成一个值
    for (EntityColumn column : columnList) {
        if (!column.isInsertable()) {
            continue;
        }
        if (skipId && column.isId()) {
            continue;
        }
        if (notNull) {
            sql.append(SqlHelper.getIfNotNull(column, column.getColumnHolder() + ",", notEmpty));
        } else {
            sql.append(column.getColumnHolder() + ",");
        }
    }
    sql.append("</trim>");
    return sql.toString();
}
 
Example #16
Source File: SqlHelper.java    From tk-mybatis with MIT License 6 votes vote down vote up
/**
 * update set列
 *
 * @param entityClass
 * @param entityName  实体映射名
 * @param notNull     是否判断!=null
 * @param notEmpty    是否判断String类型!=''
 * @return
 */
public static String updateSetColumns(Class<?> entityClass, String entityName, boolean notNull, boolean notEmpty) {
    StringBuilder sql = new StringBuilder();
    sql.append("<set>");
    //获取全部列
    Set<EntityColumn> columnList = EntityHelper.getColumns(entityClass);
    //当某个列有主键策略时,不需要考虑他的属性是否为空,因为如果为空,一定会根据主键策略给他生成一个值
    for (EntityColumn column : columnList) {
        if (!column.isId() && column.isUpdatable()) {
            if (notNull) {
                sql.append(SqlHelper.getIfNotNull(entityName, column, column.getColumnEqualsHolder(entityName) + ",", notEmpty));
            } else {
                sql.append(column.getColumnEqualsHolder(entityName) + ",");
            }
        }
    }
    sql.append("</set>");
    return sql.toString();
}
 
Example #17
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 #18
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 #19
Source File: SpecialProvider.java    From tk-mybatis with MIT License 6 votes vote down vote up
/**
 * 批量插入
 *
 * @param ms
 */
public String insertList(MappedStatement ms) {
    final Class<?> entityClass = getEntityClass(ms);
    //开始拼sql
    StringBuilder sql = new StringBuilder();
    sql.append(SqlHelper.insertIntoTable(entityClass, tableName(entityClass)));
    sql.append(SqlHelper.insertColumns(entityClass, true, false, false));
    sql.append(" VALUES ");
    sql.append("<foreach collection=\"list\" item=\"record\" separator=\",\" >");
    sql.append("<trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\">");
    //获取全部列
    Set<EntityColumn> columnList = EntityHelper.getColumns(entityClass);
    //当某个列有主键策略时,不需要考虑他的属性是否为空,因为如果为空,一定会根据主键策略给他生成一个值
    for (EntityColumn column : columnList) {
        if (!column.isId() && column.isInsertable()) {
            sql.append(column.getColumnHolder("record") + ",");
        }
    }
    sql.append("</trim>");
    sql.append("</foreach>");
    return sql.toString();
}
 
Example #20
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 #21
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 #22
Source File: IdListProvider.java    From Mapper with MIT License 6 votes vote down vote up
/**
 * 拼接条件
 *
 * @param sql
 * @param entityClass
 */
private void appendWhereIdList(StringBuilder sql, Class<?> entityClass, boolean notEmpty){
    Set<EntityColumn> columnList = EntityHelper.getPKColumns(entityClass);
    if (columnList.size() == 1) {
        EntityColumn column = columnList.iterator().next();
        if(notEmpty){
            sql.append("<bind name=\"notEmptyListCheck\" value=\"@tk.mybatis.mapper.additional.idlist.IdListProvider@notEmpty(");
            sql.append("idList, 'idList 不能为空')\"/>");
        }
        sql.append("<where>");
        sql.append("<foreach collection=\"idList\" item=\"id\" separator=\",\" open=\"");
        sql.append(column.getColumn());
        sql.append(" in ");
        sql.append("(\" close=\")\">");
        sql.append("#{id}");
        sql.append("</foreach>");
        sql.append("</where>");
    } else {
        throw new MapperException("继承 ByIdList 方法的实体类[" + entityClass.getCanonicalName() + "]中必须只有一个带有 @Id 注解的字段");
    }
}
 
Example #23
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 #24
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 #25
Source File: OGNL.java    From Mapper with MIT License 6 votes vote down vote up
/**
 * 拼接逻辑删除字段的未删除查询条件
 *
 * @param parameter
 * @return
 */
public static String andNotLogicDelete(Object parameter) {
    String result = "";
    if (parameter instanceof Example) {
        Example example = (Example) parameter;
        Map<String, EntityColumn> propertyMap = example.getPropertyMap();

        for (Map.Entry<String, EntityColumn> entry: propertyMap.entrySet()) {
            EntityColumn column = entry.getValue();
            if (column.getEntityField().isAnnotationPresent(LogicDelete.class)) {
                // 未逻辑删除的条件
                result = column.getColumn() + " = " + SqlHelper.getLogicDeletedValue(column, false);

                // 如果Example中有条件,则拼接" and ",
                // 如果是空的oredCriteria,则where中只有逻辑删除注解的未删除条件
                if (example.getOredCriteria() != null && example.getOredCriteria().size() != 0) {
                    result += " and ";
                }
            }
        }
    }
    return result;
}
 
Example #26
Source File: SqlHelper.java    From Mapper with MIT License 6 votes vote down vote up
/**
 * 判断自动!=null的条件结构
 *
 * @param entityName
 * @param column
 * @param contents
 * @param empty
 * @return
 */
public static String getIfNotNull(String entityName, EntityColumn column, String contents, boolean empty) {
    StringBuilder sql = new StringBuilder();
    sql.append("<if test=\"");
    if (StringUtil.isNotEmpty(entityName)) {
        sql.append(entityName).append(".");
    }
    sql.append(column.getProperty()).append(" != null");
    if (empty && column.getJavaType().equals(String.class)) {
        sql.append(" and ");
        if (StringUtil.isNotEmpty(entityName)) {
            sql.append(entityName).append(".");
        }
        sql.append(column.getProperty()).append(" != '' ");
    }
    sql.append("\">");
    sql.append(contents);
    sql.append("</if>");
    return sql.toString();
}
 
Example #27
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 #28
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 #29
Source File: HsqldbProvider.java    From Mapper with MIT License 5 votes vote down vote up
/**
 * 分页查询
 * @param ms
 * @return
 */
public SqlNode selectPage(MappedStatement ms) {
    Class<?> entityClass = getEntityClass(ms);
    //修改返回值类型为实体类型
    setResultType(ms, entityClass);

    List<SqlNode> sqlNodes = new ArrayList<SqlNode>();
    //静态的sql部分:select column ... from table
    sqlNodes.add(new StaticTextSqlNode("SELECT "
            + EntityHelper.getSelectColumns(entityClass)
            + " FROM "
            + tableName(entityClass)));
    //获取全部列
    Set<EntityColumn> columnList = EntityHelper.getColumns(entityClass);
    List<SqlNode> ifNodes = new ArrayList<SqlNode>();
    boolean first = true;
    //对所有列循环,生成<if test="property!=null">[AND] column = #{property}</if>
    for (EntityColumn column : columnList) {
        StaticTextSqlNode columnNode
                = new StaticTextSqlNode((first ? "" : " AND ") + column.getColumn() + " = #{entity." + column.getProperty() + "} ");
        if (column.getJavaType().equals(String.class)) {
            ifNodes.add(new IfSqlNode(columnNode, "entity."+column.getProperty() + " != null and " + "entity."+column.getProperty() + " != '' "));
        } else {
            ifNodes.add(new IfSqlNode(columnNode, "entity."+column.getProperty() + " != null "));
        }
        first = false;
    }
    //增加entity判断
    IfSqlNode ifSqlNode = new IfSqlNode(new MixedSqlNode(ifNodes),"entity!=null");
    //将if添加到<where>
    sqlNodes.add(new WhereSqlNode(ms.getConfiguration(), ifSqlNode));
    //处理分页
    sqlNodes.add(new IfSqlNode(new StaticTextSqlNode(" LIMIT #{limit}"),"offset==0"));
    sqlNodes.add(new IfSqlNode(new StaticTextSqlNode(" LIMIT #{limit} OFFSET #{offset} "),"offset>0"));
    return new MixedSqlNode(sqlNodes);
}
 
Example #30
Source File: SqlHelper.java    From Mapper with MIT License 5 votes vote down vote up
/**
 * where所有列的条件,会判断是否!=null
 *
 * @param entityClass
 * @param empty
 * @param useVersion
 * @return
 */
public static String whereAllIfColumns(Class<?> entityClass, boolean empty, boolean useVersion) {
    StringBuilder sql = new StringBuilder();
    boolean hasLogicDelete = false;

    sql.append("<where>");
    //获取全部列
    Set<EntityColumn> columnSet = EntityHelper.getColumns(entityClass);
    EntityColumn logicDeleteColumn = SqlHelper.getLogicDeleteColumn(entityClass);
    //当某个列有主键策略时,不需要考虑他的属性是否为空,因为如果为空,一定会根据主键策略给他生成一个值
    for (EntityColumn column : columnSet) {
        if (!useVersion || !column.getEntityField().isAnnotationPresent(Version.class)) {
            // 逻辑删除,后面拼接逻辑删除字段的未删除条件
            if (logicDeleteColumn != null && logicDeleteColumn == column) {
                hasLogicDelete = true;
                continue;
            }
            sql.append(getIfNotNull(column, " AND " + column.getColumnEqualsHolder(), empty));
        }
    }
    if (useVersion) {
        sql.append(whereVersion(entityClass));
    }
    if (hasLogicDelete) {
        sql.append(whereLogicDelete(entityClass, false));
    }

    sql.append("</where>");
    return sql.toString();
}