Java Code Examples for org.apache.ibatis.session.Configuration#addMappedStatement()

The following examples show how to use org.apache.ibatis.session.Configuration#addMappedStatement() . 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: UpdateBuilder.java    From azeroth with Apache License 2.0 6 votes vote down vote up
/**
 * @param configuration
 * @param entity
 */
public static void build(Configuration configuration, LanguageDriver languageDriver,
                         EntityInfo entity) {
    String[] names = GeneralSqlGenerator.methodDefines.updateName().split(",");
    for (String name : names) {
        String msId = entity.getMapperClass().getName() + "." + name;

        EntityMapper entityMapper = EntityHelper.getEntityMapper(entity.getEntityClass());

        String sql = buildUpdateSql(entityMapper, name.endsWith("Selective"));

        SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql,
                entity.getEntityClass());

        MappedStatement.Builder statementBuilder = new MappedStatement.Builder(configuration,
                msId, sqlSource, SqlCommandType.UPDATE);

        MappedStatement statement = statementBuilder.build();

        configuration.addMappedStatement(statement);
    }
}
 
Example 2
Source File: DeleteBuilder.java    From azeroth with Apache License 2.0 6 votes vote down vote up
/**
 * @param configuration
 * @param entity
 */
public static void build(Configuration configuration, LanguageDriver languageDriver, EntityInfo entity) {
    String msId = entity.getMapperClass().getName() + "." + GeneralSqlGenerator.methodDefines.deleteName();

    // 从参数对象里提取注解信息
    EntityMapper entityMapper = EntityHelper.getEntityMapper(entity.getEntityClass());
    // 生成sql
    String sql = buildDeleteSql(entityMapper);

    SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, entity.getEntityClass());

    MappedStatement.Builder statementBuilder = new MappedStatement.Builder(configuration, msId, sqlSource, SqlCommandType.DELETE);

    MappedStatement statement = statementBuilder.build();
    configuration.addMappedStatement(statement);
}
 
Example 3
Source File: InsertBuilder.java    From azeroth with Apache License 2.0 5 votes vote down vote up
/**
 * @param configuration
 * @param entity
 */
public static void build(Configuration configuration, LanguageDriver languageDriver, EntityInfo entity) {

    String[] names = GeneralSqlGenerator.methodDefines.insertName().split(",");
    for (String name : names) {
        String msId = entity.getMapperClass().getName() + "." + name;

        // 从参数对象里提取注解信息
        EntityMapper entityMapper = EntityHelper.getEntityMapper(entity.getEntityClass());
        // 生成sql
        String sql = buildInsertSql(entityMapper, name.endsWith("Selective"));

        SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, entity.getEntityClass());

        MappedStatement.Builder statementBuilder = new MappedStatement.Builder(configuration, msId, sqlSource, SqlCommandType.INSERT);

        KeyGenerator keyGenerator = entityMapper.autoId() ? new Jdbc3KeyGenerator() : new NoKeyGenerator();
        statementBuilder.keyGenerator(keyGenerator)//
                .keyProperty(entityMapper.getIdColumn().getProperty())//
                .keyColumn(entityMapper.getIdColumn().getColumn());

        MappedStatement statement = statementBuilder.build();

        configuration.addMappedStatement(statement);
    }

}
 
Example 4
Source File: AbstractMethodBuilder.java    From jeesuite-libs with Apache License 2.0 5 votes vote down vote up
public void build(Configuration configuration, LanguageDriver languageDriver,EntityInfo entity) {
	
	for (String name : methodNames()) {			
		String msId = entity.getMapperClass().getName() + "." + name;
		
		// 从参数对象里提取注解信息
		EntityMapper entityMapper = EntityHelper.getEntityMapper(entity.getEntityClass());
		// 生成sql
		String sql = buildSQL(entityMapper,name.endsWith("Selective"));
		
		SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, entity.getEntityClass());
		
		MappedStatement.Builder statementBuilder = new MappedStatement.Builder(configuration, msId, sqlSource,sqlCommandType());
		
		//主键策略
		if(sqlCommandType() == SqlCommandType.INSERT){
			KeyGenerator keyGenerator = entityMapper.autoId() ? new Jdbc3KeyGenerator() : new NoKeyGenerator();
			statementBuilder.keyGenerator(keyGenerator)//
	                        .keyProperty(entityMapper.getIdColumn().getProperty())//
	                        .keyColumn(entityMapper.getIdColumn().getColumn());
		}
		
		MappedStatement statement = statementBuilder.build();
		//
		setResultType(configuration, statement, entity.getEntityClass());
		configuration.addMappedStatement(statement);
	}
	
}
 
