Java Code Examples for org.apache.ibatis.session.SqlSession#getConfiguration()

The following examples show how to use org.apache.ibatis.session.SqlSession#getConfiguration() . 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: SqlHelper.java    From mybatis-generator-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * 通过命名空间方式获取sql
 * @param session
 * @param namespace
 * @param params
 * @return
 */
public static String getNamespaceSql(SqlSession session, String namespace, Object params) {
    Configuration configuration = session.getConfiguration();
    MappedStatement mappedStatement = configuration.getMappedStatement(namespace);
    TypeHandlerRegistry typeHandlerRegistry = mappedStatement.getConfiguration().getTypeHandlerRegistry();
    BoundSql boundSql = mappedStatement.getBoundSql(params);
    List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
    String sql = boundSql.getSql();
    if (parameterMappings != null) {
        for (int i = 0; i < parameterMappings.size(); i++) {
            ParameterMapping parameterMapping = parameterMappings.get(i);
            if (parameterMapping.getMode() != ParameterMode.OUT) {
                Object value;
                String propertyName = parameterMapping.getProperty();
                if (boundSql.hasAdditionalParameter(propertyName)) {
                    value = boundSql.getAdditionalParameter(propertyName);
                } else if (params == null) {
                    value = null;
                } else if (typeHandlerRegistry.hasTypeHandler(params.getClass())) {
                    value = params;
                } else {
                    MetaObject metaObject = configuration.newMetaObject(params);
                    value = metaObject.getValue(propertyName);
                }
                JdbcType jdbcType = parameterMapping.getJdbcType();
                if (value == null && jdbcType == null) jdbcType = configuration.getJdbcTypeForNull();
                sql = replaceParameter(sql, value, jdbcType, parameterMapping.getJavaType());
            }
        }
    }
    return sql;
}
 
Example 2
Source File: PaginationMapperMethod.java    From Shop-for-JavaWeb with MIT License 5 votes vote down vote up
public PaginationMapperMethod(Class<?> declaringInterface, Method method,
                              SqlSession sqlSession) {
    paramNames = new ArrayList<String>();
    paramPositions = new ArrayList<Integer>();
    this.sqlSession = sqlSession;
    this.method = method;
    this.config = sqlSession.getConfiguration();
    this.declaringInterface = declaringInterface;
    this.hasNamedParameters = false;
    setupFields();
    setupMethodSignature();
    setupCommandType();
    validateStatement();
}
 
Example 3
Source File: CreateTaskAccTest.java    From taskana with Apache License 2.0 4 votes vote down vote up
@WithAccessId(user = "user-1-1")
@Test
void testCreateSimpleTaskWithCustomAttributes()
    throws NotAuthorizedException, InvalidArgumentException, ClassificationNotFoundException,
        WorkbasketNotFoundException, TaskAlreadyExistException, TaskNotFoundException,
        NoSuchFieldException, IllegalAccessException {

  Task newTask = taskService.newTask("USER-1-1", "DOMAIN_A");
  newTask.setClassificationKey("T2100");
  newTask.setPrimaryObjRef(
      createObjectReference("COMPANY_A", "SYSTEM_A", "INSTANCE_A", "VNR", "1234567"));
  Map<String, String> customAttributesForCreate = createSimpleCustomProperties(13);
  newTask.setCustomAttributes(customAttributesForCreate);
  Task createdTask = taskService.createTask(newTask);
  Instant expectedPlanned = moveForwardToWorkingDay(createdTask.getCreated());

  assertThat(createdTask).isNotNull();
  assertThat(createdTask.getName()).isEqualTo("T-Vertragstermin VERA");
  assertThat(createdTask.getPrimaryObjRef().getValue()).isEqualTo("1234567");
  assertThat(createdTask.getCreated()).isNotNull();
  assertThat(createdTask.getModified()).isNotNull();
  assertThat(createdTask.getBusinessProcessId()).isNotNull();
  assertThat(createdTask.getClaimed()).isNull();
  assertThat(createdTask.getCompleted()).isNull();
  assertThat(createdTask.getModified()).isEqualTo(createdTask.getCreated());
  assertThat(createdTask.getPlanned()).isEqualTo(expectedPlanned);
  assertThat(createdTask.getState()).isEqualTo(TaskState.READY);
  assertThat(createdTask.getParentBusinessProcessId()).isNull();
  assertThat(createdTask.getPriority()).isEqualTo(2);
  assertThat(createdTask.isRead()).isFalse();
  assertThat(createdTask.isTransferred()).isFalse();
  // verify that the database content is as expected
  TaskanaEngineProxyForTest engineProxy = new TaskanaEngineProxyForTest(taskanaEngine);
  try {
    SqlSession session = engineProxy.getSqlSession();
    Configuration config = session.getConfiguration();
    if (!config.hasMapper(TaskTestMapper.class)) {
      config.addMapper(TaskTestMapper.class);
    }
    TaskTestMapper mapper = session.getMapper(TaskTestMapper.class);

    engineProxy.openConnection();
    String customProperties = mapper.getCustomAttributesAsString(createdTask.getId());
    assertThat(customProperties)
        .contains(
            "\"Property_13\":\"Property Value of Property_13\"",
            "\"Property_12\":\"Property Value of Property_12\"",
            "\"Property_11\":\"Property Value of Property_11\"",
            "\"Property_10\":\"Property Value of Property_10\"",
            "\"Property_9\":\"Property Value of Property_9\"",
            "\"Property_8\":\"Property Value of Property_8\"",
            "\"Property_7\":\"Property Value of Property_7\"",
            "\"Property_6\":\"Property Value of Property_6\"",
            "\"Property_5\":\"Property Value of Property_5\"",
            "\"Property_4\":\"Property Value of Property_4\"",
            "\"Property_3\":\"Property Value of Property_3\"",
            "\"Property_2\":\"Property Value of Property_2\"",
            "\"Property_1\":\"Property Value of Property_1\"");
  } finally {
    engineProxy.returnConnection();
  }
  // verify that the map is correctly retrieved from the database
  Task retrievedTask = taskService.getTask(createdTask.getId());
  Map<String, String> customAttributesFromDb = retrievedTask.getCustomAttributes();
  assertThat(customAttributesFromDb).isNotNull();
  assertThat(customAttributesForCreate).isEqualTo(customAttributesFromDb);
}
 
