Java Code Examples for tk.mybatis.mapper.entity.EntityColumn#getColumn()

The following examples show how to use tk.mybatis.mapper.entity.EntityColumn#getColumn() . 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: 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 2
Source File: HsqldbProvider.java    From tk-mybatis 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 3
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 4
Source File: SqlHelper.java    From Mapper with MIT License 5 votes vote down vote up
/**
 * 获取逻辑删除注解的参数值
 *
 * @param column
 * @param isDeleted true:逻辑删除的值,false:未逻辑删除的值
 * @return
 */
public static int getLogicDeletedValue(EntityColumn column, boolean isDeleted) {
    if (!column.getEntityField().isAnnotationPresent(LogicDelete.class)) {
        throw new LogicDeleteException(column.getColumn() + " 没有 @LogicDelete 注解!");
    }
    LogicDelete logicDelete = column.getEntityField().getAnnotation(LogicDelete.class);
    if (isDeleted) {
        return logicDelete.isDeletedValue();
    }
    return logicDelete.notDeletedValue();
}
 
Example 5
Source File: SqlHelper.java    From Mapper with MIT License 3 votes vote down vote up
/**
 * 返回格式: column = value
 * <br>
 * 默认isDeletedValue = 1  notDeletedValue = 0
 * <br>
 * 则返回is_deleted = 1 或 is_deleted = 0
 * <br>
 * 若没有逻辑删除注解,则返回空字符串
 *
 * @param column
 * @param isDeleted true 已经逻辑删除  false 未逻辑删除
 */
public static String logicDeleteColumnEqualsValue(EntityColumn column, boolean isDeleted) {
    String result = "";
    if (column.getEntityField().isAnnotationPresent(LogicDelete.class)) {
        result = column.getColumn() + " = " + getLogicDeletedValue(column, isDeleted);
    }
    return result;
}
 
Example 6
Source File: SelectPropertyProvider.java    From Mapper with MIT License 2 votes vote down vote up
/**
 * 根据实体Class和属性名获取对应的表字段名
 * @param entityClass 实体Class对象
 * @param property 属性名
 * @return
 */
public static String getColumnByProperty(Class<?> entityClass, String property) {
    EntityTable entityTable = EntityHelper.getEntityTable(entityClass);
    EntityColumn entityColumn = entityTable.getPropertyMap().get(property);
    return entityColumn.getColumn();
}