Announcement Announcement Module
Collapse
No announcement yet.
How to use Ibatis in spring framework Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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.

    Comment


    • #3
      How to use Ibatis in spring framework

      Hi,

      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.

      Thanks

      Rodolfo

      Comment


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

        Comment


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

          Thanks in advance

          Rodolfo

          Comment


          • #6
            You need to download the latest release from http://www.springframework.org/download

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

            Comment


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

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

              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 org.springframework.orm.ibatis.support.SqlMapClien tDaoSupport.
              5. Create a method in that class named whatever and put a body similar to the following:

              getSqlMapClientTemplate().queryForXXX(...)

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

              HTH.

              Daniel

              Comment


              • #8
                iBatis + Spring Tutorial

                Try this iBatis-Spring Tutorial http://www.developersbook.com/ibatis...-tutorials.php

                Comment


                • #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" />
                  </bean>

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

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


                  Now the MyData.xml looks like this...

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

                  <!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
                  "(omitted because of this forum restriction)...batis.apache.org/dtd/sql-map-2.dtd">
                  <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" />
                  </cacheModel>
                  <!-- Result Maps -->
                  <resultMap id="cityResult" class="city">
                  <result property="guid" column="GUID" />
                  <result property="name" column="NAME" />
                  </resultMap>
                  <!-- Queries -->
                  <select id="getAllCities" resultClass="city" cacheModel="staticDataCache">
                  SELECT GUID, NAME FROM CITY </select>
                  </sqlMap>


                  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.

                  Comment


                  • #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.

                    Comment


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

                      <!DOCTYPE sqlMapConfig      
                          PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"      
                          "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">

                      <sqlMapConfig>
                          <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" />
                      </sqlMapConfig>
                      [/code]

                      Comment


                      • #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.

                        Comment


                        • #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.

                          Comment

                          Working...
                          X