Announcement Announcement Module
No announcement yet.
How to use Ibatis in spring framework Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to use Ibatis in spring framework

    I have to use IBatis with spring framework , please provide sample test example if you can or link to the test sample application.

  • #2
    Please look at the Spring PetClinic sample application. It uses iBATIS and demonstrates Spring's iBATIS integration.


    • #3
      How to use Ibatis in spring framework


      I have tried to find the PetClinic spring sample application, and I couldn't find it. Do you now other sample or where can I find this sample.




      • #4
        Sorry, I meant the Pet Store. Apologies for the mistake.


        • #5
          I can't find the Pet Store too in If you were so kind to put a link to this sample I would appreciate.

          Thanks in advance



          • #6
            You need to download the latest release from

            You'll find the petStore sample application in "spring-framework-1.2.6\samples\jpetstore"


            • #7
              Try these steps:

              1. In the ApplicationContext.xml file, add the following definitions (1 for a Data Source and 1 for the SQL Map Spring Bean)

              <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryB ean">
              <property name="jndiName" value="java:comp/..."/>

              <bean id="sqlMapClient"
              class="org.springframework.orm.ibatis.SqlMapClient FactoryBean">
              <property name="configLocation" value="WEB-INF/sqlmaps.xml"/>
              <property name="dataSource" ref="dataSource"/>

              2. Create a sqlmaps.xml file in the /WEB-INF/ dir. You should be able to get this from the IBATIS Developer guides.
              3. Create Some SQL MAP file that you wish to query from a DB. I would suggest a very simple QUERY to start.
              4. Create a class that extends the Spring File tDaoSupport.
              5. Create a method in that class named whatever and put a body similar to the following:


              That should ideally load your SQL MAP configuration at startup, and then allow you to call the SQLMAP query from your java code.




              • #8
                iBatis + Spring Tutorial

                Try this iBatis-Spring Tutorial


                • #9
                  Spring unable to load the actual query maps of iBatis.

                  I am having a problem integrating iBatis with Spring.

                  In applicationContext, the following entry works fine since it throws an error if the path is incorrect.

                  <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClient FactoryBean">
                  <property name="configLocation" value="/WEB-INF/sqlmap.xml"/>
                  <property name="dataSource" ref="configDataSource" />

                  The /WEB-INF/sqlmap.xml looks like this

                  <?xml version="1.0" encoding="UTF-8"?>
                  <!DOCTYPE sql-map-config
                  PUBLIC "-// SQL Map Config 1.0//EN"
                  "(omitted because of this forum restriction)/sql-map-config.dtd">
                  <settings useFullyQualifiedStatementNames="true"/>
                  <sql-map resource="MyData.xml" />

                  Now the MyData.xml looks like this...

                  <?xml version="1.0" encoding="UTF-8" ?>

                  <!DOCTYPE sqlMap PUBLIC "-// SQL Map 2.0//EN"
                  "(omitted because of this forum restriction)">
                  <sqlMap namespace="StaticData">
                  <!-- Type Alias -->
                  <typeAlias alias="city" type="model.City" />

                  <!-- Cache Model -->
                  <cacheModel id="staticDataCache" type="LRU">
                  <flushInterval hours="168" />
                  <property name="size" value="100" />
                  <!-- Result Maps -->
                  <resultMap id="cityResult" class="city">
                  <result property="guid" column="GUID" />
                  <result property="name" column="NAME" />
                  <!-- Queries -->
                  <select id="getAllCities" resultClass="city" cacheModel="staticDataCache">
                  SELECT GUID, NAME FROM CITY </select>

                  I am deploying the entire thing in a Struts2 based web application.

                  Where should I place MyData.xml file so that it gets picked up.

                  It is throwing an exception as:

                  com.ibatis.sqlmap.client.SqlMapException: There is no statement named getAllCities in this SqlMap.


                  • #10
                    In a directory/folder that's in/on the classpath.

                    If you're using eclipse that would be WEB-INF/classes and it'll copy it from there to the right place when you start tomcat (if you're running your test tomcat from within eclipse).

                    If you're using maven then put it in src/main/resources, the contents of which are copied to the WEB-INF/classes folder when maven makes the war file.


                    • #11
                      And if you use the square bracket code tags there aren't any "restrictions" on posting code:
                      PHP Code:
                      <?xml version="1.0" encoding="UTF-8" ?>

                      <!DOCTYPE sqlMapConfig      
                          PUBLIC "-// SQL Map Config 2.0//EN"      

                          <settings useStatementNamespaces="true" />

                          <sqlMap resource="ibatis/hsqldb/User.xml" />

                          <sqlMap resource="ibatis/hsqldb/Unverified.xml" />

                          <sqlMap resource="ibatis/hsqldb/Photo.xml" />

                          <sqlMap resource="ibatis/hsqldb/Pool.xml" />


                      • #12
                        Thanks for the prompt response.

                        I am using ant and in the script I am manually copying the files under WEB-INF/classes.

                        When Spring loads the bean "sqlMapClient", it validates the location of the configuration file and throws exception if it doesn't find it in place.

                        However, the files which represent the resources
                        <sql-map resource="MyData.xml" /> like this, it doesn't validate the location of the same. It doesn't give error if I put any name in it, it just ignores if it doesn't find it.

                        I am really stuck at this, thanks for helping.


                        • #13
                          Originally posted by knightrider_in View Post
                          However, the files which represent the resources
                          <sql-map resource="MyData.xml" /> like this, it doesn't validate the location of the same. It doesn't give error if I put any name in it, it just ignores if it doesn't find it.
                          When it gets to that point, it's iBatis that verifies that the MyData.xml file exists, not Spring. The clue to this (subtle perhaps) is that the DOCTYPE for the xml at the top of that file refers to iBatis, not Spring.