/** USE THIS INSTEAD OF THE BATGEN DELETE It also deletes associated events of the object.s */ public int delete(Long key) throws BoException { SqlSession session = null; int result = 0; String where = "KEY='" + key + "' "; Map<String, Object> map = new HashMap<String, Object>(); map.put("where", where); // first delete all events associated with this object EventBo.getInstance().deleteEventsOf(ItemType.SOFTWARE, key); try { session = SessionFactory.getSession(); SoftwareDao mapper = session.getMapper(SoftwareDao.class); result = mapper.delete(map); session.commit(); } catch (Exception e) { session.rollback(); throw new BoException(e); } finally { if (session != null) session.close(); } return result; }
@Test public void shouldSelectOddPostsInKeysList() throws Exception { SqlSession session = sqlMapper.openSession(); try { List<Post> posts = session.selectList( "org.apache.ibatis.domain.blog.mappers.PostMapper.selectOddPostsInKeysList", new HashMap<String, List<Integer>>() { { put( "keys", new ArrayList<Integer>() { { add(0); add(1); add(2); add(3); add(4); } }); } }); // we're getting odd indexes, not odd values, 0 is not odd assertEquals(2, posts.size()); assertEquals(1, posts.get(0).getId()); assertEquals(3, posts.get(1).getId()); } finally { session.close(); } }
@Test public void shouldFindPostsWithAuthorAndBlogIdUsingDynamicSql() throws Exception { SqlSession session = sqlMapper.openSession(); try { List<Post> posts = session.selectList( "org.apache.ibatis.domain.blog.mappers.PostMapper.findPost", new HashMap<String, Object>() { { put( "ids", new ArrayList<Integer>() { { add(1); add(2); add(3); } }); put("blog_id", 1); } }); assertEquals(2, posts.size()); } finally { session.close(); } }
@Test(expected = TooManyResultsException.class) public void shouldFailWithTooManyResultsException() throws Exception { SqlSession session = sqlMapper.openSession(TransactionIsolationLevel.SERIALIZABLE); try { session.selectOne("org.apache.ibatis.domain.blog.mappers.AuthorMapper.selectAllAuthors"); } finally { session.close(); } }
@Test public void shouldSelectAllAuthors() throws Exception { SqlSession session = sqlMapper.openSession(TransactionIsolationLevel.SERIALIZABLE); try { List<Author> authors = session.selectList("org.apache.ibatis.domain.blog.mappers.AuthorMapper.selectAllAuthors"); assertEquals(2, authors.size()); } finally { session.close(); } }
@Test public void shouldSelectCountOfPosts() throws Exception { SqlSession session = sqlMapper.openSession(); try { Integer count = session.selectOne("org.apache.ibatis.domain.blog.mappers.BlogMapper.selectCountOfPosts"); assertEquals(5, count.intValue()); } finally { session.close(); } }
@Test public void shouldExecuteSelectOneAuthorUsingMapperClassThatReturnsALinedHashMap() { SqlSession session = sqlMapper.openSession(); try { AuthorMapper mapper = session.getMapper(AuthorMapper.class); LinkedHashMap<String, Object> author = mapper.selectAuthorLinkedHashMap(101); assertEquals(101, author.get("ID")); } finally { session.close(); } }
@Test public void shouldExecuteSelectAllAuthorsUsingMapperClassThatReturnsLinkedList() { SqlSession session = sqlMapper.openSession(); try { AuthorMapper mapper = session.getMapper(AuthorMapper.class); Collection<Author> authors = mapper.selectAllAuthorsLinkedList(); assertEquals(2, authors.size()); } finally { session.close(); } }
@Test public void shouldExecuteSelectAllAuthorsUsingMapperClassThatReturnsAnArray() { SqlSession session = sqlMapper.openSession(); try { AuthorMapper mapper = session.getMapper(AuthorMapper.class); Author[] authors = mapper.selectAllAuthorsArray(); assertEquals(2, authors.length); } finally { session.close(); } }
@Test public void shouldExecuteSelectOneAuthorUsingMapperClass() { SqlSession session = sqlMapper.openSession(); try { AuthorMapper mapper = session.getMapper(AuthorMapper.class); Author author = mapper.selectAuthor(101); assertEquals(101, author.getId()); } finally { session.close(); } }
@Test public void shouldSelectAllPostsUsingMapperClass() throws Exception { SqlSession session = sqlMapper.openSession(); try { BlogMapper mapper = session.getMapper(BlogMapper.class); List<Map> posts = mapper.selectAllPosts(); assertEquals(5, posts.size()); } finally { session.close(); } }
@Test public void shouldSelectAuthorsUsingMapperClass() { SqlSession session = sqlMapper.openSession(); try { AuthorMapper mapper = session.getMapper(AuthorMapper.class); List<Author> authors = mapper.selectAllAuthors(); assertEquals(2, authors.size()); } finally { session.close(); } }
@Test public void shouldFindPostsAllPostsWithDynamicSql() throws Exception { SqlSession session = sqlMapper.openSession(); try { List<Post> posts = session.selectList("org.apache.ibatis.domain.blog.mappers.PostMapper.findPost"); assertEquals(5, posts.size()); } finally { session.close(); } }
@Test public void shouldSelectAuthorsUsingMapperClassWithResultHandler() { SqlSession session = sqlMapper.openSession(); try { DefaultResultHandler handler = new DefaultResultHandler(); AuthorMapper mapper = session.getMapper(AuthorMapper.class); mapper.selectAllAuthors(handler); assertEquals(2, handler.getResultList().size()); } finally { session.close(); } }
@Test public void shouldDeleteAuthorUsingMapperClass() throws Exception { SqlSession session = sqlMapper.openSession(); try { AuthorMapper mapper = session.getMapper(AuthorMapper.class); int count = mapper.deleteAuthor(101); assertEquals(1, count); assertNull(mapper.selectAuthor(101)); } finally { session.close(); } }
@Test public void shouldThrowExceptionIfMappedStatementDoesNotExist() throws Exception { SqlSession session = sqlMapper.openSession(); try { session.selectList("ThisStatementDoesNotExist"); fail("Expected exception to be thrown due to statement that does not exist."); } catch (Exception e) { assertTrue(e.getMessage().contains("does not contain value for ThisStatementDoesNotExist")); } finally { session.close(); } }
@Test public void shouldSelectOneAuthorWithInlineParams() throws Exception { SqlSession session = sqlMapper.openSession(); try { Author author = session.selectOne( "org.apache.ibatis.domain.blog.mappers.AuthorMapper.selectAuthorWithInlineParams", new Author(101)); assertEquals(101, author.getId()); } finally { session.close(); } }
@Test(expected = BindingException.class) public void shouldFailExecutingAnAnnotatedMapperClassWithResultHandler() { SqlSession session = sqlMapper.openSession(); try { DefaultResultHandler handler = new DefaultResultHandler(); AuthorMapper mapper = session.getMapper(AuthorMapper.class); mapper.selectAuthor2(101, handler); Author author = (Author) handler.getResultList().get(0); assertEquals(101, author.getId()); } finally { session.close(); } }
@Test public void shouldExecuteSelectOneAuthorUsingMapperClassWithResultHandler() { SqlSession session = sqlMapper.openSession(); try { DefaultResultHandler handler = new DefaultResultHandler(); AuthorMapper mapper = session.getMapper(AuthorMapper.class); mapper.selectAuthor(101, handler); Author author = (Author) handler.getResultList().get(0); assertEquals(101, author.getId()); } finally { session.close(); } }
@Test public void shouldSelectOneAuthorAsList() throws Exception { SqlSession session = sqlMapper.openSession(); try { List<Author> authors = session.selectList( "org.apache.ibatis.domain.blog.mappers.AuthorMapper.selectAuthor", new Author(101)); assertEquals(101, authors.get(0).getId()); assertEquals(Section.NEWS, authors.get(0).getFavouriteSection()); } finally { session.close(); } }
@Test public void shouldLimitResultsUsingMapperClass() throws Exception { SqlSession session = sqlMapper.openSession(); try { BlogMapper mapper = session.getMapper(BlogMapper.class); List<Map> posts = mapper.selectAllPosts(new RowBounds(0, 2), null); assertEquals(2, posts.size()); assertEquals(1, posts.get(0).get("ID")); assertEquals(2, posts.get(1).get("ID")); } finally { session.close(); } }
@Test public void shouldStopResultHandler() throws Exception { SqlSession session = sqlMapper.openSession(); try { final TestResultStopHandler resultHandler = new TestResultStopHandler(); session.select( "org.apache.ibatis.domain.blog.mappers.BlogMapper.selectAllPosts", null, resultHandler); assertEquals(2, resultHandler.count); } finally { session.close(); } }
@Test public void shouldUpdateAuthorIfNecessary() throws Exception { SqlSession session = sqlMapper.openSession(); Author original; Author updated; try { original = session.selectOne("org.apache.ibatis.domain.blog.mappers.AuthorMapper.selectAuthor", 101); original.setEmail("*****@*****.**"); original.setBio(null); int updates = session.update( "org.apache.ibatis.domain.blog.mappers.AuthorMapper.updateAuthorIfNecessary", original); assertEquals(1, updates); updated = session.selectOne("org.apache.ibatis.domain.blog.mappers.AuthorMapper.selectAuthor", 101); assertEquals(original.getEmail(), updated.getEmail()); session.commit(); } finally { session.close(); } try { session = sqlMapper.openSession(); updated = session.selectOne("org.apache.ibatis.domain.blog.mappers.AuthorMapper.selectAuthor", 101); assertEquals(original.getEmail(), updated.getEmail()); } finally { session.close(); } }
@Test public void shouldSelectOneImmutableAuthor() throws Exception { SqlSession session = sqlMapper.openSession(); try { ImmutableAuthor author = session.selectOne( "org.apache.ibatis.domain.blog.mappers.AuthorMapper.selectImmutableAuthor", new Author(101)); assertEquals(101, author.getId()); assertEquals(Section.NEWS, author.getFavouriteSection()); } finally { session.close(); } }
@Test public void shouldUpdateAuthorUsingMapperClass() throws Exception { SqlSession session = sqlMapper.openSession(); try { AuthorMapper mapper = session.getMapper(AuthorMapper.class); Author expected = mapper.selectAuthor(101); expected.setUsername("NewUsername"); int count = mapper.updateAuthor(expected); assertEquals(1, count); Author actual = mapper.selectAuthor(101); assertEquals(expected.getUsername(), actual.getUsername()); } finally { session.close(); } }
@Test public void shouldSelectAllAuthorsAsMap() throws Exception { SqlSession session = sqlMapper.openSession(TransactionIsolationLevel.SERIALIZABLE); try { final Map<Integer, Author> authors = session.selectMap( "org.apache.ibatis.domain.blog.mappers.AuthorMapper.selectAllAuthors", "id"); assertEquals(2, authors.size()); for (Map.Entry<Integer, Author> authorEntry : authors.entrySet()) { assertEquals(authorEntry.getKey(), (Integer) authorEntry.getValue().getId()); } } finally { session.close(); } }
@Test(expected = BindingException.class) public void shouldFailSelectOneAuthorUsingMapperClassWithTwoRowBounds() { Configuration configuration = new Configuration(sqlMapper.getConfiguration().getEnvironment()); configuration.addMapper(AuthorMapperWithRowBounds.class); SqlSessionFactory sqlMapperWithMultipleHandlers = new DefaultSqlSessionFactory(configuration); SqlSession sqlSession = sqlMapperWithMultipleHandlers.openSession(); try { RowBounds bounds1 = new RowBounds(0, 1); RowBounds bounds2 = new RowBounds(0, 1); AuthorMapperWithRowBounds mapper = sqlSession.getMapper(AuthorMapperWithRowBounds.class); mapper.selectAuthor(101, bounds1, bounds2); } finally { sqlSession.close(); } }
/** * Deletes all data (rows) from SOFTWARE * * @throws BoException */ public void deleteAll() throws BoException { SqlSession session = null; try { session = SessionFactory.getSession(); SoftwareDao mapper = session.getMapper(SoftwareDao.class); mapper.deleteAll(); session.commit(); } catch (Exception e) { session.rollback(); throw new BoException(e); } finally { if (session != null) session.close(); } }
@Test public void shouldFindPostsWithAuthorIdUsingDynamicSql() throws Exception { SqlSession session = sqlMapper.openSession(); try { List<Post> posts = session.selectList( "org.apache.ibatis.domain.blog.mappers.PostMapper.findPost", new HashMap<String, Integer>() { { put("author_id", 101); } }); assertEquals(3, posts.size()); } finally { session.close(); } }
@Test public void shouldSelectBlogWithPostsAndAuthorUsingSubSelects() throws Exception { SqlSession session = sqlMapper.openSession(); try { Blog blog = session.selectOne( "org.apache.ibatis.domain.blog.mappers.BlogMapper.selectBlogWithPostsUsingSubSelect", 1); assertEquals("Jim Business", blog.getTitle()); assertEquals(2, blog.getPosts().size()); assertEquals("Corn nuts", blog.getPosts().get(0).getSubject()); assertEquals(101, blog.getAuthor().getId()); assertEquals("jim", blog.getAuthor().getUsername()); } finally { session.close(); } }