Java Code Examples for tk.mybatis.mapper.mapperhelper.EntityHelper#getColumns()

The following examples show how to use tk.mybatis.mapper.mapperhelper.EntityHelper#getColumns() . 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: 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 2
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 3
Source File: OGNL.java    From Mapper with MIT License 6 votes vote down vote up
/**
 * 检查 paremeter 对象中指定的 fields 是否全是 null,如果是则抛出异常
 *
 * @param parameter
 * @param fields
 * @return
 */
public static boolean notAllNullParameterCheck(Object parameter, String fields) {
    if (parameter != null) {
        try {
            Set<EntityColumn> columns = EntityHelper.getColumns(parameter.getClass());
            Set<String> fieldSet = new HashSet<String>(Arrays.asList(fields.split(",")));
            for (EntityColumn column : columns) {
                if (fieldSet.contains(column.getProperty())) {
                    Object value = column.getEntityField().getValue(parameter);
                    if (value != null) {
                        return true;
                    }
                }
            }
        } catch (Exception e) {
            throw new MapperException(SAFE_DELETE_ERROR, e);
        }
    }
    throw new MapperException(SAFE_DELETE_EXCEPTION);
}
 
Example 4
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 5
Source File: SpecialProvider.java    From Mapper with MIT License 5 votes vote down vote up
/**
 * 批量插入
 *
 * @param ms
 */
public String insertList(MappedStatement ms) {
    final Class<?> entityClass = getEntityClass(ms);
    //开始拼sql
    StringBuilder sql = new StringBuilder();
    sql.append("<bind name=\"listNotEmptyCheck\" value=\"@tk.mybatis.mapper.util.OGNL@notEmptyCollectionCheck(list, '" + ms.getId() + " 方法参数为空')\"/>");
    sql.append(SqlHelper.insertIntoTable(entityClass, tableName(entityClass), "list[0]"));
    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>");

    // 反射把MappedStatement中的设置主键名
    EntityHelper.setKeyProperties(EntityHelper.getPKColumns(entityClass), ms);

    return sql.toString();
}
 
Example 6
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 7
Source File: UpdateByDifferProvider.java    From Mapper with MIT License 5 votes vote down vote up
/**
 * update set列
 *
 * @param entityClass
 * @return
 */
public String updateSetColumnsByDiffer(Class<?> entityClass) {
    StringBuilder sql = new StringBuilder();
    sql.append("<set>");
    //获取全部列
    Set<EntityColumn> columnSet = EntityHelper.getColumns(entityClass);
    //对乐观锁的支持
    EntityColumn versionColumn = null;
    //当某个列有主键策略时,不需要考虑他的属性是否为空,因为如果为空,一定会根据主键策略给他生成一个值
    for (EntityColumn column : columnSet) {
        if (column.getEntityField().isAnnotationPresent(Version.class)) {
            if (versionColumn != null) {
                throw new VersionException(entityClass.getCanonicalName() + " 中包含多个带有 @Version 注解的字段,一个类中只能存在一个带有 @Version 注解的字段!");
            }
            versionColumn = column;
        }
        if (!column.isId() && column.isUpdatable()) {
            if (column == versionColumn) {
                Version version = versionColumn.getEntityField().getAnnotation(Version.class);
                String versionClass = version.nextVersion().getCanonicalName();
                //version = ${@tk.mybatis.mapper.version@nextVersionClass("versionClass", version)}
                sql.append(column.getColumn())
                    .append(" = ${@tk.mybatis.mapper.version.VersionUtil@nextVersion(")
                    .append("@").append(versionClass).append("@class, ")
                    .append(column.getProperty()).append(")},");
            } else {
                //if old.xx != newer.xx
                sql.append(getIfNotEqual(column, column.getColumnEqualsHolder(NEWER) + ","));
            }
        }
    }
    sql.append("</set>");
    return sql.toString();
}
 
Example 8
Source File: UpdateByPrimaryKeySelectiveForceProvider.java    From Mapper with MIT License 5 votes vote down vote up
/**
 * update set列
 *
 * @param entityClass
 * @param entityName  实体映射名
 * @param notNull     是否判断!=null
 * @param notEmpty    是否判断String类型!=''
 * @return
 */
public String updateSetColumnsForce(Class<?> entityClass, String entityName, boolean notNull, boolean notEmpty) {
    StringBuilder sql = new StringBuilder();
    sql.append("<set>");
    //获取全部列
    Set<EntityColumn> columnSet = EntityHelper.getColumns(entityClass);
    //对乐观锁的支持
    EntityColumn versionColumn = null;
    //当某个列有主键策略时,不需要考虑他的属性是否为空,因为如果为空,一定会根据主键策略给他生成一个值
    for (EntityColumn column : columnSet) {
        if (column.getEntityField().isAnnotationPresent(Version.class)) {
            if (versionColumn != null) {
                throw new VersionException(entityClass.getCanonicalName() + " 中包含多个带有 @Version 注解的字段,一个类中只能存在一个带有 @Version 注解的字段!");
            }
            versionColumn = column;
        }
        if (!column.isId() && column.isUpdatable()) {
            if (column == versionColumn) {
                Version version = versionColumn.getEntityField().getAnnotation(Version.class);
                String versionClass = version.nextVersion().getCanonicalName();
                //version = ${@tk.mybatis.mapper.version@nextVersionClass("versionClass", version)}
                sql.append(column.getColumn())
                    .append(" = ${@tk.mybatis.mapper.version.VersionUtil@nextVersion(")
                    .append("@").append(versionClass).append("@class, ")
                    .append(column.getProperty()).append(")},");
            } else if (notNull) {
                sql.append(this.getIfNotNull(entityName, column, column.getColumnEqualsHolder(entityName) + ",", notEmpty));
            } else {
                sql.append(column.getColumnEqualsHolder(entityName) + ",");
            }
        }
    }
    sql.append("</set>");
    return sql.toString();
}