Create the SqlSession+ Examples for org.apache.ibatis.session.SqlSessionFactory

Report
Question

Please briefly explain why you feel this question should be reported .

Report
Cancel

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

<?XML VERSION=”1.0″ ENCODING=”UTF-8″ ?>
<!DOCTYPE CONFIGURATION
PUBLIC “-//MYBATIS.ORG//DTD CONFIG 3.0//EN”
HTTP://MYBATIS.ORG/DTD/MYBATIS-3-CONFIG.DTD”&GT;
<CONFIGURATION>
<SETTINGS>
<SETTING NAME=”LOGIMPL” VALUE=”LOG4J”/>
</SETTINGS>
<ENVIRONMENTS DEFAULT=”CIP-TRANS”>
<ENVIRONMENT ID=”CIP-TRANS”>
<TRANSACTIONMANAGER TYPE=”JDBC”/>
<DATASOURCE TYPE=”JNDI”>
<PROPERTY NAME=”DATA_SOURCE” VALUE=”JAVA:JBOSS/DATASOURCES/XXX-XXXXX”/>
</DATASOURCE>
</ENVIRONMENT>
</ENVIRONMENTS>
<MAPPERS>
<PACKAGE NAME=”COM.XXXX.XXX.WEB.MAPPER” />
</MAPPERS>
</CONFIGURATION>

0
Manimaran Samuthirapandi 10 months 2021-08-05T18:43:56+02:00 0 Answers 10 views 0

Leave an answer

Captcha Click on image to update the captcha .