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.country.TestSelect.java

License:Open Source License

/**
 * ?null//  w ww. j a  va2 s  .  c  o m
 */
@Test
public void testDynamicSelectAllByNull() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {
        CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
        mapper.select(null);
    } finally {
        sqlSession.close();
    }
}

From source file:com.quancheng.mybatis.mapper.test.country.TestSelect.java

License:Open Source License

/**
 * ??/*from   w  w w  .  java 2s.c o m*/
 */
@Test
public void testDynamicSelect() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {
        CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
        Country country = new Country();
        country.setCountrycode("CN");
        List<Country> countryList = mapper.select(country);

        Assert.assertEquals(1, countryList.size());
        Assert.assertEquals(true, countryList.get(0).getId() == 35);
        Assert.assertEquals("China", countryList.get(0).getCountryname());
    } finally {
        sqlSession.close();
    }
}

From source file:com.quancheng.mybatis.mapper.test.country.TestSelect.java

License:Open Source License

/**
 * ?//from ww w  .j  ava 2s. co  m
 */
@Test
public void testDynamicSelectZero() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {
        CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
        Country country = new Country();
        country.setCountrycode("CN");
        country.setCountryname("?");// China
        List<Country> countryList = mapper.select(country);

        Assert.assertEquals(0, countryList.size());
    } finally {
        sqlSession.close();
    }
}

From source file:com.quancheng.mybatis.mapper.test.country.TestSelect.java

License:Open Source License

/**
 * ?,?/*w  ww .  j  a  va2 s .  com*/
 */
@Test
public void testDynamicSelectNotFoundKeyProperties() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {
        CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
        //?
        Assert.assertEquals(183, mapper.select(new Key()).size());

        Key key = new Key();
        key.setCountrycode("CN");
        key.setCountrytel("+86");
        Assert.assertEquals(1, mapper.select(key).size());
    } finally {
        sqlSession.close();
    }
}

From source file:com.quancheng.mybatis.mapper.test.country.TestSelectAll.java

License:Open Source License

/**
 * //w w w .  j  av  a2 s .  c  o  m
 */
@Test
public void testDynamicSelectPage() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {
        CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);

        List<Country> countryList = mapper.selectAll();
        //
        Assert.assertEquals(183, countryList.size());
    } finally {
        sqlSession.close();
    }
}

From source file:com.quancheng.mybatis.mapper.test.country.TestSelectAll.java

License:Open Source License

/**
 * /*from  ww w  .  j  av a  2 s  .co  m*/
 */
@Test
public void testDynamicSelectPage2() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {
        CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);

        List<Country> countryList = mapper.selectAll();
        //
        Assert.assertEquals(183, countryList.size());
        //selectAll?
        Assert.assertEquals(183, (int) countryList.get(0).getId());
    } finally {
        sqlSession.close();
    }
}

From source file:com.quancheng.mybatis.mapper.test.country.TestSelectByPrimaryKey.java

License:Open Source License

/**
 * ?PK//from   w  ww  . j a  va2  s . co  m
 */
@Test
public void testDynamicSelectByPrimaryKey2() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {
        CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
        Country country = mapper.selectByPrimaryKey(35);

        Assert.assertNotNull(country);
        Assert.assertEquals(true, country.getId() == 35);
        Assert.assertEquals("China", country.getCountryname());
        Assert.assertEquals("CN", country.getCountrycode());
    } finally {
        sqlSession.close();
    }
}

From source file:com.quancheng.mybatis.mapper.test.country.TestSelectByPrimaryKey.java

License:Open Source License

/**
 * ???//w w  w .  j a  v a 2  s .c  o m
 */
@Test
public void testDynamicSelectByPrimaryKey() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {
        CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
        Country country = new Country();
        country.setId(35);
        country = mapper.selectByPrimaryKey(country);
        Assert.assertNotNull(country);
        Assert.assertEquals(true, country.getId() == 35);
        Assert.assertEquals("China", country.getCountryname());
        Assert.assertEquals("CN", country.getCountrycode());
    } finally {
        sqlSession.close();
    }
}

From source file:com.quancheng.mybatis.mapper.test.country.TestSelectByPrimaryKey.java

License:Open Source License

/**
 * ?/*from w  ww .ja v a  2  s. c  om*/
 */
@Test
public void testDynamicSelectByPrimaryKeyZero() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {
        CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
        Assert.assertNull(mapper.selectByPrimaryKey(new Country()));
        Assert.assertNull(mapper.selectByPrimaryKey(new HashMap<String, Object>()));
        Assert.assertNull(mapper.selectByPrimaryKey(-10));
        Assert.assertNull(mapper.selectByPrimaryKey(0));
        Assert.assertNull(mapper.selectByPrimaryKey(1000));
        Assert.assertNull(mapper.selectByPrimaryKey(null));
    } finally {
        sqlSession.close();
    }
}

From source file:com.quancheng.mybatis.mapper.test.country.TestSelectByPrimaryKey.java

License:Open Source License

/**
 * Map???// w  w  w  .  j a  va2 s  . co  m
 */
@Test
public void testSelectByPrimaryKeyMap() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {
        CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);

        Map map = new HashMap();
        map.put("id", 35);
        Country country = mapper.selectByPrimaryKey(map);
        Assert.assertNotNull(country);
        Assert.assertEquals(true, country.getId() == 35);
        Assert.assertEquals("China", country.getCountryname());
        Assert.assertEquals("CN", country.getCountrycode());

        map = new HashMap();
        map.put("countryname", "China");
        Assert.assertNull(mapper.selectByPrimaryKey(map));
    } finally {
        sqlSession.close();
    }
}