@Test public void findByHQL() { // 初始化数据中共有6个email为@springside.org.cn的用户 Page<User> page = new Page<User>(5); dao.findPage(page, "from User u where email like ?", "%springside.org.cn%"); assertEquals(5, page.getResult().size()); // 自动统计总数 assertEquals(6L, page.getTotalCount()); // 翻页 page.setPageNo(2); dao.findPage(page, "from User u where email like ?", "%springside.org.cn%"); assertEquals(1, page.getResult().size()); // 命名参数版本 Map<String, String> paraMap = Collections.singletonMap("email", "%springside.org.cn%"); page = new Page<User>(5); dao.findPage(page, "from User u where email like :email", paraMap); assertEquals(5, page.getResult().size()); // 自动统计总数 assertEquals(6L, page.getTotalCount()); // 翻页 page.setPageNo(2); dao.findPage(page, "from User u where email like :email", paraMap); assertEquals(1, page.getResult().size()); }
@Test public void findByProperty() { List<User> users = dao.findBy("loginName", "admin", PropertyFilter.MatchType.EQ); assertEquals(1, users.size()); assertEquals("admin", users.get(0).getLoginName()); users = dao.findBy("email", "springside.org.cn", PropertyFilter.MatchType.LIKE); assertEquals(6, users.size()); assertTrue(users.get(0).getEmail().indexOf("springside.org.cn") != -1); }
@Test public void findPageByHqlAutoCount() { Page<User> page = new Page<User>(5); dao.findPage(page, "from User user"); assertEquals(6L, page.getTotalCount()); dao.findPage(page, "select user from User user"); assertEquals(6L, page.getTotalCount()); dao.findPage(page, "select user from User user order by id"); assertEquals(6L, page.getTotalCount()); }
@Test public void getAll() { // 初始化数据中共有6个用户 Page<User> page = new Page<User>(5); dao.getAll(page); assertEquals(5, page.getResult().size()); // 自动统计总数 assertEquals(6L, page.getTotalCount()); page.setPageNo(2); dao.getAll(page); assertEquals(1, page.getResult().size()); }
@Test public void findByCriterion() { // 初始化数据中共有6个email为@springside.org.cn的用户 Page<User> page = new Page<User>(5); Criterion c = Restrictions.like("email", "springside.org.cn", MatchMode.ANYWHERE); dao.findPage(page, c); assertEquals(5, page.getResult().size()); // 自动统计总数 assertEquals(6L, page.getTotalCount()); // 翻页 page.setPageNo(2); dao.findPage(page, c); assertEquals(1, page.getResult().size()); }
/** 重载函数,因为Role中没有建立与User的关联,因此需要以较低效率的方式进行删除User与Role的多对多中间表. */ @SuppressWarnings("unchecked") @Override public void delete(Long id) { Role role = get(id); // 查询出拥有该角色的用户,并删除该用户的角色. List<User> users = createQuery(QUERY_USER_BY_ROLEID, role.getId()).list(); for (User u : users) { u.getRoleList().remove(role); } super.delete(role); }
@Test public void findByCriterionWithOrder() { // 初始化数据中共有6个email为@springside.org.cn的用户 Page<User> page = new Page<User>(5); page.setOrderBy("name,loginName"); page.setOrder(Page.DESC + "," + Page.ASC); Criterion c = Restrictions.like("email", "springside.org.cn", MatchMode.ANYWHERE); dao.findPage(page, c); assertEquals("Sawyer", page.getResult().get(0).getName()); }
@Test public void findByFilters() { List<PropertyFilter> filters; // EQ filter PropertyFilter eqFilter = new PropertyFilter("EQS_loginName", "admin"); filters = Lists.newArrayList(eqFilter); List<User> users = dao.find(filters); assertEquals(1, users.size()); assertEquals("admin", users.get(0).getLoginName()); // LIKE filter PropertyFilter likeFilter = new PropertyFilter("LIKES_email", "springside.org.cn"); filters = Lists.newArrayList(likeFilter); users = dao.find(filters); assertEquals(6, users.size()); assertTrue(users.get(0).getEmail().indexOf("springside.org.cn") != -1); // Filter with Page Page<User> page = new Page<User>(5); dao.findPage(page, filters); assertEquals(5, page.getResult().size()); assertEquals(6L, page.getTotalCount()); page.setPageNo(2); dao.findPage(page, filters); assertEquals(1, page.getResult().size()); // Date and LT/GT filter PropertyFilter dateLtFilter = new PropertyFilter("LTD_createTime", "2046-01-01"); filters = Lists.newArrayList(dateLtFilter); users = dao.find(filters); assertEquals(6, users.size()); PropertyFilter dateGtFilter = new PropertyFilter("GTD_createTime", "2046-01-01 10:00:22"); filters = Lists.newArrayList(dateGtFilter); users = dao.find(filters); assertEquals(0, users.size()); }
/** * {@inheritDoc} * * @since 2012-7-28 * @see com.drcl.yz.api.IntroductionDao#batchDelete(java.util.List) */ @Override public void batchDelete(List<Long> ids) { String hql = "delete from Introduction where id in(:ids)"; Map<String, List<Long>> values = Collections.singletonMap("ids", ids); super.batchExecute(hql, values); }
@Test public void isPropertyUnique() { assertEquals(true, dao.isPropertyUnique("loginName", "admin", "admin")); assertEquals(true, dao.isPropertyUnique("loginName", "user6", "admin")); assertEquals(false, dao.isPropertyUnique("loginName", "user2", "admin")); }