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

The following examples show how to use org.apache.ibatis.session.SqlSession#close() . 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: PermissionsTest.java    From mybaties with Apache License 2.0 6 votes vote down vote up
@Test // see issue #168
public void checkNestedResultMapLoop() {
  SqlSession sqlSession = sqlSessionFactory.openSession();
  try {
    final PermissionsMapper mapper = sqlSession.getMapper(PermissionsMapper.class);

    final List<Resource> resources = mapper.getResources();
    Assert.assertEquals(2, resources.size());

    final Resource firstResource = resources.get(0);
    final List<Principal> principalPermissions = firstResource.getPrincipals();
    Assert.assertEquals(1, principalPermissions.size());
    
    final Principal firstPrincipal = principalPermissions.get(0);
    final List<Permission> permissions = firstPrincipal.getPermissions();
    Assert.assertEquals(2, permissions.size());
    
    final Permission firstPermission = firstPrincipal.getPermissions().get(0);
    Assert.assertSame(firstResource, firstPermission.getResource());
    final Permission secondPermission = firstPrincipal.getPermissions().get(1);
    Assert.assertSame(firstResource, secondPermission.getResource());
  } finally {
    sqlSession.close();
  }
}
 
Example 2
Source File: OneopsCmsManager.java    From oneops with Apache License 2.0 6 votes vote down vote up
/**
 * Gets the clazzes.
 *
 * @return the clazzes
 */
public List<CmsClazz> getClazzes() {
	SqlSession session = sqlSessionFactory.openSession();
	List<CmsClazz> clazzes;
	try {
		ClazzMapper mapper = session.getMapper(ClazzMapper.class);
		clazzes = mapper.getClazzes();
		for(CmsClazz clazz : clazzes) {
			clazz.setMdAttributes(mapper.getClazzAttrs(clazz.getClassId()));
			clazz.setFromRelations(mapper.getFromClazzRelations(clazz.getClassId()));
			clazz.setToRelations(mapper.getToClazzRelations(clazz.getClassId()));
		}
	} finally {
		session.close();
	}
	return clazzes;
}
 
Example 3
Source File: BindingTest.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldSelectDraftTypedPosts() {
  SqlSession session = sqlSessionFactory.openSession();
  try {
    BoundBlogMapper mapper = session.getMapper(BoundBlogMapper.class);
    List<Post> posts = mapper.selectPosts();
    assertEquals(5, posts.size());
    assertTrue(posts.get(0) instanceof DraftPost);
    assertFalse(posts.get(1) instanceof DraftPost);
    assertTrue(posts.get(2) instanceof DraftPost);
    assertFalse(posts.get(3) instanceof DraftPost);
    assertFalse(posts.get(4) instanceof DraftPost);
  } finally {
    session.close();
  }
}
 
Example 4
Source File: DateTimeTest.java    From Mapper with MIT License 6 votes vote down vote up
@Test
public void testSelect2() {
    SqlSession sqlSession = getSqlSession();
    try {
        TimeModel2Mapper mapper = sqlSession.getMapper(TimeModel2Mapper.class);
        List<TimeModel2> list = mapper.selectAll();
        Assert.assertEquals(2, list.size());

        Assert.assertEquals("2018-01-01", toDate(list.get(0).getTestDate()));
        Assert.assertEquals("12:11:00", toTime(list.get(0).getTestTime()));
        Assert.assertEquals("2018-01-01 12:00:00", toDatetime(list.get(0).getTestDatetime()));

        Assert.assertEquals("2018-11-11", toDate(list.get(1).getTestDate()));
        Assert.assertEquals("01:59:11", toTime(list.get(1).getTestTime()));
        Assert.assertEquals("2018-02-12 17:58:12", toDatetime(list.get(1).getTestDatetime()));
    } finally {
        sqlSession.close();
    }
}
 
Example 5
Source File: BindingTest.java    From mybaties with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldSelectOneBlogAsMap() {
  SqlSession session = sqlSessionFactory.openSession();
  try {
    BoundBlogMapper mapper = session.getMapper(BoundBlogMapper.class);
    Map<String,Object> blog = mapper.selectBlogAsMap(new HashMap<String, Object>() {
      {
        put("id", 1);
      }
    });
    assertEquals(1, blog.get("ID"));
    assertEquals("Jim Business", blog.get("TITLE"));
  } finally {
    session.close();
  }
}
 
Example 6
Source File: PrimitivesTest.java    From mybaties with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setUp() throws Exception {
  // create an SqlSessionFactory
  Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/primitives/mybatis-config.xml");
  sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
  reader.close();

  // populate in-memory database
  SqlSession session = sqlSessionFactory.openSession();
  Connection conn = session.getConnection();
  reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/primitives/CreateDB.sql");
  ScriptRunner runner = new ScriptRunner(conn);
  runner.setLogWriter(null);
  runner.runScript(reader);
  reader.close();
  session.close();
}
 
