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

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>
Advertisements