Create the SqlSession+ Examples for org.apache.ibatis.session.SqlSessionFactory
ReportPlease briefly explain why you feel this question should be reported .
Example 1
public class GetSession{
private SqlSessionFactory sqlSessionFactory;
SqlSession session = null;
//Connection conn = null;
private <T> T getMapper(Class<T> clazz, SqlSession session2) {
return session2.getMapper(clazz);
}
//Fire every two minutes
@Scheduled(cron="0 0/2 * * * ?")
public void print()
{
try{
sqlSessionFactory = new SqlSessionFactoryBuilder().build(getClass()
.getClassLoader().getResourceAsStream("META-INF/mybatis.xml"));
session = sqlSessionFactory.openSession();
//conn = session.getConnection();
Long gameId = getMapper(AppMapper.class, session).getLatestGame();
System.out.println(gameId);
if(gameId != null){
Timestamp gameEndTime = getMapper(AppMapper.class, session).getGameEndTime(gameId);
System.out.println("Game end time is :: "+ gameEndTime);
}
}finally {
session.close();
}
}
}
Example 2
public void shouldFindFormationsWithMyBatis() throws Exception {
InputStream stream = null;
try {
stream = Resources.getResourceAsStream("mybatis.xml");
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(stream);
SqlSession session = sessionFactory.openSession();
checkSessions( session.getMapper(SessionDao.class).findAll() );
}
finally {
IOUtils.closeSilently(stream);
}
}
mybatis.xml
Leave an answer