Search in sources :

Example 21 with CountryMapper

use of com.github.pagehelper.mapper.CountryMapper in project Mybatis-PageHelper by pagehelper.

the class RowBoundsTest method testWithRowboundsAndCountTrue.

/**
     * 使用Mapper接口调用时,使用PageHelper.startPage效果更好,不需要添加Mapper接口参数
     */
@Test
public void testWithRowboundsAndCountTrue() {
    SqlSession sqlSession = MybatisRowBoundsHelper.getSqlSession();
    CountryMapper countryMapper = sqlSession.getMapper(CountryMapper.class);
    try {
        //limit=0,这时候相当于用分页插件求count,但是前提必须是配置rounbounds方式求count,否则都是-1
        //这里由于没有配置,应该都是-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());
        //pageSize<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 22 with CountryMapper

use of com.github.pagehelper.mapper.CountryMapper in project Mybatis-PageHelper by pagehelper.

the class TestParameterNone method testMapperWithStartPage.

/**
     * 使用Mapper接口调用时,使用PageHelper.startPage效果更好,不需要添加Mapper接口参数
     */
@Test
public void testMapperWithStartPage() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    CountryMapper countryMapper = sqlSession.getMapper(CountryMapper.class);
    try {
        //获取第1页,10条内容,默认查询总数count
        PageHelper.startPage(1, 10);
        List<Country> list = countryMapper.selectAll();
        assertEquals(1, list.get(0).getId());
        assertEquals(10, list.size());
        assertEquals(183, ((Page<?>) list).getTotal());
    } finally {
        sqlSession.close();
    }
}
Also used : SqlSession(org.apache.ibatis.session.SqlSession) CountryMapper(com.github.pagehelper.mapper.CountryMapper) Country(com.github.pagehelper.model.Country) Test(org.junit.Test)

Example 23 with CountryMapper

use of com.github.pagehelper.mapper.CountryMapper in project Mybatis-PageHelper by pagehelper.

the class TestProvider method testProvider.

@Test
public void testProvider() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("id", 100);
    CountryMapper countryMapper = sqlSession.getMapper(CountryMapper.class);
    try {
        PageHelper.startPage(1, 10);
        List<Country> list = countryMapper.selectByProvider(map);
        assertEquals(100, list.get(0).getId());
        assertEquals(1, list.size());
        assertEquals(1, ((Page<?>) list).getTotal());
        map.put("countryname", "天朝");
        PageHelper.startPage(1, 10);
        list = countryMapper.selectByProvider(map);
        assertEquals(0, list.size());
    } finally {
        sqlSession.close();
    }
}
Also used : SqlSession(org.apache.ibatis.session.SqlSession) HashMap(java.util.HashMap) CountryMapper(com.github.pagehelper.mapper.CountryMapper) Country(com.github.pagehelper.model.Country) Test(org.junit.Test)

Example 24 with CountryMapper

use of com.github.pagehelper.mapper.CountryMapper in project Mybatis-PageHelper by pagehelper.

the class TestProvider method testCountryProvider.

@Test
public void testCountryProvider() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    Country country = new Country();
    country.setId(100);
    CountryMapper countryMapper = sqlSession.getMapper(CountryMapper.class);
    try {
        PageHelper.startPage(1, 10);
        List<Country> list = countryMapper.selectByCountryProvider(country);
        assertEquals(100, list.get(0).getId());
        assertEquals(1, list.size());
        assertEquals(1, ((Page<?>) list).getTotal());
        country.setCountryname("天朝");
        PageHelper.startPage(1, 10);
        list = countryMapper.selectByCountryProvider(country);
        assertEquals(0, list.size());
    } finally {
        sqlSession.close();
    }
}
Also used : SqlSession(org.apache.ibatis.session.SqlSession) CountryMapper(com.github.pagehelper.mapper.CountryMapper) Country(com.github.pagehelper.model.Country) Test(org.junit.Test)

Example 25 with CountryMapper

use of com.github.pagehelper.mapper.CountryMapper in project Mybatis-PageHelper by pagehelper.

the class TestExists method testExists.

/**
     * union的count查询sql特殊
     */
@Test
public void testExists() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    CountryMapper countryMapper = sqlSession.getMapper(CountryMapper.class);
    try {
        //获取第1页,10条内容,默认查询总数count
        PageHelper.startPage(1, 10);
        List<Country> list = countryMapper.selectExists();
        assertEquals(101, list.get(0).getId());
        assertEquals(10, list.size());
        assertEquals(83, ((Page<?>) list).getTotal());
        //获取第1页,10条内容,默认查询总数count
        PageHelper.startPage(2, 10);
        list = countryMapper.selectExists();
        assertEquals(111, list.get(0).getId());
        assertEquals(10, list.size());
        assertEquals(83, ((Page<?>) list).getTotal());
    } finally {
        sqlSession.close();
    }
}
Also used : SqlSession(org.apache.ibatis.session.SqlSession) CountryMapper(com.github.pagehelper.mapper.CountryMapper) Country(com.github.pagehelper.model.Country) Test(org.junit.Test)

Aggregations

CountryMapper (com.github.pagehelper.mapper.CountryMapper)57 Country (com.github.pagehelper.model.Country)57 SqlSession (org.apache.ibatis.session.SqlSession)57 Test (org.junit.Test)57 PageInfo (com.github.pagehelper.PageInfo)11 RowBounds (org.apache.ibatis.session.RowBounds)7 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)4 PageRowBounds (com.github.pagehelper.PageRowBounds)2 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