Example usage for org.apache.ibatis.session SqlSession close

List of usage examples for org.apache.ibatis.session SqlSession close

Introduction

In this page you can find the example usage for org.apache.ibatis.session SqlSession close.

Prototype

@Override
void close();

Source Link

Document

Closes the session.

Usage

From source file:com.github.pagehelper.test.pagesize.PageSizeLessThenOrEqualZeroTest.java

License:Open Source License

/**
 * Mapper?PageHelper.startPage??Mapper??
 *///from   w  ww .  jav  a 2s . co  m
@Test
public void testWithRowbounds() {
    //?MybatisRowBoundsHelpercount
    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, 0));
        PageInfo<Country> page = new PageInfo<Country>(list);
        assertEquals(0, list.size());
        assertEquals(-1, page.getTotal());

        //limit<0?
        list = countryMapper.selectAll(new RowBounds(1, -100));
        page = new PageInfo<Country>(list);
        assertEquals(0, list.size());
        assertEquals(-1, page.getTotal());
    } finally {
        sqlSession.close();
    }
}

From source file:com.github.pagehelper.test.pagesize.PageSizeZeroTest.java

License:Open Source License

/**
 * Mapper?PageHelper.startPage??Mapper??
 *///from ww  w. j a va  2s.  com
@Test
public void testWithStartPage() {
    SqlSession sqlSession = MybatisPageSizeZeroHelper.getSqlSession();
    CountryMapper countryMapper = sqlSession.getMapper(CountryMapper.class);
    try {
        //pageSize=0
        PageHelper.startPage(1, 0);
        List<Country> list = countryMapper.selectAll();
        PageInfo<Country> page = new PageInfo<Country>(list);
        assertEquals(183, list.size());
        assertEquals(183, page.getTotal());

        //pageSize=0
        PageHelper.startPage(10, 0);
        list = countryMapper.selectAll();
        page = new PageInfo<Country>(list);
        assertEquals(183, list.size());
        assertEquals(183, page.getTotal());
    } finally {
        sqlSession.close();
    }
}

From source file:com.github.pagehelper.test.pagesize.PageSizeZeroTest.java

License:Open Source License

/**
 * Mapper?PageHelper.startPage??Mapper??
 *///  w  ww .j a  v a  2 s .c  o  m
@Test
public void testWithRowbounds() {
    SqlSession sqlSession = MybatisPageSizeZeroHelper.getSqlSession();
    CountryMapper countryMapper = sqlSession.getMapper(CountryMapper.class);
    try {
        //pageSize=0
        List<Country> list = countryMapper.selectAll(new RowBounds(1, 0));
        PageInfo<Country> page = new PageInfo<Country>(list);
        assertEquals(183, list.size());
        assertEquals(183, page.getTotal());

        //pageSize=0
        PageHelper.startPage(10, 0);
        list = countryMapper.selectAll(new RowBounds(1000, 0));
        page = new PageInfo<Country>(list);
        assertEquals(183, list.size());
        assertEquals(183, page.getTotal());
    } finally {
        sqlSession.close();
    }
}

From source file:com.github.pagehelper.test.reasonable.PageTest.java

License:Open Source License

/**
 * Mapper?PageHelper.startPage??Mapper??
 *///from w ww . j a va  2 s . co  m
@Test
public void testMapperWithStartPage() {
    SqlSession sqlSession = MybatisReasonableHelper.getSqlSession();
    CountryMapper countryMapper = sqlSession.getMapper(CountryMapper.class);
    try {
        //?202?
        //??
        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());

        //?-32?
        //?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();
    }
}

From source file:com.github.pagehelper.test.rowbounds.RowBoundsTest.java

License:Open Source License

/**
 * Mapper??RowBounds???xml??//from   w w w. j  a  v  a  2s . com
 * <p/>
 * RowBounds??count?Page?
 * <p/>
 * ??startPagestartPage
 */
@Test
public void testMapperWithRowBounds() {
    SqlSession sqlSession = MybatisRowBoundsHelper.getSqlSession();
    CountryMapper countryMapper = sqlSession.getMapper(CountryMapper.class);
    try {
        //?110?count
        List<Country> list = countryMapper.selectAll(new RowBounds(1, 10));
        //PageInfo?
        PageInfo<Country> page = new PageInfo<Country>(list);
        assertEquals(10, list.size());
        assertEquals(183, page.getTotal());
        //??
        assertEquals(1, list.get(0).getId());
        assertEquals(10, list.get(list.size() - 1).getId());

        //?1010??count
        list = countryMapper.selectAll(new RowBounds(10, 10));
        assertEquals(10, list.size());
        assertEquals(183, ((Page<?>) list).getTotal());
        //??
        assertEquals(91, list.get(0).getId());
        assertEquals(100, list.get(list.size() - 1).getId());

        //?320?count
        list = countryMapper.selectAll(new RowBounds(6, 20));
        assertEquals(20, list.size());
        assertEquals(183, ((Page<?>) list).getTotal());
        //??
        assertEquals(101, list.get(0).getId());
        assertEquals(120, list.get(list.size() - 1).getId());
    } finally {
        sqlSession.close();
    }
}

