tk.mybatis.mapper.mapperhelper.SqlHelper Java Examples

The following examples show how to use tk.mybatis.mapper.mapperhelper.SqlHelper. 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: 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 #2
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 #3
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 #4
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 #5
Source File: InMapperProvider.java    From BlogManagePlatform with Apache License 2.0 6 votes vote down vote up
public String selectIn(MappedStatement ms) {
	final Class<?> entityClass = getEntityClass(ms);
	//将返回值修改为实体类型
	setResultType(ms, entityClass);
	String tableName = tableName(entityClass);
	StringBuilder sql = new StringBuilder();
	sql.append(SqlHelper.selectAllColumns(entityClass));
	sql.append(SqlHelper.fromTable(entityClass, tableName));
	sql.append(" where ");
	sql.append(tableName).append(".${paramName}");
	sql.append(" in ");
	sql.append("<foreach collection=\"params\" item=\"item\" index=\"index\" open=\"(\" close=\")\" separator=\",\">");
	sql.append(" #{item} ");
	sql.append("</foreach>");
	return sql.toString();
}
 
Example #6
Source File: SelectOneByExampleProvider.java    From dynamic-data-source-demo with Apache License 2.0 6 votes vote down vote up
public String selectOneByExample(MappedStatement ms) {
    Class<?> entityClass = getEntityClass(ms);
    // 将返回值修改为实体类型
    setResultType(ms, entityClass);
    StringBuilder sql = new StringBuilder("SELECT ");
    if (isCheckExampleEntityClass()) {
        sql.append(SqlHelper.exampleCheck(entityClass));
    }
    sql.append("<if test=\"distinct\">distinct</if>");
    // 支持查询指定列
    sql.append(SqlHelper.exampleSelectColumns(entityClass));
    sql.append(SqlHelper.fromTable(entityClass, tableName(entityClass)));
    sql.append(SqlHelper.exampleWhereClause());
    sql.append(SqlHelper.exampleOrderBy(entityClass));
    sql.append(SqlHelper.exampleForUpdate());
    return sql.toString();
}
 
Example #7
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 #8
Source File: IdsProvider.java    From tk-mybatis with MIT License 6 votes vote down vote up
/**
 * 根据主键字符串进行删除,类中只有存在一个带有@Id注解的字段
 *
 * @param ms
 * @return
 */
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 (${_parameter})");
    } else {
        throw new MapperException("继承 deleteByIds 方法的实体类[" + entityClass.getCanonicalName() + "]中必须只有一个带有 @Id 注解的字段");
    }
    return sql.toString();
}
 
Example #9
Source File: IdsProvider.java    From tk-mybatis 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: ExampleProvider.java    From tk-mybatis with MIT License 6 votes vote down vote up
/**
 * 根据Example查询
 *
 * @param ms
 * @return
 */
public String selectByExample(MappedStatement ms) {
    Class<?> entityClass = getEntityClass(ms);
    //将返回值修改为实体类型
    setResultType(ms, entityClass);
    StringBuilder sql = new StringBuilder("SELECT ");
    if(isCheckExampleEntityClass()){
        sql.append(SqlHelper.exampleCheck(entityClass));
    }
    sql.append("<if test=\"distinct\">distinct</if>");
    //支持查询指定列
    sql.append(SqlHelper.exampleSelectColumns(entityClass));
    sql.append(SqlHelper.fromTable(entityClass, tableName(entityClass)));
    sql.append(SqlHelper.exampleWhereClause());
    sql.append(SqlHelper.exampleOrderBy(entityClass));
    sql.append(SqlHelper.exampleForUpdate());
    return sql.toString();
}
 
Example #11
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 #12
Source File: BaseDeleteProvider.java    From Mapper with MIT License 6 votes vote down vote up
/**
 * 通过条件删除
 *
 * @param ms
 * @return
 */
public String delete(MappedStatement ms) {
    Class<?> entityClass = getEntityClass(ms);
    StringBuilder sql = new StringBuilder();
    //如果设置了安全删除,就不允许执行不带查询条件的 delete 方法
    if (getConfig().isSafeDelete()) {
        sql.append(SqlHelper.notAllNullParameterCheck("_parameter", EntityHelper.getColumns(entityClass)));
    }
    // 如果是逻辑删除,则修改为更新表,修改逻辑删除字段的值
    if (SqlHelper.hasLogicDeleteColumn(entityClass)) {
        sql.append(SqlHelper.updateTable(entityClass, tableName(entityClass)));
        sql.append("<set>");
        sql.append(SqlHelper.logicDeleteColumnEqualsValue(entityClass, true));
        sql.append("</set>");
        MetaObjectUtil.forObject(ms).setValue("sqlCommandType", SqlCommandType.UPDATE);
    } else {
        sql.append(SqlHelper.deleteFromTable(entityClass, tableName(entityClass)));
    }
    sql.append(SqlHelper.whereAllIfColumns(entityClass, isNotEmpty()));
    return sql.toString();
}
 