Example 5
Source File: ExecutorTestHelper.java    From mybaties with Apache License 2.0 5 votes vote down vote up
public static MappedStatement prepareInsertAuthorMappedStatementWithBeforeAutoKey(final Configuration config) {
  final TypeHandlerRegistry registry = config.getTypeHandlerRegistry();
  final ResultMap rm = new ResultMap.Builder(config, "keyResultMap", Integer.class, new ArrayList<ResultMapping>())
      .build();

  MappedStatement kms = new MappedStatement.Builder(config, "insertAuthor!selectKey", new StaticSqlSource(config,"SELECT 123456 as id FROM SYSIBM.SYSDUMMY1"), SqlCommandType.SELECT)
      .keyProperty("id")
      .resultMaps(new ArrayList<ResultMap>() {
        {
          add(rm);
        }
      })
      .build();
  config.addMappedStatement(kms);
  MappedStatement ms = new MappedStatement.Builder(config, "insertAuthor", new DynamicSqlSource(config, new TextSqlNode("INSERT INTO author (id,username,password,email,bio,favourite_section) values(#{id},#{username},#{password},#{email},#{bio:VARCHAR},#{favouriteSection})")), SqlCommandType.INSERT)
      .parameterMap(
          new ParameterMap.Builder(config, "defaultParameterMap", Author.class, new ArrayList<ParameterMapping>() {
            {
              add(new ParameterMapping.Builder(config, "id", registry.getTypeHandler(Integer.class)).build());
              add(new ParameterMapping.Builder(config, "username", registry.getTypeHandler(String.class)).build());
              add(new ParameterMapping.Builder(config, "password", registry.getTypeHandler(String.class)).build());
              add(new ParameterMapping.Builder(config, "email", registry.getTypeHandler(String.class)).build());
              add(new ParameterMapping.Builder(config, "bio", registry.getTypeHandler(String.class)).jdbcType(JdbcType.VARCHAR).build());
              add(new ParameterMapping.Builder(config, "favouriteSection", registry.getTypeHandler(Section.class)).jdbcType(JdbcType.VARCHAR).build());
            }
          }).build())
      .cache(authorCache)
      .keyGenerator(new SelectKeyGenerator(kms, true))
      .keyProperty("id")
      .build();
  return ms;
}
 
Example 6
Source File: ExecutorTestHelper.java    From mybatis with Apache License 2.0 5 votes vote down vote up
public static MappedStatement prepareInsertAuthorMappedStatementWithBeforeAutoKey(final Configuration config) {
  final TypeHandlerRegistry registry = config.getTypeHandlerRegistry();
  final ResultMap rm = new ResultMap.Builder(config, "keyResultMap", Integer.class, new ArrayList<ResultMapping>())
      .build();

  MappedStatement kms = new MappedStatement.Builder(config, "insertAuthor!selectKey", new StaticSqlSource(config,"SELECT 123456 as id FROM SYSIBM.SYSDUMMY1"), SqlCommandType.SELECT)
      .keyProperty("id")
      .resultMaps(new ArrayList<ResultMap>() {
        {
          add(rm);
        }
      })
      .build();
  config.addMappedStatement(kms);
  MappedStatement ms = new MappedStatement.Builder(config, "insertAuthor", new DynamicSqlSource(config, new TextSqlNode("INSERT INTO author (id,username,password,email,bio,favourite_section) values(#{id},#{username},#{password},#{email},#{bio:VARCHAR},#{favouriteSection})")), SqlCommandType.INSERT)
      .parameterMap(
          new ParameterMap.Builder(config, "defaultParameterMap", Author.class, new ArrayList<ParameterMapping>() {
            {
              add(new ParameterMapping.Builder(config, "id", registry.getTypeHandler(Integer.class)).build());
              add(new ParameterMapping.Builder(config, "username", registry.getTypeHandler(String.class)).build());
              add(new ParameterMapping.Builder(config, "password", registry.getTypeHandler(String.class)).build());
              add(new ParameterMapping.Builder(config, "email", registry.getTypeHandler(String.class)).build());
              add(new ParameterMapping.Builder(config, "bio", registry.getTypeHandler(String.class)).jdbcType(JdbcType.VARCHAR).build());
              add(new ParameterMapping.Builder(config, "favouriteSection", registry.getTypeHandler(Section.class)).jdbcType(JdbcType.VARCHAR).build());
            }
          }).build())
      .cache(authorCache)
      .keyGenerator(new SelectKeyGenerator(kms, true))
      .keyProperty("id")
      .build();
  return ms;
}
 
