Search in sources :

Example 36 with Country

use of com.github.pagehelper.model.Country in project Mybatis-PageHelper by pagehelper.

the class BasicTest method testNamespace1.

@Test
public void testNamespace1() {
    SqlSession sqlSession = MybatisRowBoundsHelper.getSqlSession();
    try {
        Map<String, Object> map = new HashMap<String, Object>();
        Country country = new Country();
        country.setCountryname("China");
        map.put("country", country);
        //同时测试不可变Map
        map = Collections.unmodifiableMap(map);
        List<Country> list = sqlSession.selectList("select1", map, new RowBounds(1, 10));
        assertEquals(1, list.size());
        //判断查询结果的位置是否正确
        assertEquals(35, list.get(0).getId());
    } finally {
        sqlSession.close();
    }
}
Also used : SqlSession(org.apache.ibatis.session.SqlSession) HashMap(java.util.HashMap) Country(com.github.pagehelper.model.Country) RowBounds(org.apache.ibatis.session.RowBounds) Test(org.junit.Test)

Example 37 with Country

use of com.github.pagehelper.model.Country in project Mybatis-PageHelper by pagehelper.

the class BasicTest method testNamespace2.

@Test
public void testNamespace2() {
    SqlSession sqlSession = MybatisRowBoundsHelper.getSqlSession();
    try {
        Map<String, Object> map = new HashMap<String, Object>();
        Country country = new Country();
        country.setCountryname("China");
        map.put("country", country);
        PageHelper.startPage(1, 10);
        List<Country> list = sqlSession.selectList("select1", map);
        assertEquals(1, list.size());
        //判断查询结果的位置是否正确
        assertEquals(35, list.get(0).getId());
    } finally {
        sqlSession.close();
    }
}
Also used : SqlSession(org.apache.ibatis.session.SqlSession) HashMap(java.util.HashMap) Country(com.github.pagehelper.model.Country) Test(org.junit.Test)

Example 38 with Country

use of com.github.pagehelper.model.Country in project Mybatis-PageHelper by pagehelper.

the class PageSizeLessThenOrEqualZeroTest method testWithStartPage.

/**
     * 使用Mapper接口调用时,使用PageHelper.startPage效果更好,不需要添加Mapper接口参数
     */
@Test
public void testWithStartPage() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    CountryMapper countryMapper = sqlSession.getMapper(CountryMapper.class);
    try {
        //pageSize=0,这时候相当于用分页插件求count
        PageHelper.startPage(1, 0);
        List<Country> list = countryMapper.selectAll();
        PageInfo<Country> page = new PageInfo<Country>(list);
        assertEquals(183, list.size());
        assertEquals(183, page.getTotal());
        //limit<0的时候同上
        PageHelper.startPage(1, -100);
        list = countryMapper.selectAll();
        page = new PageInfo<Country>(list);
        assertEquals(0, list.size());
        assertEquals(183, page.getTotal());
    } finally {
        sqlSession.close();
    }
}
Also used : PageInfo(com.github.pagehelper.PageInfo) SqlSession(org.apache.ibatis.session.SqlSession) CountryMapper(com.github.pagehelper.mapper.CountryMapper) Country(com.github.pagehelper.model.Country) Test(org.junit.Test)

Example 39 with Country

use of com.github.pagehelper.model.Country in project Mybatis-PageHelper by pagehelper.

the class PageSizeLessThenOrEqualZeroTest method testWithRowbounds.

/**
     * 使用Mapper接口调用时,使用PageHelper.startPage效果更好,不需要添加Mapper接口参数
     */
@Test
public void testWithRowbounds() {
    //注意这里是MybatisRowBoundsHelper,会求count
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    CountryMapper countryMapper = sqlSession.getMapper(CountryMapper.class);
    try {
        //limit=0,这时候相当于用分页插件求count,但是前提必须是配置rounbounds方式求count,否则都是-1
        List<Country> list = countryMapper.selectAll(new RowBounds(1, -1));
        PageInfo<Country> page = new PageInfo<Country>(list);
        assertEquals(0, list.size());
        assertEquals(183, page.getTotal());
        //limit<0的时候同上
        list = countryMapper.selectAll(new RowBounds(1, -100));
        page = new PageInfo<Country>(list);
        assertEquals(0, list.size());
        assertEquals(183, page.getTotal());
    } finally {
        sqlSession.close();
    }
}
Also used : PageInfo(com.github.pagehelper.PageInfo) SqlSession(org.apache.ibatis.session.SqlSession) CountryMapper(com.github.pagehelper.mapper.CountryMapper) RowBounds(org.apache.ibatis.session.RowBounds) Country(com.github.pagehelper.model.Country) Test(org.junit.Test)

Example 40 with Country

use of com.github.pagehelper.model.Country in project Mybatis-PageHelper by pagehelper.

the class PageTest method testMapperWithStartPage.

/**
     * 使用Mapper接口调用时,使用PageHelper.startPage效果更好,不需要添加Mapper接口参数
     */
@Test
public void testMapperWithStartPage() {
    SqlSession sqlSession = MybatisReasonableHelper.getSqlSession();
    CountryMapper countryMapper = sqlSession.getMapper(CountryMapper.class);
    try {
        //获取第20页,2条内容
        //分页插件会自动改为查询最后一页
        PageHelper.startPage(20, 50);
        List<Country> list = countryMapper.selectAll();
        PageInfo<Country> page = new PageInfo<Country>(list);
        assertEquals(33, list.size());
        assertEquals(151, page.getStartRow());
        assertEquals(4, page.getPageNum());
        assertEquals(183, page.getTotal());
        //获取第-3页,2条内容
        //由于只有7天数据,分页插件会自动改为查询最后一页
        PageHelper.startPage(-3, 50);
        list = countryMapper.selectAll();
        page = new PageInfo<Country>(list);
        assertEquals(50, list.size());
        assertEquals(1, page.getStartRow());
        assertEquals(1, page.getPageNum());
        assertEquals(183, page.getTotal());
    } finally {
        sqlSession.close();
    }
}
Also used : PageInfo(com.github.pagehelper.PageInfo) SqlSession(org.apache.ibatis.session.SqlSession) CountryMapper(com.github.pagehelper.mapper.CountryMapper) Country(com.github.pagehelper.model.Country) Test(org.junit.Test)

Aggregations

Country (com.github.pagehelper.model.Country)71 SqlSession (org.apache.ibatis.session.SqlSession)71 Test (org.junit.Test)71 CountryMapper (com.github.pagehelper.mapper.CountryMapper)57 RowBounds (org.apache.ibatis.session.RowBounds)14 HashMap (java.util.HashMap)12 PageInfo (com.github.pagehelper.PageInfo)11 ArrayList (java.util.ArrayList)5 PageRowBounds (com.github.pagehelper.PageRowBounds)4 CountryExample (com.github.pagehelper.model.CountryExample)2 ISelect (com.github.pagehelper.ISelect)1 CountryQueryModel (com.github.pagehelper.model.CountryQueryModel)1 Map (java.util.Map)1