Example 7
Source File: Sch2ServiceImpl.java    From SDA with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public int insertSchHist(Map<String, List<SchHistDTO>> map) throws Exception {
	int cnt = -1;
	SqlSession sqlSession = factory.openSession(); 

	SchHist2DAO schHist2DAO = new SchHist2DAO(sqlSession);
	
	List<SchHistDTO> list = map.get("list");
	for(int i = 0; i < list.size(); i++) {
		SchHistDTO schHistDTO = (SchHistDTO)list.get(i);
		try {
			log.debug("insertSchHist() ......................... start ");
			
			cnt = schHist2DAO.insert(schHistDTO);

			if(cnt > 0) {
				sqlSession.commit();
			} else {
				sqlSession.rollback();
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.debug("Exception in insertSchHist()=====> "+e.getMessage());				
			throw e;
		} finally {
			sqlSession.close();				
		}
	}
	log.debug("insertSchHist() ......................... end ");
	return cnt;
}
 
Example 8
Source File: AssociationTest.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldGetAllCarsNonUnique() {
  // this is a little weird - we might expect 4 objects back, but there are only
  // 1 distinct carid, so we get one back.
  SqlSession sqlSession = sqlSessionFactory.openSession();
  try {
    Mapper mapper = sqlSession.getMapper(Mapper.class);
    List<Car> cars = mapper.getCars2();
    Assert.assertEquals(1, cars.size());
  } finally {
    sqlSession.close();
  }
}
 
Example 9
Source File: InheritanceTest.java    From mybaties with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldGetAUser() {
  SqlSession sqlSession = sqlSessionFactory.openSession();
  try {
    UserProfileMapper mapper = sqlSession.getMapper(UserProfileMapper.class);
    UserProfile user = mapper.retrieveById(1);
    Assert.assertEquals("Profile1", user.getName());
  } finally {
    sqlSession.close();
  }
}
 
Example 10
Source File: EncodingTest.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Test
public void testEncoding2() {
  SqlSession sqlSession = sqlSessionFactory.openSession();
  try {
    EncodingMapper mapper = sqlSession.getMapper(EncodingMapper.class);
    String answer = mapper.select2();
    assertEquals("Mara\u00f1\u00f3n", answer);
  } finally {
    sqlSession.close();
  }
}
 
Example 11
Source File: TestSelectItems.java    From Mybatis-PageHelper with MIT License 5 votes vote down vote up
/**
 * 查询自定义列时 - 实际上这个测试由于使用${},并没有起到真正的目的
 */
@Test
public void testSelectColumns() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
    try {
        //获取第1页,10条内容,默认查询总数count
        PageHelper.startPage(1, 10);
        List<User> list = userMapper.selectColumns();
        //1,'Angola','AO'
        assertEquals(1, list.get(0).getId());
        assertEquals("毕淑儒", list.get(0).getName());
        assertEquals("BSR", list.get(0).getPy());
        assertEquals(10, list.size());
        assertEquals(183, ((Page<?>) list).getTotal());

        //获取第1页,10条内容,默认查询总数count
        PageHelper.startPage(1, 10);
        list = userMapper.selectColumns("id", "name");
        //1,'Angola','AO'
        assertEquals(1, list.get(0).getId());
        assertEquals("毕淑儒", list.get(0).getName());
        assertNull(list.get(0).getPy());
        assertEquals(10, list.size());
        assertEquals(183, ((Page<?>) list).getTotal());
    } finally {
        sqlSession.close();
    }
}
 
Example 12
Source File: EnumWithOgnlTest.java    From mybaties with Apache License 2.0 5 votes vote down vote up
@Test
public void testEnumWithOgnlDirectorWithInterface() {
    SqlSession sqlSession = sqlSessionFactory.openSession();
    PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
    List<Person> persons = personMapper.selectAllByTypeWithInterface(new PersonType() {
        public Type getType() {
            return Person.Type.DIRECTOR;
        }
    });
    Assert.assertEquals("Persons must contain 1 persons", 1, persons.size());
sqlSession.close();
}
 
Example 13
Source File: NotNullColumnTest.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Test
public void testNotNullColumnWithoutChildrenFidMultipleNullColumns() {
  SqlSession sqlSession = sqlSessionFactory.openSession();
  try {
      FatherMapper fatherMapper = sqlSession.getMapper(FatherMapper.class);

      Father test = fatherMapper.selectByIdFidMultipleNullColumns(2);
      assertNotNull(test);
      assertNotNull(test.getChildren());
      assertTrue(test.getChildren().isEmpty());
  } finally {
    sqlSession.close();
  }
}
 
Example 14
Source File: UUIDTest.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Test(expected=PersistenceException.class)
public void shouldGetAUser() {
  SqlSession sqlSession = sqlSessionFactory.openSession();
  try {
    Mapper mapper = sqlSession.getMapper(Mapper.class);
    User user = mapper.getUser(UUID.fromString("38400000-8cf0-11bd-b23e-10b96e4ef00d"));
    Assert.assertEquals("User1", user.getName());
  } finally {
    sqlSession.close();
  }
}
 
