org.apache.ibatis.builder.MapperBuilderAssistant Java Examples

The following examples show how to use org.apache.ibatis.builder.MapperBuilderAssistant. 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: MapperAnnotationBuilder.java    From mybatis with Apache License 2.0 6 votes vote down vote up
public MapperAnnotationBuilder(Configuration configuration, Class<?> type) {
  String resource = type.getName().replace('.', '/') + ".java (best guess)";
  this.assistant = new MapperBuilderAssistant(configuration, resource);
  this.configuration = configuration;
  this.type = type;

  sqlAnnotationTypes.add(Select.class);
  sqlAnnotationTypes.add(Insert.class);
  sqlAnnotationTypes.add(Update.class);
  sqlAnnotationTypes.add(Delete.class);

  sqlProviderAnnotationTypes.add(SelectProvider.class);
  sqlProviderAnnotationTypes.add(InsertProvider.class);
  sqlProviderAnnotationTypes.add(UpdateProvider.class);
  sqlProviderAnnotationTypes.add(DeleteProvider.class);
}
 
Example #2
Source File: MapperAnnotationBuilder.java    From mybaties with Apache License 2.0 6 votes vote down vote up
public MapperAnnotationBuilder(Configuration configuration, Class<?> type) {
  String resource = type.getName().replace('.', '/') + ".java (best guess)";
  this.assistant = new MapperBuilderAssistant(configuration, resource);
  this.configuration = configuration;
  this.type = type;

  sqlAnnotationTypes.add(Select.class);
  sqlAnnotationTypes.add(Insert.class);
  sqlAnnotationTypes.add(Update.class);
  sqlAnnotationTypes.add(Delete.class);

  sqlProviderAnnotationTypes.add(SelectProvider.class);
  sqlProviderAnnotationTypes.add(InsertProvider.class);
  sqlProviderAnnotationTypes.add(UpdateProvider.class);
  sqlProviderAnnotationTypes.add(DeleteProvider.class);
}
 
Example #3
Source File: SqlInjectorUtil.java    From albedo with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static String parseSql(MapperBuilderAssistant builderAssistant,
							  SqlCustomMethod sqlMethod, Class<?> modelClass, TableInfo tableInfo, String sqlWhereEntityWrapper) {
	String tableNameAlias = StringUtil.lowerCase(modelClass.getSimpleName()), tempNameAlias;
	TableInfo tableAlias;
	PropertyDescriptor[] ps = BeanUtil.getPropertyDescriptors(modelClass);
	StringBuffer sbSelectCoumns = new StringBuffer(SqlInjectorUtil.sqlSelectColumns(tableInfo, false, tableNameAlias, null)),
		sbLeftJoin = new StringBuffer(tableInfo.getTableName()).append(" `").append(tableNameAlias).append("`");
	for (PropertyDescriptor p : ps) {

		ManyToOne annotation = ClassUtil.findAnnotation(modelClass, p.getName(), ManyToOne.class);
		if (annotation != null) {
			tableAlias = TableInfoHelper.initTableInfo(builderAssistant, p.getPropertyType());
			sbSelectCoumns.append(",")
				.append(SqlInjectorUtil.sqlSelectColumns(tableAlias, false, p.getName(), p.getName()));
			sbLeftJoin.append(" LEFT JOIN ").append(tableAlias.getTableName()).append(" `").append(p.getName())
				.append("` ON `").append(tableNameAlias).append("`.").append(annotation.name())
				.append(" = `").append(p.getName()).append("`.").append(TreeEntity.F_SQL_ID);
		}
	}

	String sql = String.format(sqlMethod.getSql(),
		sbSelectCoumns.toString(),
		sbLeftJoin.toString(),
		sqlWhereEntityWrapper);
	return sql;
}
 
Example #4
Source File: AbstractSqlInjector.java    From Roothub with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * 根据 mapperClass 注入 SQL,需要检查 SQL 是否已注入(已经注入过不再注入)
 *
 * @param builderAssistant
 * @param mapperClass
 */
@Override
public void inspectInject(MapperBuilderAssistant builderAssistant, Class<?> mapperClass) {
    List<AbstractMethod> methodList = this.getMethodList();
    Assert.notEmpty(methodList);
    Class<?> modelClass = this.extractModelClass(mapperClass);
    methodList.forEach(m -> m.inject(builderAssistant, mapperClass, modelClass));
}
 
Example #5
Source File: XMLMapperBuilder.java    From mybatis with Apache License 2.0 5 votes vote down vote up
private XMLMapperBuilder(XPathParser parser, Configuration configuration, String resource, Map<String, XNode> sqlFragments) {
  super(configuration);
  this.builderAssistant = new MapperBuilderAssistant(configuration, resource);
  this.parser = parser;
  this.sqlFragments = sqlFragments;
  this.resource = resource;
}
 
