Java Code Examples for org.apache.ibatis.session.SqlSession#selectList()

The following examples show how to use org.apache.ibatis.session.SqlSession#selectList() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DynSqlTest.java    From mybaties with Apache License 2.0 6 votes vote down vote up
@Test
public void testSelectSimple() {
  SqlSession sqlSession = sqlSessionFactory.openSession();
  try {
    List<Integer> ids = new ArrayList<Integer>();
    ids.add(1);
    ids.add(3);
    ids.add(5);
    Parameter parameter = new Parameter();
    parameter.setEnabled(true);
    parameter.setSchema("ibtest");
    parameter.setIds(ids);

    List<Map<String, Object>> answer = sqlSession.selectList("org.apache.ibatis.submitted.dynsql.select_simple", parameter);

    assertTrue(answer.size() == 3);
  } finally {
    sqlSession.close();
  }
}
 
Example 2
Source File: LanguageTest.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@Test
public void testDynamicSelectWithIteration() {
  SqlSession sqlSession = sqlSessionFactory.openSession();
  try {

    int[] ids = { 2, 4, 5 };
    Map<String, Object> param = new HashMap<String, Object>();
    param.put("ids", ids);
    List<Name> answer = sqlSession.selectList("selectNamesWithIteration", param);
    assertEquals(3, answer.size());
    for (int i = 0; i < ids.length; i++) {
      assertEquals(ids[i], answer.get(i).getId());
    }

  } finally {
    sqlSession.close();
  }
}
 
Example 3
Source File: CriterionTest.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@Test
public void testSimpleSelect() {
  SqlSession sqlSession = sqlSessionFactory.openSession();
  try {
    Criterion criterion = new Criterion();
    criterion.setTest("firstName =");
    criterion.setValue("Fred");
    Parameter parameter = new Parameter();
    parameter.setCriterion(criterion);

    List<Map<String, Object>> answer =
        sqlSession.selectList("org.apache.ibatis.submitted.criterion.simpleSelect", parameter);

    assertEquals(1, answer.size());
  } finally {
    sqlSession.close();
  }
}
 
Example 4
Source File: NestedForEachTest.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@Test
public void testNestedSelect() {
  SqlSession sqlSession = sqlSessionFactory.openSession();
  try {
    Name name = new Name();
    name.setLastName("Flintstone");
    name.addFirstName("Fred");
    name.addFirstName("Wilma");

    Parameter parameter = new Parameter();
    parameter.addName(name);

    List<Map<String, Object>> answer =
        sqlSession.selectList("org.apache.ibatis.submitted.nested.Mapper.nestedSelect", parameter);

    assertEquals(2, answer.size());
  } finally {
    sqlSession.close();
  }
}
 
Example 5
Source File: NoParamTypeTest.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldAcceptDifferentTypeInTheSameBatch() {
  SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
  try {
    ObjA a = new ObjA();
    a.setId(1);
    a.setName(111);
    sqlSession.insert("insertUser", a);
    ObjB b = new ObjB();
    b.setId(2);
    b.setName("222");
    sqlSession.insert("insertUser", b);
    List<BatchResult> batchResults = sqlSession.flushStatements();
    batchResults.clear();
    sqlSession.clearCache();
    sqlSession.commit();
    List<User> users = sqlSession.selectList("selectUser");
    assertEquals(2, users.size());
  } finally {
    sqlSession.close();
  }
}
 
Example 6
Source File: DynSqlTest.java    From mybaties with Apache License 2.0 6 votes vote down vote up
@Test
public void testDynamicSelectWithTypeHandler() {
  SqlSession sqlSession = sqlSessionFactory.openSession();
  try {
    List<Name> names = new ArrayList<Name>();

    Name name = new Name();
    name.setFirstName("Fred");
    name.setLastName("Flintstone");
    names.add(name);

    name = new Name();
    name.setFirstName("Barney");
    name.setLastName("Rubble");
    names.add(name);

    Parameter parameter = new Parameter();
    parameter.setNames(names);

    List<Map<String, Object>> answer = sqlSession.selectList("org.apache.ibatis.submitted.dynsql2.dynamicSelectWithTypeHandler", parameter);

    assertTrue(answer.size() == 2);
  } finally {
    sqlSession.close();
  }
}
 
Example 7
Source File: BatchKeysTest.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@Test
public void testInsertJdbc3() throws Exception {
  SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
  try {
    User user1 = new User(null, "Pocoyo");
    sqlSession.insert("insertIdentity", user1);
    User user2 = new User(null, "Valentina");
    sqlSession.insert("insertIdentity", user2);
    sqlSession.flushStatements();
    assertEquals(Integer.valueOf(0), user1.getId());
    assertEquals(Integer.valueOf(1), user2.getId());
    sqlSession.commit();
  } finally {
    sqlSession.close();
  }

  sqlSession = sqlSessionFactory.openSession();
  List<User> users = sqlSession.selectList("selectIdentity");
  Assert.assertTrue(users.size() == 2);
}
 