Example 15
Source File: SelectKeyTest.java    From mybaties with Apache License 2.0 5 votes vote down vote up
@Test
public void testAnnotatedInsertTable3_2() {
    SqlSession sqlSession = sqlSessionFactory.openSession();

    try {
      Name name = new Name();
      name.setName("barney");
      AnnotatedMapper mapper = sqlSession.getMapper(AnnotatedMapper.class);
      int rows = mapper.insertTable3_2(name);
      assertEquals(1, rows);
      assertEquals(33, name.getNameId());
    } finally {
      sqlSession.close();
    }
}
 
Example 16
Source File: SimpleListParameterTest.java    From mybaties with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldGetACar() throws Exception {
  SqlSession sqlSession = sqlSessionFactory.openSession();
  try {
    CarMapper carMapper = sqlSession.getMapper(CarMapper.class);
    Car car = new Car();
    car.setDoors(Arrays.asList(new String[] {"2", "4"}));
    List<Car> cars = carMapper.getCar(car);
    Assert.assertNotNull(cars);
  } finally {
    sqlSession.close();
  }
}
 
Example 17
Source File: NonFullyQualifiedNamespaceTest.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Test
public void testCrossReferenceXmlConfig() throws Exception {
    Reader configReader = Resources
            .getResourceAsReader("org/apache/ibatis/submitted/xml_external_ref/NonFullyQualifiedNamespaceConfig.xml");
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configReader);
    configReader.close();

    Configuration configuration = sqlSessionFactory.getConfiguration();

    MappedStatement selectPerson = configuration.getMappedStatement("person namespace.select");
    assertEquals(
            "org/apache/ibatis/submitted/xml_external_ref/NonFullyQualifiedNamespacePersonMapper.xml",
            selectPerson.getResource());

    Connection conn = configuration.getEnvironment().getDataSource().getConnection();
    initDb(conn);

    SqlSession sqlSession = sqlSessionFactory.openSession();
    try {
        Person person = (Person) sqlSession.selectOne("person namespace.select", 1);
        assertEquals((Integer)1, person.getId());
        assertEquals(2, person.getPets().size());
        assertEquals((Integer)2, person.getPets().get(1).getId());

        Pet pet = (Pet) sqlSession.selectOne("person namespace.selectPet", 1);
        assertEquals(Integer.valueOf(1), pet.getId());

        Pet pet2 = (Pet) sqlSession.selectOne("pet namespace.select", 3);
        assertEquals((Integer)3, pet2.getId());
        assertEquals((Integer)2, pet2.getOwner().getId());
    }
    finally {
        sqlSession.close();
    }
}
 
Example 18
Source File: BindingTest.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldSelectBlogWithDefault31ParamNames() {
  SqlSession session = sqlSessionFactory.openSession();
  try {
    BoundBlogMapper mapper = session.getMapper(BoundBlogMapper.class);
    Blog blog = mapper.selectBlogByDefault31ParamNames(1, "Jim Business");
    assertNotNull(blog);
  } finally {
    session.close();
  }
}
 
Example 19
Source File: SqlProviderTest.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldGetOneUser() {
  SqlSession sqlSession = sqlSessionFactory.openSession();
  try {
    Mapper mapper = sqlSession.getMapper(Mapper.class);
    User user = mapper.getUser(4);
    assertNotNull(user);
    assertEquals("User4", user.getName());
  } finally {
    sqlSession.close();
  }
}
 
Example 20
Source File: TestUpdateByPrimaryKey.java    From Mapper with MIT License 5 votes vote down vote up
/**
 * 根据查询条件进行查询
 */
@Test
public void testUpdateByPrimaryKeyAndVersion() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {
        CountryVersionMapper mapper = sqlSession.getMapper(CountryVersionMapper.class);
        CountryVersion country = mapper.selectByPrimaryKey(174);
        Assert.assertNotNull(country);
        Assert.assertEquals(new Integer(1), country.getVersion());
        country.setCountryname("美国2");
        Assert.assertEquals(1, mapper.updateByPrimaryKey(country));

        country = mapper.selectByPrimaryKey(174);
        Assert.assertNotNull(country);
        Assert.assertEquals(new Integer(2), country.getVersion());

        country.setCountryname("美国3");
        Assert.assertEquals(1, mapper.updateByPrimaryKey(country));

        country = mapper.selectByPrimaryKey(174);
        Assert.assertNotNull(country);
        Assert.assertEquals(new Integer(3), country.getVersion());

        country.setCountryname("美国4");
        Assert.assertEquals(1, mapper.updateByPrimaryKey(country));

        country = mapper.selectByPrimaryKey(174);
        Assert.assertNotNull(country);
        Assert.assertEquals(new Integer(4), country.getVersion());
    } finally {
        sqlSession.close();
    }
}