Example #6
Source File: XMLMapperBuilder.java    From mybaties with Apache License 2.0 5 votes vote down vote up
private XMLMapperBuilder(XPathParser parser, Configuration configuration, String resource, Map<String, XNode> sqlFragments) {
  super(configuration);
  this.builderAssistant = new MapperBuilderAssistant(configuration, resource);
  this.parser = parser;
  this.sqlFragments = sqlFragments;
  this.resource = resource;
}
 
Example #7
Source File: XMLMapperBuilder.java    From Shop-for-JavaWeb with MIT License 5 votes vote down vote up
private XMLMapperBuilder(XPathParser parser, Configuration configuration,
		String resource, Map<String, XNode> sqlFragments) {
	super(configuration);
	this.builderAssistant = new MapperBuilderAssistant(configuration,
			resource);
	this.parser = parser;
	this.sqlFragments = sqlFragments;
	this.resource = resource;
}
 
Example #8
Source File: BaseMapperBuilder.java    From Roothub with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * 初始化各个属性
 * @param configuration
 * @param mapperClass
 */
public BaseMapperBuilder(Configuration configuration, Class<?> mapperClass) {
    super(configuration, mapperClass);
    String resource = mapperClass.getName().replace('.', '/') + ".java (best guess)";
    this.configuration = configuration;
    this.assistant = new MapperBuilderAssistant(configuration, resource);
    this.mapperClass = mapperClass;
    // 设置当前 Mapper 的全局命名空间
    this.assistant.setCurrentNamespace(mapperClass.getName());
}
 
Example #9
Source File: TableInfoBuilder.java    From Roothub with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * 初始化 TableInfo
 * @param builderAssistant
 * @param modelClass
 * @return
 */
public static synchronized TableInfo initTableInfo (MapperBuilderAssistant builderAssistant, Class<?> modelClass) {
    TableInfo tableInfo = TABLE_INFO_CACHE.get(modelClass);
    if (tableInfo != null) {
        return tableInfo;
    } else {
        tableInfo = new TableInfo(modelClass);
        initTableName(modelClass, tableInfo);
        initTableFields(modelClass, tableInfo);
        TABLE_INFO_CACHE.put(modelClass, tableInfo);
        return tableInfo;
    }
}
 
Example #10
Source File: AbstractMethod.java    From Roothub with GNU Affero General Public License v3.0 5 votes vote down vote up
public void inject(MapperBuilderAssistant builderAssistant, Class<?> mapperClass, Class<?> modelClass) {
    this.configuration = builderAssistant.getConfiguration();
    // 使用默认的语言驱动 XMLLanguageDriver
    this.languageDriver = this.configuration.getDefaultScriptingLanuageInstance();
    this.builderAssistant = builderAssistant;
    if (modelClass != null) {
        TableInfo tableInfo = TableInfoBuilder.initTableInfo(builderAssistant, modelClass);
        this.injectMappedStatement(mapperClass, modelClass, tableInfo);
    }
}
 
Example #11
Source File: ResultMapParser.java    From mybatis-jpa with Apache License 2.0 4 votes vote down vote up
public List<ResultMapping> resolveResultMappings(String resource, String id, Class<?> type) {
  List<ResultMapping> resultMappings = new ArrayList<>();

  MapperBuilderAssistant assistant = new MapperBuilderAssistant(configuration, resource);

  List<Field> fields = PersistentUtil.getPersistentFields(type);

  for (Field field : fields) {
    // java field name
    String property = field.getName();
    // sql column name
    String column = PersistentUtil.getColumnName(field);
    Class<?> javaType = field.getType();

    //resultMap is not need jdbcType
    JdbcType jdbcType = null;

    String nestedSelect = null;
    String nestedResultMap = null;
    if (PersistentUtil.isAssociationField(field)) {
      // OneToOne or OneToMany

      // mappedBy
      column = PersistentUtil.getMappedName(field);
      if (field.isAnnotationPresent(OneToOne.class)) {
        nestedResultMap = id + "_association[" + javaType.getSimpleName() + "]";
        registerResultMap(resolveResultMap(resource, nestedResultMap, javaType));
      }
      if (field.isAnnotationPresent(OneToMany.class)) {
        Type genericType = field.getGenericType();
        if (genericType instanceof ParameterizedType) {
          ParameterizedType pt = (ParameterizedType) genericType;
          Class<?> actualType = (Class<?>) pt.getActualTypeArguments()[0];
          // create resultMap with actualType
          nestedResultMap = id + "collection[" + actualType.getSimpleName() + "]";
          registerResultMap(resolveResultMap(resource, nestedResultMap, actualType));
        }
      }
    }

    String notNullColumn = null;
    String columnPrefix = null;
    String resultSet = null;
    String foreignColumn = null;
    // if primaryKey,then flags.add(ResultFlag.ID);
    List<ResultFlag> flags = new ArrayList<>();
    if (field.isAnnotationPresent(Id.class)) {
      flags.add(ResultFlag.ID);
    }
    // lazy or eager
    boolean lazy = false;
    // typeHandler
    Class<? extends TypeHandler<?>> typeHandlerClass = ColumnMetaResolver
        .resolveTypeHandler(field);

    ResultMapping resultMapping = assistant.buildResultMapping(type, property, column,
        javaType, jdbcType, nestedSelect, nestedResultMap, notNullColumn, columnPrefix,
        typeHandlerClass, flags, resultSet, foreignColumn, lazy);
    resultMappings.add(resultMapping);
  }
  return resultMappings;

}
 