Example 8
Source File: AuthorDAOTest.java    From mybaties with Apache License 2.0 5 votes vote down vote up
@Ignore // issue #75 nested selects overwrite collections
@Test
public void shouldNotOverwriteCollectionOnNestedQuery() {
  SqlSession session = factory.openSession();
  try {
  List<Author> authors = session.selectList("getAllAuthorsNestedQuery");
  assertEquals(1, authors.size());
  assertEquals(4, authors.get(0).getPosts().size());
  } finally {
    session.close();
  }
}
 
Example 9
Source File: RowBoundsTest.java    From Mybatis-PageHelper with MIT License 5 votes vote down vote up
/**
 * 使用命名空间方式的RowBounds进行分页,使用RowBounds时不进行count查询
 * 通过修改代码可以进行count查询,没法通过其他方法改变参数
 * 因为如果通过调用一个别的方法来标记count查询,还不如直接startPage
 * <p/>
 * 同时使用startPage时,以startPage为准,会根据startPage参数来查询
 */
@Test
public void testNamespaceWithRowBounds() {
    SqlSession sqlSession = RowBoundsHelper.getSqlSession();
    try {
        //获取从0开始,10条内容
        List<User> list = sqlSession.selectList("selectAll", null, new RowBounds(0, 10));
        assertEquals(10, list.size());
        //判断查询结果的位置是否正确
        assertEquals(1, list.get(0).getId());
        assertEquals(10, list.get(list.size() - 1).getId());


        //获取从10开始,10条内容
        list = sqlSession.selectList("selectAll", null, new RowBounds(90, 10));
        assertEquals(10, list.size());
        //判断查询结果的位置是否正确
        assertEquals(91, list.get(0).getId());
        assertEquals(100, list.get(list.size() - 1).getId());


        //获取从20开始,20条内容
        list = sqlSession.selectList("selectAll", null, new RowBounds(100, 20));
        assertEquals(20, list.size());
        //判断查询结果的位置是否正确
        assertEquals(101, list.get(0).getId());
        assertEquals(120, list.get(list.size() - 1).getId());
    } finally {
        sqlSession.close();
    }
}
 
Example 10
Source File: CamelCaseMappingTest.java    From mybaties with Apache License 2.0 5 votes vote down vote up
@Test
public void testList() {
  SqlSession sqlSession = sqlSessionFactory.openSession();
  try {
    List<Camel> list = sqlSession.selectList("org.apache.ibatis.submitted.camel.doSelect");
    Assert.assertTrue(list.size() > 0);
    Assert.assertNotNull(list.get(0).getFirstName());
    Assert.assertNull(list.get(0).getLAST_NAME());
  } finally {
    sqlSession.close();
  }
}
 
Example 11
Source File: LanguageTest.java    From mybaties with Apache License 2.0 5 votes vote down vote up
@Test
public void testLangRaw() {
  SqlSession sqlSession = sqlSessionFactory.openSession();
  try {
    Parameter p = new Parameter(true, "Fli%");
    List<Name> answer = sqlSession.selectList("selectRaw", p);
    assertEquals(3, answer.size());
    for (Name n : answer) {
      assertEquals("Flintstone", n.getLastName());
    }
  } finally {
    sqlSession.close();
  }
}
 
Example 12
Source File: DefaultResultHandlerTypeTest.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Test
public void testSelectList() throws Exception {
  String xmlConfig = "org/apache/ibatis/submitted/result_handler_type/MapperConfig.xml";
  SqlSessionFactory sqlSessionFactory = getSqlSessionFactoryXmlConfig(xmlConfig);
  SqlSession sqlSession = sqlSessionFactory.openSession();
  try {
    List<Person> list = sqlSession
        .selectList("org.apache.ibatis.submitted.result_handler_type.PersonMapper.doSelect");
    assertEquals(list.size(), 2);
    assertEquals("java.util.LinkedList", list.getClass().getCanonicalName());
  } finally {
    sqlSession.close();
  }
}
 
Example 13
Source File: IDAllocDaoImpl.java    From Leaf with Apache License 2.0 5 votes vote down vote up
@Override
public List<LeafAlloc> getAllLeafAllocs() {
    SqlSession sqlSession = sqlSessionFactory.openSession(false);
    try {
        return sqlSession.selectList("com.sankuai.inf.leaf.segment.dao.IDAllocMapper.getAllLeafAllocs");
    } finally {
        sqlSession.close();
    }
}
 
