/** * WARNING: don't do stupid things such like below, we do this because we can guarantee the shard * id will NOT change. if you want to use cobar client corretly, make sure you are partitioning * you databases with shard id that will not be changed once it's created!!! */ public void testUpdateOnCobarSqlMapClientTemplate() { String[] names = {"Aaron", "Amily", "Aragon", "Darren", "Darwin"}; batchInsertMultipleFollowersAsFixture(names); String nameSuffix = "Wang"; for (String name : names) { Follower follower = (Follower) getSqlMapClientTemplate() .queryForObject("com.alibaba.cobar.client.entities.Follower.finaByName", name); assertNotNull(follower); follower.setName(follower.getName() + nameSuffix); getSqlMapClientTemplate() .update("com.alibaba.cobar.client.entities.Follower.update", follower); Long id = follower.getId(); follower = null; follower = (Follower) getSqlMapClientTemplate() .queryForObject("com.alibaba.cobar.client.entities.Follower.finaByName", name); assertNull(follower); follower = (Follower) getSqlMapClientTemplate() .queryForObject("com.alibaba.cobar.client.entities.Follower.load", id); assertNotNull(follower); assertEquals(name + nameSuffix, follower.getName()); } }
public void testQueryForListOnCobarSqlMapClientTemplate() { // 1. initialize data String[] names = {"Aaron", "Amily", "Aragon", "Darren", "Darwin"}; batchInsertMultipleFollowersAsFixture(names); // 2. perform assertion @SuppressWarnings("unchecked") List<Follower> resultList = (List<Follower>) getSqlMapClientTemplate() .queryForList("com.alibaba.cobar.client.entities.Follower.findAll"); assertTrue(CollectionUtils.isNotEmpty(resultList)); assertEquals(5, resultList.size()); for (Follower f : resultList) { assertTrue(ArrayUtils.contains(names, f.getName())); } // 3. perform assertion with another different query @SuppressWarnings("unchecked") List<Follower> followersWithNameStartsWithA = (List<Follower>) getSqlMapClientTemplate() .queryForList("com.alibaba.cobar.client.entities.Follower.finaByNameAlike", "A"); assertTrue(CollectionUtils.isNotEmpty(followersWithNameStartsWithA)); assertEquals(3, followersWithNameStartsWithA.size()); for (Follower f : followersWithNameStartsWithA) { assertTrue(ArrayUtils.contains(names, f.getName())); } }