Example #12
Source File: XMLIncludeTransformer.java    From mybaties with Apache License 2.0 4 votes vote down vote up
public XMLIncludeTransformer(Configuration configuration, MapperBuilderAssistant builderAssistant) {
  this.configuration = configuration;
  this.builderAssistant = builderAssistant;
}
 
Example #13
Source File: XMLStatementBuilder.java    From mybaties with Apache License 2.0 4 votes vote down vote up
public XMLStatementBuilder(Configuration configuration, MapperBuilderAssistant builderAssistant, XNode context) {
  this(configuration, builderAssistant, context, null);
}
 
Example #14
Source File: XMLStatementBuilder.java    From mybaties with Apache License 2.0 4 votes vote down vote up
public XMLStatementBuilder(Configuration configuration, MapperBuilderAssistant builderAssistant, XNode context, String databaseId) {
  super(configuration);
  this.builderAssistant = builderAssistant;
  this.context = context;
  this.requiredDatabaseId = databaseId;
}
 
Example #15
Source File: TransController.java    From das with Apache License 2.0 4 votes vote down vote up
private String createClassName(XMLMapperBuilder xmlMapperBuilder) throws NoSuchFieldException, IllegalAccessException {
    MapperBuilderAssistant mapperBuilderAssistant = get(xmlMapperBuilder, "builderAssistant");
    return Iterables.getLast(DOT_SPLITTER.splitToList(mapperBuilderAssistant.getCurrentNamespace()));
}
 
Example #16
Source File: XMLIncludeTransformer.java    From mybatis with Apache License 2.0 4 votes vote down vote up
public XMLIncludeTransformer(Configuration configuration, MapperBuilderAssistant builderAssistant) {
  this.configuration = configuration;
  this.builderAssistant = builderAssistant;
}
 
Example #17
Source File: XMLStatementBuilder.java    From mybatis with Apache License 2.0 4 votes vote down vote up
public XMLStatementBuilder(Configuration configuration, MapperBuilderAssistant builderAssistant, XNode context) {
  this(configuration, builderAssistant, context, null);
}
 
Example #18
Source File: XMLStatementBuilder.java    From mybatis with Apache License 2.0 4 votes vote down vote up
public XMLStatementBuilder(Configuration configuration, MapperBuilderAssistant builderAssistant, XNode context, String databaseId) {
  super(configuration);
  this.builderAssistant = builderAssistant;
  this.context = context;
  this.requiredDatabaseId = databaseId;
}
 
Example #19
Source File: MybatisMapperBuildAssistant.java    From spring-data-mybatis with Apache License 2.0 3 votes vote down vote up
public MybatisMapperBuildAssistant(Configuration configuration,
		PersistentEntity<?, ?> persistentEntity, String namespace) {

	this.configuration = configuration;

	dialect = detectDialect();

	this.entity = (MybatisPersistentEntity<?>) persistentEntity;

	this.assistant = new MapperBuilderAssistant(configuration,
			namespace.replace('.', '/') + ".java (mapper)");
	this.assistant.setCurrentNamespace(namespace);
}
 
Example #20
Source File: ISqlInjector.java    From Roothub with GNU Affero General Public License v3.0 2 votes vote down vote up
/**
 * 根据 mapperClass 注入 SQL,需要检查 SQL 是否已注入(已经注入过不再注入)
 * @param builderAssistant
 * @param mapperClass
 */
void inspectInject(MapperBuilderAssistant builderAssistant, Class<?> mapperClass);