Example 7
Source File: PaginationHandler.java    From azeroth with Apache License 2.0 4 votes vote down vote up
/**
 * 新建count查询的MappedStatement
 *
 * @param ms
 * @return
 */
public MappedStatement getCountMappedStatement(MappedStatement ms) {

    String newMsId = ms.getId() + PAGE_COUNT_SUFFIX;

    MappedStatement statement = null;
    Configuration configuration = ms.getConfiguration();

    try {
        statement = configuration.getMappedStatement(newMsId);
        if (statement != null) { return statement; }
    } catch (Exception e) {
    }

    synchronized (configuration) {

        if (configuration.hasStatement(newMsId)) { return configuration.getMappedStatement(newMsId); }

        MappedStatement.Builder builder = new MappedStatement.Builder(ms.getConfiguration(),
                newMsId, ms.getSqlSource(), ms.getSqlCommandType());
        builder.resource(ms.getResource());
        builder.fetchSize(ms.getFetchSize());
        builder.statementType(ms.getStatementType());
        builder.keyGenerator(ms.getKeyGenerator());
        if (ms.getKeyProperties() != null && ms.getKeyProperties().length != 0) {
            StringBuilder keyProperties = new StringBuilder();
            for (String keyProperty : ms.getKeyProperties()) {
                keyProperties.append(keyProperty).append(",");
            }
            keyProperties.delete(keyProperties.length() - 1, keyProperties.length());
            builder.keyProperty(keyProperties.toString());
        }
        builder.timeout(ms.getTimeout());
        builder.parameterMap(ms.getParameterMap());
        //count查询返回值int
        List<ResultMap> resultMaps = new ArrayList<ResultMap>();
        String id = newMsId + "-Inline";
        ResultMap resultMap = new ResultMap.Builder(configuration, id, Long.class,
                new ArrayList<ResultMapping>(0)).build();
        resultMaps.add(resultMap);
        builder.resultMaps(resultMaps);

        builder.resultSetType(ms.getResultSetType());
        builder.cache(ms.getCache());
        builder.flushCacheRequired(ms.isFlushCacheRequired());
        builder.useCache(ms.isUseCache());

        statement = builder.build();
        configuration.addMappedStatement(statement);
        return statement;
    }

}
 
Example 8
Source File: CacheHandler.java    From azeroth with Apache License 2.0 4 votes vote down vote up
private MappedStatement getQueryIdsMappedStatementForUpdateCache(MappedStatement mt,
                                                                 EntityInfo entityInfo) {
    String msId = mt.getId() + QUERY_IDS_SUFFIX;

    MappedStatement statement = null;
    Configuration configuration = mt.getConfiguration();
    try {
        statement = configuration.getMappedStatement(msId);
        if (statement != null) { return statement; }
    } catch (Exception e) {
    }

    synchronized (configuration) {
        if (configuration.hasStatement(msId)) { return configuration.getMappedStatement(msId); }

        String sql = entityInfo.getMapperSqls().get(mt.getId());

        if (StringUtils.isNotBlank(sql)) {
            if (!sql.toLowerCase().contains(entityInfo.getTableName().toLowerCase())) {
                return null;
            }
            sql = "select " + entityInfo.getIdColumn() + " from " + entityInfo.getTableName()
                    + " WHERE " + sql.split(WHERE_REGEX)[1];
            sql = String.format(SqlTemplate.SCRIPT_TEMAPLATE, sql);
        } else {
            sql = PARSE_SQL_ERROR_DEFAULT;
        }
        SqlSource sqlSource = configuration.getDefaultScriptingLanguageInstance()
                .createSqlSource(configuration, sql, Object.class);

        MappedStatement.Builder statementBuilder = new MappedStatement.Builder(configuration,
                msId, sqlSource, SqlCommandType.SELECT);

        statementBuilder.resource(mt.getResource());
        statementBuilder.fetchSize(mt.getFetchSize());
        statementBuilder.statementType(mt.getStatementType());
        statementBuilder.parameterMap(mt.getParameterMap());
        statement = statementBuilder.build();

        List<ResultMap> resultMaps = new ArrayList<ResultMap>();

        String id = msId + "-Inline";
        ResultMap.Builder builder = new ResultMap.Builder(configuration, id,
                entityInfo.getIdType(), new ArrayList<ResultMapping>(), true);
        resultMaps.add(builder.build());
        MetaObject metaObject = SystemMetaObject.forObject(statement);
        metaObject.setValue("resultMaps", Collections.unmodifiableList(resultMaps));

        configuration.addMappedStatement(statement);

        return statement;
    }
}
 
