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

The following examples show how to use org.apache.ibatis.session.SqlSession#select() . 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: SqlSessionTestBase.java    From pinpoint with Apache License 2.0 6 votes vote down vote up
protected final void testAndVerifySelect() throws Exception {
    // Given
    final String selectId = "selectId";
    SqlSession sqlSession = getSqlSession();
    // When
    sqlSession.select(selectId, new DefaultResultHandler());
    sqlSession.select(selectId, new Object(), new DefaultResultHandler());
    sqlSession.select(selectId, new Object(), RowBounds.DEFAULT, new DefaultResultHandler());
    // Then
    PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
    Method select1 = sqlSession.getClass().getDeclaredMethod("select", String.class, ResultHandler.class);
    verifier.verifyTrace(event("MYBATIS", select1, Expectations.cachedArgs(selectId)));
    Method select2 = sqlSession.getClass().getDeclaredMethod("select", String.class, Object.class,
            ResultHandler.class);
    verifier.verifyTrace(event("MYBATIS", select2, Expectations.cachedArgs(selectId)));
    Method select3 = sqlSession.getClass().getDeclaredMethod("select", String.class, Object.class, RowBounds.class,
            ResultHandler.class);
    verifier.verifyTrace(event("MYBATIS", select3, Expectations.cachedArgs(selectId)));
}
 
Example 2
Source File: NestedResultHandlerAssociationTest.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldHandleStop() throws Exception {
  SqlSession sqlSession = sqlSessionFactory.openSession();
  final SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
  final List<Account> accounts = new ArrayList<Account>();
  try {
    Date targetMonth = fmt.parse("2014-01-01");
    sqlSession.select("collectPageByBirthMonth", targetMonth, new ResultHandler() {
      @Override
      public void handleResult(ResultContext context) {
        Account account = (Account) context.getResultObject();
        accounts.add(account);
        if (accounts.size() > 1)
          context.stop();
      }
    });
  } finally {
    sqlSession.close();
  }
  assertEquals(2, accounts.size());
  assertEquals("Bob1", accounts.get(0).getAccountName());
  assertEquals("Bob2", accounts.get(1).getAccountName());
}
 
Example 3
Source File: NestedResultHandlerAssociationTest.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldHandleRowBounds() throws Exception {
  SqlSession sqlSession = sqlSessionFactory.openSession();
  final SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
  Date targetMonth = fmt.parse("2014-01-01");
  final List<Account> accounts = new ArrayList<Account>();
  try {
    sqlSession.select("collectPageByBirthMonth", targetMonth, new RowBounds(1, 2), new ResultHandler() {
      @Override
      public void handleResult(ResultContext context) {
        Account account = (Account) context.getResultObject();
        accounts.add(account);
      }
    });
  } finally {
    sqlSession.close();
  }
  assertEquals(2, accounts.size());
  assertEquals("Bob2", accounts.get(0).getAccountName());
  assertEquals("Bob3", accounts.get(1).getAccountName());
}
 
Example 4
Source File: NestedResultHandlerTest.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@Test(expected=PersistenceException.class)
public void testUnorderedGetPersonWithHandler() {
  SqlSession sqlSession = sqlSessionFactory.openSession();
  try {
    sqlSession.select("getPersonsWithItemsOrdered", new ResultHandler() {
      public void handleResult(ResultContext context) {
        Person person = (Person) context.getResultObject();
        if ("grandma".equals(person.getName())) {
          Assert.assertEquals(2, person.getItems().size());
        }
      }
    });
  } finally {
    sqlSession.close();
  }
}
 
Example 5
Source File: NestedResultHandlerTest.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@Test
// issue #542
public void testGetPersonWithHandler() {
  SqlSession sqlSession = sqlSessionFactory.openSession();
  try {
    sqlSession.select("getPersons", new ResultHandler() {
      public void handleResult(ResultContext context) {
        Person person = (Person) context.getResultObject();
        if ("grandma".equals(person.getName())) {
          Assert.assertEquals(2, person.getItems().size());
        }
      }
    });
  } finally {
    sqlSession.close();
  }
}
 
Example 6
Source File: BindingTest.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldExecuteMultipleBoundSelectOfBlogsByIdInWithProvidedResultHandlerInSameSession() {
  SqlSession session = sqlSessionFactory.openSession();
  try {
    final DefaultResultHandler handler = new DefaultResultHandler();
    session.select("selectBlogsAsMapById", handler);

    final DefaultResultHandler moreHandler = new DefaultResultHandler();
    session.select("selectBlogsAsMapById", moreHandler);

    assertEquals(2, handler.getResultList().size());
    assertEquals(2, moreHandler.getResultList().size());

  } finally {
    session.close();
  }
}
 