Example 4
Source File: QueryTasksAccTest.java    From taskana with Apache License 2.0 4 votes vote down vote up
@WithAccessId(user = "admin")
@Test
void testQueryTaskByCustomAttributes()
    throws NotAuthorizedException, InvalidArgumentException, ClassificationNotFoundException,
        WorkbasketNotFoundException, TaskAlreadyExistException, NoSuchFieldException,
        IllegalAccessException {

  Task newTask = taskService.newTask("USER-1-1", "DOMAIN_A");
  newTask.setPrimaryObjRef(
      createObjectReference("COMPANY_A", "SYSTEM_A", "INSTANCE_A", "VNR", "1234567"));
  newTask.setClassificationKey("T2100");
  Map<String, String> customAttributesForCreate =
      createSimpleCustomProperties(20000); // about 1 Meg
  newTask.setCustomAttributes(customAttributesForCreate);
  Task createdTask = taskService.createTask(newTask);

  assertThat(createdTask).isNotNull();
  // query the task by custom attributes
  TaskanaEngineProxyForTest engineProxy = new TaskanaEngineProxyForTest(taskanaEngine);
  try {
    SqlSession session = engineProxy.getSqlSession();
    Configuration config = session.getConfiguration();
    if (!config.hasMapper(TaskTestMapper.class)) {
      config.addMapper(TaskTestMapper.class);
    }

    TaskTestMapper mapper = session.getMapper(TaskTestMapper.class);
    engineProxy.openConnection();
    List<TaskImpl> queryResult =
        mapper.selectTasksByCustomAttributeLike("%Property Value of Property_1339%");

    assertThat(queryResult).hasSize(1);
    Task retrievedTask = queryResult.get(0);

    assertThat(retrievedTask.getId()).isEqualTo(createdTask.getId());

    // verify that the map is correctly retrieved from the database
    Map<String, String> customAttributesFromDb = retrievedTask.getCustomAttributes();
    assertThat(customAttributesFromDb).isNotNull();
    assertThat(customAttributesFromDb).isEqualTo(customAttributesForCreate);

  } finally {
    engineProxy.returnConnection();
  }
}
 
Example 5
Source File: SqlAutoMapper.java    From hui-core-autoreport with Apache License 2.0 2 votes vote down vote up
/**
 * 构造方法,默认缓存MappedStatement
 *
 * @param sqlSession
 */
public SqlAutoMapper(SqlSession sqlSession) {
    this.sqlSession = sqlSession;
    this.msUtils = new MSUtils(sqlSession.getConfiguration());
}