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.ids.TestIds.java

License:Open Source License

@Test
public void testSelectByIds() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {/*from  w  w w. ja v a2s  .c o  m*/
        CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
        List<Country> countryList = mapper.selectByIds("1,2,3");
        //
        Assert.assertEquals(3, countryList.size());
    } finally {
        sqlSession.close();
    }
}

From source file:com.quancheng.mybatis.mapper.test.ids.TestIds.java

License:Open Source License

@Test
public void testDeleteByIds() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {/*  www .  java 2s . co m*/
        CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
        int count = mapper.deleteByIds("1,2,3");
        //
        Assert.assertEquals(3, count);
        Assert.assertEquals(180, mapper.selectCount(null));
    } finally {
        sqlSession.rollback();
        sqlSession.close();
    }
}

From source file:com.quancheng.mybatis.mapper.test.jdbc.TestJDBC.java

License:Open Source License

public void testJDBC() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {//from ww w  .  j  a  va  2  s . com
        CountryJDBCMapper mapper = sqlSession.getMapper(CountryJDBCMapper.class);
        CountryJDBC country = new CountryJDBC();
        country.setId(1);
        country.setCountrycode("CN");
        country.setCountryname("China");
        Assert.assertEquals(1, mapper.insert(country));
        Assert.assertNotNull(country.getId());
    } finally {
        sqlSession.rollback();
        sqlSession.close();
    }
}

From source file:com.quancheng.mybatis.mapper.test.jdbc.TestJDBC.java

License:Open Source License

public void testJDBC2() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {//from   w ww.  jav  a2s  .c om
        CountryJDBCMapper mapper = sqlSession.getMapper(CountryJDBCMapper.class);
        CountryJDBC country = new CountryJDBC();
        country.setId(1);
        country.setCountrycode("CN");
        country.setCountryname("China");
        Assert.assertEquals(1, mapper.insertSelective(country));
        Assert.assertNotNull(country.getId());
    } finally {
        sqlSession.rollback();
        sqlSession.close();
    }
}

From source file:com.quancheng.mybatis.mapper.test.mysql.TestMysql.java

License:Open Source License

/**
 * ??/*ww  w  . j av  a2  s .  c o m*/
 */
//?mysqlh2?
//@Test
public void testInsertList() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {
        CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
        List<Country> countryList = new ArrayList<Country>();
        for (int i = 0; i < 10; i++) {
            Country country = new Country();
            country.setCountrycode("CN" + i);
            country.setCountryname("?" + i);
            countryList.add(country);
        }
        int count = mapper.insertList(countryList);
        Assert.assertEquals(10, count);
        for (Country country : countryList) {
            Assert.assertNotNull(country.getId());
        }
    } finally {
        sqlSession.rollback();
        sqlSession.close();
    }
}

From source file:com.quancheng.mybatis.mapper.test.mysql.TestMysql.java

License:Open Source License

/**
 * ??/* ww w  . j a  v  a2s.c  om*/
 */
//?mysqlh2?
//@Test
public void testInsert() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {
        CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
        Country country = new Country();
        country.setCountrycode("CN");
        country.setCountryname("?");
        int count = mapper.insertUseGeneratedKeys(country);
        Assert.assertEquals(1, count);
        Assert.assertNotNull(country.getId());
    } finally {
        sqlSession.rollback();
        sqlSession.close();
    }
}

From source file:com.quancheng.mybatis.mapper.test.rowbounds.TestSelectRowBounds.java

License:Open Source License

@Test
public void testSelectByExample() {
    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().andGreaterThan("id", 100).andLessThan("id", 151);
        example.or().andLessThan("id", 41);
        List<Country> countries = mapper.selectByExampleAndRowBounds(example, new RowBounds(10, 20));
        //
        Assert.assertEquals(20, countries.size());
    } finally {
        sqlSession.close();
    }
}

From source file:com.quancheng.mybatis.mapper.test.transientc.TestTransient.java

License:Open Source License

/**
 * ??//w  w  w .  j  a  va  2  s  .c  om
 */
@Test
public void testDynamicInsert() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {
        CountryTMapper mapper = sqlSession.getMapper(CountryTMapper.class);
        CountryT country = new CountryT();
        country.setId(10086);
        country.setCountrycode("CN");
        country.setCountryname("?");
        Assert.assertEquals(1, mapper.insert(country));

        //CN
        country = new CountryT();
        country.setCountrycode("CN");
        List<CountryT> list = mapper.select(country);

        Assert.assertEquals(2, list.size());
        //??null
        Assert.assertNull(list.get(0).getCountrycode());
        //??,??
        Assert.assertEquals(1, mapper.deleteByPrimaryKey(10086));
    } finally {
        sqlSession.close();
    }
}

From source file:com.quancheng.mybatis.mapper.test.transientc.TestTransient.java

License:Open Source License

/**
 * ??//from ww  w  . j  a  v  a2 s  . co  m
 */
@Test
public void testDynamicUpdateByPrimaryKey() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {
        CountryTMapper mapper = sqlSession.getMapper(CountryTMapper.class);
        CountryT country = new CountryT();
        country.setId(174);
        country.setCountryname("");
        country.setCountrycode("US");
        Assert.assertEquals(1, mapper.updateByPrimaryKey(country));

        country = mapper.selectByPrimaryKey(174);
        Assert.assertNotNull(country);
        Assert.assertEquals(174, (int) country.getId());
        Assert.assertEquals("", country.getCountryname());
        Assert.assertNull(country.getCountrycode());
    } finally {
        sqlSession.close();
    }
}

From source file:com.quancheng.mybatis.mapper.test.transientc.TestTransient.java

License:Open Source License

/**
 * ??// w ww. j a va 2s .c  o m
 */
@Test
public void testDynamicSelect() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {
        CountryTMapper mapper = sqlSession.getMapper(CountryTMapper.class);
        CountryT country = new CountryT();
        country.setId(174);
        country.setCountrycode("US");
        List<CountryT> countryList = mapper.select(country);

        Assert.assertEquals(1, countryList.size());
        Assert.assertEquals(true, countryList.get(0).getId() == 174);
        Assert.assertNotNull(countryList.get(0).getCountryname());
        Assert.assertNull(countryList.get(0).getCountrycode());
    } finally {
        sqlSession.close();
    }
}