Example #13
Source File: IdsProvider.java    From Mapper with MIT License 6 votes vote down vote up
/**
 * 根据主键字符串进行删除,类中只有存在一个带有@Id注解的字段
 *
 * @param ms
 * @return
 */
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 (${_parameter})");
    } else {
        throw new MapperException("继承 deleteByIds 方法的实体类[" + entityClass.getCanonicalName() + "]中必须只有一个带有 @Id 注解的字段");
    }
    return sql.toString();
}
 
Example #14
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 #15
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 #16
Source File: SelectPropertyProvider.java    From Mapper with MIT License 6 votes vote down vote up
/**
 * 根据属性查询,查询条件使用 between
 *
 * @param ms
 * @return
 */
public String selectBetweenByProperty(MappedStatement ms) {
    Class<?> entityClass = getEntityClass(ms);
    //修改返回值类型为实体类型
    setResultType(ms, entityClass);
    StringBuilder sql = new StringBuilder();
    sql.append(SqlHelper.selectAllColumns(entityClass));
    sql.append(SqlHelper.fromTable(entityClass, tableName(entityClass)));
    sql.append("<where>\n");
    String entityClassName = entityClass.getName();
    String propertyHelper = SelectPropertyProvider.class.getName();
    String sqlSegment =
            "${@" + propertyHelper + "@getColumnByProperty(@java.lang.Class@forName(\"" + entityClassName + "\"),"
                    + "@tk.mybatis.mapper.weekend.reflection.Reflections@fnToFieldName(fn))} "
                    + "between #{begin} and #{end}";
    sql.append(sqlSegment);
    // 逻辑删除的未删除查询条件
    sql.append(SqlHelper.whereLogicDelete(entityClass, false));
    sql.append("</where>");
    return sql.toString();
}
 
Example #17
Source File: SelectPropertyProvider.java    From Mapper with MIT License 6 votes vote down vote up
/**
 * 根据属性查询,查询条件使用 in
 *
 * @param ms
 * @return
 */
public String selectInByProperty(MappedStatement ms) {
    Class<?> entityClass = getEntityClass(ms);
    //修改返回值类型为实体类型
    setResultType(ms, entityClass);
    StringBuilder sql = new StringBuilder();
    sql.append(SqlHelper.selectAllColumns(entityClass));
    sql.append(SqlHelper.fromTable(entityClass, tableName(entityClass)));
    sql.append("<where>\n");
    String entityClassName = entityClass.getName();
    String propertyHelper = SelectPropertyProvider.class.getName();
    String sqlSegment =
            "${@" + propertyHelper + "@getColumnByProperty(@java.lang.Class@forName(\"" + entityClassName + "\"),"
                    +   "@tk.mybatis.mapper.weekend.reflection.Reflections@fnToFieldName(fn))} in"
                    +   "<foreach open=\"(\" close=\")\" separator=\",\" collection=\"values\" item=\"obj\">\n"
                    +      "#{obj}\n"
                    +   "</foreach>\n";
    sql.append(sqlSegment);
    // 逻辑删除的未删除查询条件
    sql.append(SqlHelper.whereLogicDelete(entityClass, false));
    sql.append("</where>");
    return sql.toString();
}
 
Example #18
Source File: AggregationProvider.java    From Mapper with MIT License 6 votes vote down vote up
/**
 * 根据Example查询总数
 *
 * @param ms
 * @return
 */
public String selectAggregationByExample(MappedStatement ms) {
    Class<?> entityClass = getEntityClass(ms);
    StringBuilder sql = new StringBuilder();
    if (isCheckExampleEntityClass()) {
        sql.append(SqlHelper.exampleCheck(entityClass));
    }
    sql.append("SELECT ${@tk.mybatis.mapper.additional.aggregation.AggregationProvider@aggregationSelectClause(");
    sql.append("@").append(entityClass.getCanonicalName()).append("@class");
    sql.append(", '").append(getConfig().getWrapKeyword()).append("'");
    sql.append(", aggregateCondition");
    sql.append(")} ");
    sql.append(SqlHelper.fromTable(entityClass, tableName(entityClass)));
    sql.append(SqlHelper.updateByExampleWhereClause());
    sql.append(" ${@tk.mybatis.mapper.additional.aggregation.AggregationProvider@aggregationGroupBy(");
    sql.append("@").append(entityClass.getCanonicalName()).append("@class");
    sql.append(", '").append(getConfig().getWrapKeyword()).append("'");
    sql.append(", aggregateCondition");
    sql.append(")} ");
    sql.append(SqlHelper.exampleOrderBy("example", entityClass));
    sql.append(SqlHelper.exampleForUpdate());
    return sql.toString();
}
 