Example 7
Source File: BindingTest.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldExecuteMultipleBoundSelectOfBlogsByIdInWithProvidedResultHandlerBetweenSessions() {
  SqlSession session = sqlSessionFactory.openSession();
  try {
    final DefaultResultHandler handler = new DefaultResultHandler();
    session.select("selectBlogsAsMapById", handler);

    //new session
    session.close();
    session = sqlSessionFactory.openSession();

    final DefaultResultHandler moreHandler = new DefaultResultHandler();
    session.select("selectBlogsAsMapById", moreHandler);

    assertEquals(2, handler.getResultList().size());
    assertEquals(2, moreHandler.getResultList().size());

  } finally {
    session.close();
  }
}
 
Example 8
Source File: NestedResultHandlerAssociationTest.java    From mybaties with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldHandleStop() throws Exception {
  SqlSession sqlSession = sqlSessionFactory.openSession();
  final SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
  final List<Account> accounts = new ArrayList<Account>();
  try {
    Date targetMonth = fmt.parse("2014-01-01");
    sqlSession.select("collectPageByBirthMonth", targetMonth, new ResultHandler() {
      @Override
      public void handleResult(ResultContext context) {
        Account account = (Account) context.getResultObject();
        accounts.add(account);
        if (accounts.size() > 1)
          context.stop();
      }
    });
  } finally {
    sqlSession.close();
  }
  assertEquals(2, accounts.size());
  assertEquals("Bob1", accounts.get(0).getAccountName());
  assertEquals("Bob2", accounts.get(1).getAccountName());
}
 
Example 9
Source File: NestedResultHandlerAssociationTest.java    From mybaties with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldHandleRowBounds() throws Exception {
  SqlSession sqlSession = sqlSessionFactory.openSession();
  final SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
  Date targetMonth = fmt.parse("2014-01-01");
  final List<Account> accounts = new ArrayList<Account>();
  try {
    sqlSession.select("collectPageByBirthMonth", targetMonth, new RowBounds(1, 2), new ResultHandler() {
      @Override
      public void handleResult(ResultContext context) {
        Account account = (Account) context.getResultObject();
        accounts.add(account);
      }
    });
  } finally {
    sqlSession.close();
  }
  assertEquals(2, accounts.size());
  assertEquals("Bob2", accounts.get(0).getAccountName());
  assertEquals("Bob3", accounts.get(1).getAccountName());
}
 
Example 10
Source File: NestedResultHandlerTest.java    From mybaties with Apache License 2.0 6 votes vote down vote up
@Test(expected=PersistenceException.class)
public void testUnorderedGetPersonWithHandler() {
  SqlSession sqlSession = sqlSessionFactory.openSession();
  try {
    sqlSession.select("getPersonsWithItemsOrdered", new ResultHandler() {
      public void handleResult(ResultContext context) {
        Person person = (Person) context.getResultObject();
        if ("grandma".equals(person.getName())) {
          Assert.assertEquals(2, person.getItems().size());
        }
      }
    });
  } finally {
    sqlSession.close();
  }
}
 
Example 11
Source File: NestedResultHandlerTest.java    From mybaties with Apache License 2.0 6 votes vote down vote up
@Test
// issue #542
public void testGetPersonWithHandler() {
  SqlSession sqlSession = sqlSessionFactory.openSession();
  try {
    sqlSession.select("getPersons", new ResultHandler() {
      public void handleResult(ResultContext context) {
        Person person = (Person) context.getResultObject();
        if ("grandma".equals(person.getName())) {
          Assert.assertEquals(2, person.getItems().size());
        }
      }
    });
  } finally {
    sqlSession.close();
  }
}
 
Example 12
Source File: BindingTest.java    From mybaties with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldExecuteMultipleBoundSelectOfBlogsByIdInWithProvidedResultHandlerInSameSession() {
  SqlSession session = sqlSessionFactory.openSession();
  try {
    final DefaultResultHandler handler = new DefaultResultHandler();
    session.select("selectBlogsAsMapById", handler);

    final DefaultResultHandler moreHandler = new DefaultResultHandler();
    session.select("selectBlogsAsMapById", moreHandler);

    assertEquals(2, handler.getResultList().size());
    assertEquals(2, moreHandler.getResultList().size());

  } finally {
    session.close();
  }
}
 