Example 9
Source File: GetByPrimaryKeyBuilder.java    From azeroth with Apache License 2.0 4 votes vote down vote up
/**
 * @param configuration
 * @param entity
 */
public static void build(Configuration configuration, LanguageDriver languageDriver, EntityInfo entity) {
    String msId = entity.getMapperClass().getName() + "." + GeneralSqlGenerator.methodDefines.selectName();

    EntityMapper entityMapper = EntityHelper.getEntityMapper(entity.getEntityClass());

    String sql = buildGetByIdSql(entityMapper);

    SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, entity.getEntityClass());

    MappedStatement.Builder statementBuilder = new MappedStatement.Builder(configuration, msId, sqlSource, SqlCommandType.SELECT);

    // 将返回值修改为实体类型
    MappedStatement statement = statementBuilder.build();
    setResultType(configuration, statement, entity.getEntityClass());

    configuration.addMappedStatement(statement);
}
 
Example 10
Source File: PaginationHandler.java    From jeesuite-libs with Apache License 2.0 4 votes vote down vote up
/**
   * 新建count查询的MappedStatement
   *
   * @param ms
   * @return
   */
  public MappedStatement getCountMappedStatement(MappedStatement ms) {
  	
  	String newMsId = ms.getId() + PAGE_COUNT_SUFFIX;
  	
  	MappedStatement statement = null;
  	Configuration configuration = ms.getConfiguration();
	
  	try {
  		statement = configuration.getMappedStatement(newMsId);
  		if(statement != null)return statement;
} catch (Exception e) {}
  	
  	synchronized (configuration) {   
  		
  		if(configuration.hasStatement(newMsId))return configuration.getMappedStatement(newMsId);
  		
  		MappedStatement.Builder builder = new MappedStatement.Builder(ms.getConfiguration(), newMsId, ms.getSqlSource(), ms.getSqlCommandType());
  		builder.resource(ms.getResource());
  		builder.fetchSize(ms.getFetchSize());
  		builder.statementType(ms.getStatementType());
  		builder.keyGenerator(ms.getKeyGenerator());
  		if (ms.getKeyProperties() != null && ms.getKeyProperties().length != 0) {
  			StringBuilder keyProperties = new StringBuilder();
  			for (String keyProperty : ms.getKeyProperties()) {
  				keyProperties.append(keyProperty).append(",");
  			}
  			keyProperties.delete(keyProperties.length() - 1, keyProperties.length());
  			builder.keyProperty(keyProperties.toString());
  		}
  		builder.timeout(ms.getTimeout());
  		builder.parameterMap(ms.getParameterMap());
  		//count查询返回值int
  		 List<ResultMap> resultMaps = new ArrayList<ResultMap>();
           String id = newMsId + "-Inline";
           ResultMap resultMap = new ResultMap.Builder(configuration, id, Long.class, new ArrayList<ResultMapping>(0)).build();
           resultMaps.add(resultMap);
           builder.resultMaps(resultMaps);
           
  		builder.resultSetType(ms.getResultSetType());
  		builder.cache(ms.getCache());
  		builder.flushCacheRequired(ms.isFlushCacheRequired());
  		builder.useCache(ms.isUseCache());
  		
  		statement = builder.build();
  		configuration.addMappedStatement(statement);
  		return statement;
  	}
  	
  }