From source file:com.github.pagehelper.test.rowbounds.RowBoundsTest.java

License:Open Source License

/**
 * ???RowBoundsRowBounds?count/*from w ww  . ja  va 2 s  .c om*/
 * ??count??
 * ?count?startPage
 * <p/>
 * ?startPagestartPage?startPage??
 */
@Test
public void testNamespaceWithRowBounds() {
    SqlSession sqlSession = MybatisRowBoundsHelper.getSqlSession();
    try {
        //?010?
        List<Country> list = sqlSession.selectList("selectAll", null, new RowBounds(1, 10));
        assertEquals(10, list.size());
        assertEquals(183, ((Page<?>) list).getTotal());
        //??
        assertEquals(1, list.get(0).getId());
        assertEquals(10, list.get(list.size() - 1).getId());

        //?1010?
        list = sqlSession.selectList("selectAll", null, new RowBounds(10, 10));
        assertEquals(10, list.size());
        assertEquals(183, ((Page<?>) list).getTotal());
        //??
        assertEquals(91, list.get(0).getId());
        assertEquals(100, list.get(list.size() - 1).getId());

        //?2020?
        list = sqlSession.selectList("selectAll", null, new RowBounds(6, 20));
        assertEquals(20, list.size());
        assertEquals(183, ((Page<?>) list).getTotal());
        //??
        assertEquals(101, list.get(0).getId());
        assertEquals(120, list.get(list.size() - 1).getId());
    } finally {
        sqlSession.close();
    }
}

From source file:com.github.pagehelper.test.rowbounds.RowBoundsTest.java

License:Open Source License

@Test
public void testNamespaceWithRowBounds2() {
    SqlSession sqlSession = MybatisRowBoundsHelper.getSqlSession();
    try {/*from  www  .j  ava  2 s .c  o m*/
        //?010?
        List<Country> list = sqlSession.selectList("selectIf", null, new RowBounds(1, 10));
        assertEquals(10, list.size());
        assertEquals(183, ((Page<?>) list).getTotal());
        //??
        assertEquals(1, list.get(0).getId());
        assertEquals(10, list.get(list.size() - 1).getId());

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", 10);
        //?1010?
        list = sqlSession.selectList("selectIf", map, new RowBounds(10, 10));
        assertEquals(10, list.size());
        assertEquals(173, ((Page<?>) list).getTotal());
        //??
        assertEquals(101, list.get(0).getId());
        assertEquals(110, list.get(list.size() - 1).getId());
    } finally {
        sqlSession.close();
    }
}

From source file:com.github.pagehelper.test.rowbounds.RowBoundsTest.java

License:Open Source License

@Test
public void testNamespaceWithRowBounds3() {
    SqlSession sqlSession = MybatisRowBoundsHelper.getSqlSession();
    try {/*from  w  w w .java 2s . c o  m*/
        //?010?
        PageHelper.startPage(1, 10);
        List<Country> list = sqlSession.selectList("selectIf", null);
        assertEquals(10, list.size());
        assertEquals(183, ((Page<?>) list).getTotal());
        //??
        assertEquals(1, list.get(0).getId());
        assertEquals(10, list.get(list.size() - 1).getId());

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", 10);
        //?1010?
        PageHelper.startPage(10, 10);
        list = sqlSession.selectList("selectIf", map);
        assertEquals(10, list.size());
        assertEquals(173, ((Page<?>) list).getTotal());
        //??
        assertEquals(101, list.get(0).getId());
        assertEquals(110, list.get(list.size() - 1).getId());

        IdBean country = new IdBean();
        //?1010?
        PageHelper.startPage(10, 10);
        list = sqlSession.selectList("selectIf", country);
        assertEquals(10, list.size());
        assertEquals(183, ((Page<?>) list).getTotal());
        //??
        assertEquals(91, list.get(0).getId());
        assertEquals(100, list.get(list.size() - 1).getId());
    } finally {
        sqlSession.close();
    }
}

From source file:com.github.pagehelper.test.rowbounds.RowBoundsTest.java

License:Open Source License

/**
 * Mapper?PageHelper.startPage??Mapper??
 *//* w  w  w.  jav a 2 s.com*/
@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, 0));
        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();
    }
}

From source file:com.glaf.base.test.UserTest2.java

License:Apache License

public static void main(String[] args) {
    long start = System.currentTimeMillis();
    SqlSession session = sqlSessionFactory.openSession();
    try {//from www  . j  a va  2 s .c  o m
        User user = session.selectOne("getUserById", "root");
        System.out.println(user.toJsonObject().toJSONString());
    } finally {
        session.close();
    }
    long time = System.currentTimeMillis() - start;
    System.out.println("" + (time));
}