Example 13
Source File: BindingTest.java    From mybaties with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldExecuteMultipleBoundSelectOfBlogsByIdInWithProvidedResultHandlerBetweenSessions() {
  SqlSession session = sqlSessionFactory.openSession();
  try {
    final DefaultResultHandler handler = new DefaultResultHandler();
    session.select("selectBlogsAsMapById", handler);

    //new session
    session.close();
    session = sqlSessionFactory.openSession();

    final DefaultResultHandler moreHandler = new DefaultResultHandler();
    session.select("selectBlogsAsMapById", moreHandler);

    assertEquals(2, handler.getResultList().size());
    assertEquals(2, moreHandler.getResultList().size());

  } finally {
    session.close();
  }
}
 
Example 14
Source File: MapperMethod.java    From mybaties with Apache License 2.0 5 votes vote down vote up
private void executeWithResultHandler(SqlSession sqlSession, Object[] args) {
  MappedStatement ms = sqlSession.getConfiguration().getMappedStatement(command.getName());
  if (void.class.equals(ms.getResultMaps().get(0).getType())) {
    throw new BindingException("method " + command.getName() 
        + " needs either a @ResultMap annotation, a @ResultType annotation," 
        + " or a resultType attribute in XML so a ResultHandler can be used as a parameter.");
  }
  Object param = method.convertArgsToSqlCommandParam(args);
  if (method.hasRowBounds()) {
    RowBounds rowBounds = method.extractRowBounds(args);
    sqlSession.select(command.getName(), param, rowBounds, method.extractResultHandler(args));
  } else {
    sqlSession.select(command.getName(), param, method.extractResultHandler(args));
  }
}
 
Example 15
Source File: MapperMethod.java    From mybatis with Apache License 2.0 5 votes vote down vote up
private void executeWithResultHandler(SqlSession sqlSession, Object[] args) {
  MappedStatement ms = sqlSession.getConfiguration().getMappedStatement(command.getName());
  if (void.class.equals(ms.getResultMaps().get(0).getType())) {
    throw new BindingException("method " + command.getName() 
        + " needs either a @ResultMap annotation, a @ResultType annotation," 
        + " or a resultType attribute in XML so a ResultHandler can be used as a parameter.");
  }
  Object param = method.convertArgsToSqlCommandParam(args);
  if (method.hasRowBounds()) {
    RowBounds rowBounds = method.extractRowBounds(args);
    sqlSession.select(command.getName(), param, rowBounds, method.extractResultHandler(args));
  } else {
    sqlSession.select(command.getName(), param, method.extractResultHandler(args));
  }
}
 
Example 16
Source File: CommonPropertyDeferLoadError.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeferLoadDuringResultHandler() {
    SqlSession sqlSession = sqlSessionFactory.openSession();
    try {
        class MyResultHandler implements ResultHandler {
            public void handleResult(ResultContext context) {
                Child child = (Child)context.getResultObject();
                assertNotNull(child.getFather());
            }
        };
        sqlSession.select("org.apache.ibatis.submitted.deferload_common_property.ChildMapper.selectAll", new MyResultHandler());
    } finally {
        sqlSession.close();
    }
}
 
Example 17
Source File: CommonPropertyDeferLoadError.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeferLoadDuringResultHandlerWithLazyLoad() {
    SqlSession sqlSession = lazyLoadSqlSessionFactory.openSession();
    try {
        class MyResultHandler implements ResultHandler {
            public void handleResult(ResultContext context) {
                Child child = (Child)context.getResultObject();
                assertNotNull(child.getFather());
            }
        };
        sqlSession.select("org.apache.ibatis.submitted.deferload_common_property.ChildMapper.selectAll", new MyResultHandler());
    } finally {
        sqlSession.close();
    }
}
 
Example 18
Source File: CommonPropertyDeferLoadError.java    From mybaties with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeferLoadDuringResultHandlerWithLazyLoad() {
    SqlSession sqlSession = lazyLoadSqlSessionFactory.openSession();
    try {
        class MyResultHandler implements ResultHandler {
            public void handleResult(ResultContext context) {
                Child child = (Child)context.getResultObject();
                assertNotNull(child.getFather());
            }
        };
        sqlSession.select("org.apache.ibatis.submitted.deferload_common_property.ChildMapper.selectAll", new MyResultHandler());
    } finally {
        sqlSession.close();
    }
}
 
Example 19
Source File: CommonPropertyDeferLoadError.java    From mybaties with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeferLoadDuringResultHandler() {
    SqlSession sqlSession = sqlSessionFactory.openSession();
    try {
        class MyResultHandler implements ResultHandler {
            public void handleResult(ResultContext context) {
                Child child = (Child)context.getResultObject();
                assertNotNull(child.getFather());
            }
        };
        sqlSession.select("org.apache.ibatis.submitted.deferload_common_property.ChildMapper.selectAll", new MyResultHandler());
    } finally {
        sqlSession.close();
    }
}