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

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

Introduction

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

Prototype

<T> T getMapper(Class<T> type);

Source Link

Document

Retrieves a mapper.

Usage

From source file:com.quancheng.mybatis.mapper.test.example.TestSelectByExample.java

License:Open Source License

@Test
public void testAndExample() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {//from   w  w  w . ja v a 2  s  . c  o  m
        CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
        Example example = new Example(Country.class);
        example.createCriteria().andCondition("countryname like 'C%' and id < 100")
                .andCondition("length(countryname) = ", 5)
                .andCondition("countrycode =", "CN", StringTypeHandler.class);
        List<Country> countries = mapper.selectByExample(example);
        //
        Assert.assertEquals(1, countries.size());
    } finally {
        sqlSession.close();
    }
}

From source file:com.quancheng.mybatis.mapper.test.example.TestSelectByExample.java

License:Open Source License

@Test
public void testSelectByExampleInNotIn() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {/*from  w ww .j  ava2s.  com*/
        CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
        Example example = new Example(Country.class);
        Set<Integer> set = new HashSet<Integer>();
        set.addAll(Arrays.asList(new Integer[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }));
        example.createCriteria().andIn("id", set).andNotIn("id", Arrays.asList(new Object[] { 11 }));
        List<Country> countries = mapper.selectByExample(example);
        //
        Assert.assertEquals(10, countries.size());
    } finally {
        sqlSession.close();
    }
}

From source file:com.quancheng.mybatis.mapper.test.example.TestSelectByExample.java

License:Open Source License

@Test
public void testSelectByExampleInNotIn2() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {/*from w  w  w  .ja v a 2s.c  om*/
        CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
        Example example = new Example(Country.class);
        example.createCriteria().andIn("id", Arrays.asList(new Integer[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }))
                .andNotIn("id", Arrays.asList(new Object[] { 11 }));
        List<Country> countries = mapper.selectByExample(example);
        //
        Assert.assertEquals(10, countries.size());
    } finally {
        sqlSession.close();
    }
}

From source file:com.quancheng.mybatis.mapper.test.example.TestSelectByExample.java

License:Open Source License

@Test
public void testSelectByExample2() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {/*from  ww w.j a va 2s .c o  m*/
        CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
        Example example = new Example(Country.class);
        example.createCriteria().andLike("countryname", "A%");
        example.or().andGreaterThan("id", 100);
        example.setDistinct(true);
        List<Country> countries = mapper.selectByExample(example);
        //
        Assert.assertEquals(true, countries.size() > 83);
    } finally {
        sqlSession.close();
    }
}

From source file:com.quancheng.mybatis.mapper.test.example.TestSelectByExample.java

License:Open Source License

@Test
public void testSelectByExample3() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {/*w  w w  . java  2s  .  com*/
        CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
        CountryExample example = new CountryExample();
        example.createCriteria().andCountrynameLike("A%");
        example.or().andIdGreaterThan(100);
        example.setDistinct(true);
        List<Country> countries = mapper.selectByExample(example);
        //
        Assert.assertEquals(true, countries.size() > 83);
    } finally {
        sqlSession.close();
    }
}

From source file:com.quancheng.mybatis.mapper.test.example.TestSelectByExample.java

License:Open Source License

@Test
public void testSelectByExample4() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {/*from   w  w  w.j a va2  s. co  m*/
        Country ct = new Country();
        ct.setCountryname("China");

        CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
        Example example = new Example(Country.class);
        example.createCriteria().andGreaterThan("id", 20).andEqualTo(ct);
        List<Country> countries = mapper.selectByExample(example);
        //
        System.out.println(countries.get(0).toString());
        Assert.assertEquals(1, countries.size());
    } finally {
        sqlSession.close();
    }
}

From source file:com.quancheng.mybatis.mapper.test.example.TestSelectByExample.java

License:Open Source License

@Test
public void testSelectColumnsByExample() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {//from  www  . j  a v a2  s . co m
        CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
        Example example = new Example(Country.class);
        example.createCriteria().andGreaterThan("id", 100).andLessThan("id", 151);
        example.or().andLessThan("id", 41);
        example.selectProperties("id", "countryname", "hehe");
        List<Country> countries = mapper.selectByExample(example);
        //
        Assert.assertEquals(90, countries.size());
    } finally {
        sqlSession.close();
    }
}

From source file:com.quancheng.mybatis.mapper.test.example.TestSelectByExample.java

License:Open Source License

@Test
public void testOrderBy() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {/*from   w  w w  .  ja  va2 s .c  o m*/
        CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
        Example example = new Example(Country.class);
        //            example.setOrderByClause("id desc");
        example.orderBy("id").desc().orderBy("countryname").orderBy("countrycode").asc();
        List<Country> countries = mapper.selectByExample(example);
        //
        Assert.assertEquals(183, (int) countries.get(0).getId());
    } finally {
        sqlSession.close();
    }
}

From source file:com.quancheng.mybatis.mapper.test.example.TestSelectCountByExample.java

License:Open Source License

@Test
public void testSelectCountByExample() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {// ww w  . j  a va2s .c o m
        CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
        Example example = new Example(Country.class);
        example.createCriteria().andGreaterThan("id", 100);
        int count = mapper.selectCountByExample(example);
        //
        Assert.assertEquals(83, count);
    } finally {
        sqlSession.close();
    }
}

From source file:com.quancheng.mybatis.mapper.test.example.TestSelectCountByExample.java

License:Open Source License

@Test
public void testSelectCountByExample2() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {/*  w  ww .  j  av  a2 s .  c  om*/
        CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
        Example example = new Example(Country.class);
        example.createCriteria().andLike("countryname", "A%");
        example.or().andGreaterThan("id", 100);
        example.setDistinct(true);
        int count = mapper.selectCountByExample(example);
        //
        Assert.assertEquals(true, count > 83);
    } finally {
        sqlSession.close();
    }
}