HiIs there any standard way or best practice for of using Ibatis in web
application,
This is how i use it
In my InitServlet which is loaded when the application starts, i create
instance SqlMapClient and save it in ServletContext.
And in other servlets where i need to get data from database, i get the
SqlMapClient from ServletConext and use it
I think by doing this, SqlMapClient will automatically create ConnectionPool
and use this connection pool for running queries
public class InitServlet extends javax.servlet.http.HttpServlet
{
public void init(ServletConfig config)
{
super.init(config);
ServletContext oCtx = config.getServletContext();
String resource = "SQLMapConfigWeb.xml";
Reader reader = Resources.getResourceAsReader(resource);
SqlMapClient sqlMap =
SqlMapClientBuilder.buildSqlMapClient(reader);
oCtx.setAttribute("SQLMAPCLIENT", sqlMap );
}
}
public class DataAccessServlet extends javax.servlet.http.HttpServlet
{
public void doGet(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
throws javax.servlet.ServletException, java.io.IOException
{
SqlMapClient sqlMap = oCtx.getAttribute("SQLMAPCLIENT", sqlMap
);
sqlMap.queryForObject("getPerson", personId);
}
}
//This is my "SQLMapConfigWeb.xml"
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
file (e.g. "${driver}". The file is usually relative to the classpath and is
optional. -->
<properties resource="SqlMapConfigAS400.properties" />
<settings cacheModelsEnabled="true" enhancementEnabled="true"
lazyLoadingEnabled="true" maxRequests="32" maxSessions="10"
maxTransactions="5" useStatementNamespaces="false" />
<transactionManager type="JDBC">
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="${driver}" />
<property name="JDBC.ConnectionURL" value="${url}" />
<property name="JDBC.Username" value="${username}" />
<property name="JDBC.Password" value="${password}" />
</dataSource>
</transactionManager>
<sqlMap resource="ExternalTable.xml" />
</sqlMapConfig>