You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user-java@ibatis.apache.org by Ashish Kulkarni <as...@gmail.com> on 2008/11/07 16:17:08 UTC

Best practice for using Ibatis in Web application

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>

Re: Best practice for using Ibatis in Web application

Posted by "Alejandro D. Garin" <ag...@gmail.com>.
+1 for ibatis with spring in web application.

On Wed, Dec 3, 2008 at 5:30 PM, Rick <ri...@gmail.com> wrote:

> @Ashish You can look at Larry's old struts-spring-ibatis for basic web
> app setup: http://learntechnology.net/content/ibatis/spring_ibatis.jsp
>
> @Larry I did liked guice when I looked at it, but how does it help
> with iBATIS - I doubt it comes with and sqlMapClientTemplate?
>
> On Wed, Dec 3, 2008 at 8:00 AM, Larry Meadors <la...@gmail.com>
> wrote:
> > I'm looking at google guice + stripes + ibatis (ashamed to say it was
> > Brandon and Nathan's idea though).
> >
> > Virtually zero configuration. It just works.
> >
> > Larry
> >
>
>
>
> --
> Rick
>

Re: Best practice for using Ibatis in Web application

Posted by Rick <ri...@gmail.com>.
@Ashish You can look at Larry's old struts-spring-ibatis for basic web
app setup: http://learntechnology.net/content/ibatis/spring_ibatis.jsp

@Larry I did liked guice when I looked at it, but how does it help
with iBATIS - I doubt it comes with and sqlMapClientTemplate?

On Wed, Dec 3, 2008 at 8:00 AM, Larry Meadors <la...@gmail.com> wrote:
> I'm looking at google guice + stripes + ibatis (ashamed to say it was
> Brandon and Nathan's idea though).
>
> Virtually zero configuration. It just works.
>
> Larry
>



-- 
Rick

Re: Best practice for using Ibatis in Web application

Posted by Larry Meadors <la...@gmail.com>.
I'm looking at google guice + stripes + ibatis (ashamed to say it was
Brandon and Nathan's idea though).

Virtually zero configuration. It just works.

Larry

Re: Best practice for using Ibatis in Web application

Posted by Zoran Avtarovski <zo...@sparecreative.com>.
+1

Spring and ibatis make life much simpler when used together.

Z.
> 
> I agree with Kai,
> for me Spring and Ibatis are the better way to use Ibatis in a web
> Application.
> 
> Regards



Re: Best practice for using Ibatis in Web application

Posted by Rinaldo <rb...@gmail.com>.
I agree with Kai,
for me Spring and Ibatis are the better way to use Ibatis in a web
Application.

Regards

-- 
Rinaldo Bonazzo

Il giorno mar, 02/12/2008 alle 22.24 +0100, Kai Grabfelder ha scritto:
> for me combining Spring with iBATIS is pretty much my standard way for using iBATIS in web applications. The
> webframework differs (may it be Wicket, Stripes or Struts) but iBATIS and Spring dependency injection always
> stays the same ;-). See the jpetstore (an example contained in the Spring download) for an example.
> 
> Regards Kai
> 
> Kai
> 
> 
> 
> --- Original Nachricht ---
> Absender: Ashish Kulkarni
> Datum: 02.12.2008 21:34
> > HiAny ideas on the mail below
> > 
> > On Fri, Nov 7, 2008 at 10:17 AM, Ashish Kulkarni <
> > ashish.kulkarni13@gmail.com> wrote:
> > 
> >> 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>
> >>
> >>
> >>
> >>
> >>
> > 
> 



Re: Best practice for using Ibatis in Web application

Posted by Kai Grabfelder <no...@kinokai.de>.
for me combining Spring with iBATIS is pretty much my standard way for using iBATIS in web applications. The
webframework differs (may it be Wicket, Stripes or Struts) but iBATIS and Spring dependency injection always
stays the same ;-). See the jpetstore (an example contained in the Spring download) for an example.

Regards Kai

Kai



--- Original Nachricht ---
Absender: Ashish Kulkarni
Datum: 02.12.2008 21:34
> HiAny ideas on the mail below
> 
> On Fri, Nov 7, 2008 at 10:17 AM, Ashish Kulkarni <
> ashish.kulkarni13@gmail.com> wrote:
> 
>> 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>
>>
>>
>>
>>
>>
> 


Re: Best practice for using Ibatis in Web application

Posted by Ashish Kulkarni <as...@gmail.com>.
HiAny ideas on the mail below

On Fri, Nov 7, 2008 at 10:17 AM, Ashish Kulkarni <
ashish.kulkarni13@gmail.com> wrote:

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