Example 14
Source File: IDAllocDaoImpl.java    From SpringMVC-Project with MIT License 5 votes vote down vote up
@Override
public List<LeafAlloc> getAllLeafAllocs() {
    SqlSession sqlSession = sqlSessionFactory.openSession(false);
    try {
        return sqlSession.selectList("com.doodl6.springmvc.service.leaf.segment.dao.IDAllocMapper.getAllLeafAllocs");
    } finally {
        sqlSession.close();
    }
}
 
Example 15
Source File: AssociationTypeTest.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldGetAUser() {
  SqlSession sqlSession = sqlSessionFactory.openSession();
  try {
    List<Map> results = sqlSession.selectList("getUser");
    for (Map r : results) {
        Assert.assertEquals(String.class, r.get("a1").getClass());
        Assert.assertEquals(String.class, r.get("a2").getClass());
    }
  } finally {
    sqlSession.close();
  }
}
 
Example 16
Source File: LanguageTest.java    From mybaties with Apache License 2.0 5 votes vote down vote up
@Test
public void testLangRawWithIncludeAndCData() {
  SqlSession sqlSession = sqlSessionFactory.openSession();
  try {
    Parameter p = new Parameter(true, "Fli%");
    List<Name> answer = sqlSession.selectList("selectRawWithIncludeAndCData", p);
    assertEquals(3, answer.size());
    for (Name n : answer) {
      assertEquals("Flintstone", n.getLastName());
    }
  } finally {
    sqlSession.close();
  }
}
 
Example 17
Source File: ColumnPrefixAutoMappingTest.java    From mybatis with Apache License 2.0 4 votes vote down vote up
protected List<Pet> getPetAndRoom(SqlSession sqlSession) {
  List<Pet> pets = sqlSession.selectList("org.apache.ibatis.submitted.column_prefix.MapperAutoMapping.selectPets");
  return pets;
}
 
Example 18
Source File: PageHelperTest.java    From Mybatis-PageHelper with MIT License 4 votes vote down vote up
/**
 * 使用命名空间方式的RowBounds进行分页,使用RowBounds时不进行count查询
 * 通过修改代码可以进行count查询,没法通过其他方法改变参数
 * 因为如果通过调用一个别的方法来标记count查询,还不如直接startPage
 * <p/>
 * 同时使用startPage时,以startPage为准,会根据startPage参数来查询
 */
@Test
public void testNamespaceWithRowBounds() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {
        //获取从0开始,10条内容
        List<User> list = sqlSession.selectList("selectAll", null, new RowBounds(0, 10));
        assertEquals(10, list.size());
        assertEquals(183, ((Page<?>) list).getTotal());
        //判断查询结果的位置是否正确
        assertEquals(1, list.get(0).getId());
        assertEquals(10, list.get(list.size() - 1).getId());


        //获取从10开始,10条内容
        list = sqlSession.selectList("selectAll", null, new RowBounds(10, 10));
        assertEquals(10, list.size());
        assertEquals(183, ((Page<?>) list).getTotal());
        //判断查询结果的位置是否正确
        assertEquals(11, list.get(0).getId());
        assertEquals(20, list.get(list.size() - 1).getId());


        //获取从20开始,20条内容
        list = sqlSession.selectList("selectAll", null, new RowBounds(20, 20));
        assertEquals(20, list.size());
        assertEquals(183, ((Page<?>) list).getTotal());
        //判断查询结果的位置是否正确
        assertEquals(21, list.get(0).getId());
        assertEquals(40, list.get(list.size() - 1).getId());


        //同时使用startPage和RowBounds时,以startPage为准
        PageHelper.startPage(1, 20);
        list = sqlSession.selectList("selectAll", null, new RowBounds(0, 10));
        assertEquals(20, list.size());
        assertEquals(183, ((Page<?>) list).getTotal());
        //判断查询结果的位置是否正确
        assertEquals(1, list.get(0).getId());
        assertEquals(20, list.get(list.size() - 1).getId());
    } finally {
        sqlSession.close();
    }
}
 
Example 19
Source File: ColumnPrefixTest.java    From mybaties with Apache License 2.0 4 votes vote down vote up
protected List<Pet> getPetAndRoom(SqlSession sqlSession) {
  List<Pet> pets = sqlSession.selectList("org.apache.ibatis.submitted.column_prefix.Mapper.selectPets");
  return pets;
}
 
Example 20
Source File: ColumnPrefixAutoMappingTest.java    From mybatis with Apache License 2.0 4 votes vote down vote up
protected List<Person> getPersons(SqlSession sqlSession) {
  List<Person> list = sqlSession
      .selectList("org.apache.ibatis.submitted.column_prefix.MapperAutoMapping.selectPersons");
  return list;
}