Example #19
Source File: ExampleProvider.java    From Mapper with MIT License 6 votes vote down vote up
/**
 * 根据Example更新
 *
 * @param ms
 * @return
 */
public String updateByExample(MappedStatement ms) {
    Class<?> entityClass = getEntityClass(ms);
    StringBuilder sql = new StringBuilder();
    if (isCheckExampleEntityClass()) {
        sql.append(SqlHelper.exampleCheck(entityClass));
    }
    //安全更新,Example 必须包含条件
    if (getConfig().isSafeUpdate()) {
        sql.append(SqlHelper.exampleHasAtLeastOneCriteriaCheck("example"));
    }
    sql.append(SqlHelper.updateTable(entityClass, tableName(entityClass), "example"));
    sql.append(SqlHelper.updateSetColumnsIgnoreVersion(entityClass, "record", false, false));
    sql.append(SqlHelper.updateByExampleWhereClause());
    return sql.toString();
}
 
Example #20
Source File: ExampleProvider.java    From Mapper with MIT License 6 votes vote down vote up
/**
 * 根据Example更新非null字段
 *
 * @param ms
 * @return
 */
public String updateByExampleSelective(MappedStatement ms) {
    Class<?> entityClass = getEntityClass(ms);
    StringBuilder sql = new StringBuilder();
    if (isCheckExampleEntityClass()) {
        sql.append(SqlHelper.exampleCheck(entityClass));
    }
    //安全更新,Example 必须包含条件
    if (getConfig().isSafeUpdate()) {
        sql.append(SqlHelper.exampleHasAtLeastOneCriteriaCheck("example"));
    }
    sql.append(SqlHelper.updateTable(entityClass, tableName(entityClass), "example"));
    sql.append(SqlHelper.updateSetColumnsIgnoreVersion(entityClass, "record", true, isNotEmpty()));
    sql.append(SqlHelper.updateByExampleWhereClause());
    return sql.toString();
}
 
Example #21
Source File: ExampleProvider.java    From Mapper with MIT License 6 votes vote down vote up
/**
 * 根据Example查询
 *
 * @param ms
 * @return
 */
public String selectByExample(MappedStatement ms) {
    Class<?> entityClass = getEntityClass(ms);
    //将返回值修改为实体类型
    setResultType(ms, entityClass);
    StringBuilder sql = new StringBuilder("SELECT ");
    if (isCheckExampleEntityClass()) {
        sql.append(SqlHelper.exampleCheck(entityClass));
    }
    sql.append("<if test=\"distinct\">distinct</if>");
    //支持查询指定列
    sql.append(SqlHelper.exampleSelectColumns(entityClass));
    sql.append(SqlHelper.fromTable(entityClass, tableName(entityClass)));
    sql.append(SqlHelper.exampleWhereClause());
    sql.append(SqlHelper.exampleOrderBy(entityClass));
    sql.append(SqlHelper.exampleForUpdate());
    return sql.toString();
}
 
Example #22
Source File: ExampleProvider.java    From Mapper with MIT License 6 votes vote down vote up
/**
 * 根据Example删除
 *
 * @param ms
 * @return
 */
public String deleteByExample(MappedStatement ms) {
    Class<?> entityClass = getEntityClass(ms);
    StringBuilder sql = new StringBuilder();
    if (isCheckExampleEntityClass()) {
        sql.append(SqlHelper.exampleCheck(entityClass));
    }
    //如果设置了安全删除,就不允许执行不带查询条件的 delete 方法
    if (getConfig().isSafeDelete()) {
        sql.append(SqlHelper.exampleHasAtLeastOneCriteriaCheck("_parameter"));
    }
    if (SqlHelper.hasLogicDeleteColumn(entityClass)) {
        sql.append(SqlHelper.updateTable(entityClass, tableName(entityClass)));
        sql.append("<set>");
        sql.append(SqlHelper.logicDeleteColumnEqualsValue(entityClass, true));
        sql.append("</set>");
        MetaObjectUtil.forObject(ms).setValue("sqlCommandType", SqlCommandType.UPDATE);
    } else {
        sql.append(SqlHelper.deleteFromTable(entityClass, tableName(entityClass)));
    }
    sql.append(SqlHelper.exampleWhereClause());
    return sql.toString();
}
 
