mybatis database paging

WBOY
Release: 2016-07-25 09:08:18
Original
1027 people have browsed it

I started to like the interface-based approach of mybatis.
Actually there are two parts of changes (based on the modifications made in the official website demo):
  • Added paging plug-in, simple and easy to use (feel good about yourself).
  • Add a BaseMapper to abstract commonly used methods into this interface to avoid repeated definitions in multiple interfaces (equivalent to common dao base classes).
Based on maven and using the in-memory database hsqldb, junit tests can be run directly.
  1. public interface BaseMapper {
  2. String PO_KEY = "po";
  3. T get(Serializable pk);
  4. Page getPage(@Param(PageInterceptor.PAGE_KEY) Page p, @ Param(PO_KEY) T obj);
  5. }
Copy code
  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @ContextConfiguration({"classpath:applicationContext.xml"})
  3. public class ItemServiceTest {
  4. @Autowired
  5. ItemService service;
  6. @Test
  7. public void testGetItem() {
  8. System.out.println(service.getItem(null).getProduct());
  9. }
  10. @Test
  11. public void testGetPage() {
  12. Page p = new Page();
  13. p.setCurrentPage (1);
  14. p.setSize(10);
  15. Item item = new Item();
  16. item.setProductId("FI-SW-01");
  17. service.getPage(p, item);
  18. System .out.println(p.getTotal() + " " + p.getResult().size());
  19. for(Item i : p.getResult()) {
  20. System.out.println(i.getItemId( ));
  21. }
  22. }
  23. }
Copy code


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template