Example #23
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 #24
Source File: BaseDeleteProvider.java    From Mapper with MIT License 6 votes vote down vote up
/**
 * 通过主键删除
 *
 * @param ms
 */
public String deleteByPrimaryKey(MappedStatement ms) {
    final Class<?> entityClass = getEntityClass(ms);
    StringBuilder sql = new StringBuilder();
    if (SqlHelper.hasLogicDeleteColumn(entityClass)) {
        sql.append(SqlHelper.updateTable(entityClass, tableName(entityClass)));
        sql.append("<set>");
        sql.append(SqlHelper.logicDeleteColumnEqualsValue(entityClass, true));
        sql.append("</set>");
        MetaObjectUtil.forObject(ms).setValue("sqlCommandType", SqlCommandType.UPDATE);
    } else {
        sql.append(SqlHelper.deleteFromTable(entityClass, tableName(entityClass)));
    }
    sql.append(SqlHelper.wherePKColumns(entityClass));
    return sql.toString();
}
 
Example #25
Source File: ExampleProvider.java    From Mapper with MIT License 5 votes vote down vote up
/**
 * 根据Example查询总数
 *
 * @param ms
 * @return
 */
public String selectCountByExample(MappedStatement ms) {
    Class<?> entityClass = getEntityClass(ms);
    StringBuilder sql = new StringBuilder("SELECT ");
    if (isCheckExampleEntityClass()) {
        sql.append(SqlHelper.exampleCheck(entityClass));
    }
    sql.append(SqlHelper.exampleCountColumn(entityClass));
    sql.append(SqlHelper.fromTable(entityClass, tableName(entityClass)));
    sql.append(SqlHelper.exampleWhereClause());
    sql.append(SqlHelper.exampleForUpdate());
    return sql.toString();
}
 
Example #26
Source File: EqualMapperProvider.java    From BlogManagePlatform with Apache License 2.0 5 votes vote down vote up
public String updateEqualSelective(MappedStatement ms) {
	final Class<?> entityClass = getEntityClass(ms);
	String tableName = tableName(entityClass);
	StringBuilder sql = new StringBuilder();
	sql.append(SqlHelper.updateTable(entityClass, tableName));
	sql.append(SqlHelper.updateSetColumns(entityClass, null, true, isNotEmpty()));
	sql.append(" where ");
	sql.append(tableName).append(".${paramName}");
	sql.append(" = ");
	sql.append(" #{param}");
	return sql.toString();
}
 
Example #27
Source File: EqualMapperProvider.java    From BlogManagePlatform with Apache License 2.0 5 votes vote down vote up
public String updateEqual(MappedStatement ms) {
	final Class<?> entityClass = getEntityClass(ms);
	String tableName = tableName(entityClass);
	StringBuilder sql = new StringBuilder();
	sql.append(SqlHelper.updateTable(entityClass, tableName));
	sql.append(SqlHelper.updateSetColumns(entityClass, null, false, false));
	sql.append(" where ");
	sql.append(tableName).append(".${paramName}");
	sql.append(" = ");
	sql.append(" #{param}");
	return sql.toString();
}
 
Example #28
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 #29
Source File: EqualMapperProvider.java    From BlogManagePlatform with Apache License 2.0 5 votes vote down vote up
public String deleteEqual(MappedStatement ms) {
	final Class<?> entityClass = getEntityClass(ms);
	String tableName = tableName(entityClass);
	StringBuilder sql = new StringBuilder();
	sql.append(SqlHelper.deleteFromTable(entityClass, tableName));
	sql.append(" where ");
	sql.append(tableName).append(".${paramName}");
	sql.append(" = ");
	sql.append(" #{param}");
	return sql.toString();
}
 
Example #30
Source File: EqualMapperProvider.java    From BlogManagePlatform with Apache License 2.0 5 votes vote down vote up
public String selectEqual(MappedStatement ms) {
	final Class<?> entityClass = getEntityClass(ms);
	//将返回值修改为实体类型
	setResultType(ms, entityClass);
	String tableName = tableName(entityClass);
	StringBuilder sql = new StringBuilder();
	sql.append(SqlHelper.selectAllColumns(entityClass));
	sql.append(SqlHelper.fromTable(entityClass, tableName));
	sql.append(" where ");
	sql.append(tableName).append(".${paramName}");
	sql.append(" = ");
	sql.append(" #{param}");
	return sql.toString();
}