My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
DocUpgrade3  
Updated Mar 5, 2011 by eduardo.macarron

This page provides some information that is useful when migrating a project from iBatis 2 to MyBatis 3. It is probably not 100% complete.

Conversion Tool

There is a tool available in the downloads section that will help you to convert your iBATIS 2.x sqlmap files into MyBatis 3.x xml mapper files.

Get it from http://mybatis.googlecode.com/files/ibatis2mybatis.zip

The tool is designed around an xslt transformation and some text replacements packaged in an ant task and tries to deliver a good starting point before the more complex work begins.

New DTDs

New sqlMapConfig.xml DTD:

<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">

New sqlMap (*.map.xml) DTD:

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 

Configuration

  • Root configuration tag <sqlMapConfig> is now <configuration>

Settings

Within the root configuration tag:

<settings x="y" foo="bar"/>

is now:

<settings>
    <setting name="x" value="y"/>
    <setting name="foo" value="bar"/>
</settings>

and

<settings useStatementNamespaces="true"/>

can be removed, since the use of namespaces has become mandatory.

<typeAlias>

<typeAlias> must be moved out of the <sqlMap> element to <configuration><typeAliases></typeAliases></configuration>

<configuration>
    <settings>
    ...
    </settings>
    <typeAliases>
        <typeAlias ... />
    </typeAliases>
</configuration>

<transactionManager> and <dataSource>

<transactionManager type="JDBC" commitRequired="false">
    <dataSource type="your.package.CustomDataSourceFactory" />
</transactionManager>

is now:

<environments default="env">
    <environment id="env">
        <transactionManager type="JDBC">
            <property name="commitRequired" value="false"/>
        </transactionManager>
        <dataSource type="your.package.CustomDataSourceFactory" />
    </environment>
</environments>

<sqlMap>

<sqlMap resource=... />
<sqlMap resource=... />
<sqlMap resource=... />

is now:

<mappers>
    <mapper resource=... />
</mappers>

Mapping

  • The root element <sqlMap> is now <mapper>
  • The attribute parameterClass should be changed to parameterType
  • The attribute resultClass should be changed to resultType
  • The attribute class should be changed to type
  • the columnIndex attribute does not exist anymore for the <result> tag
  • The groupBy attribute has been eliminated. Here is an example of groupBy from a 2.x sqlMap:

<resultMap id="productRM" class="product" groupBy="id">
    <result property="id" column="product_id"/>
    <result property="name" column="product_name"/>
    <result property="category" column="product_category"/>
    <result property="subProducts" resultMap="Products.subProductsRM"/>
</resultMap>

New:

<resultMap id="productRM" type="product" >
    <id property="id" column="product "/>
    <result property="name " column="product_name "/>
    <result property="category " column="product_category "/>
    <collection property="subProducts" resultMap="Products.subProductsRM"/>
</resultMap>

Nested resultMaps

These should now be specified using the <association> tag.

<resultMap ...>
    <result property="client" resultMap="Client.clientRM"/>
    ...
</resultMap>

is now:

<resultMap ...>
    <association property="client" resultMap="Client.clientRM"/>
    ...
</resultMap>

<parameterMap>

Although this tag is deprecated, it can be used as in iBatis 2. However for versions up to 3.0.3 there is a bug when using type="map" and not specifying javaType for a parameter. This will result in

There is no getter for property named '...' in 'interface java.util.Map'    

This should be solved in MyBatis 3.0.4. For versions 3.0.3 and earlier the workaround is to explicitly specify javaType.

Inline parameters

#value#

is now:

#{value}

jdbcType changes

jdbcType="ORACLECURSOR"

is now:

jdbcType="CURSOR"

and

jdbcType="NUMBER"

is now:

jdbcType="NUMERIC"

Stored procedures

  • the <procedure> tag doesn't exist anymore. Use <select>, <insert> or <update>.
<procedure id="getValues" parameterMap="getValuesPM">
    { ? = call pkgExample.getValues(p_id => ?) }
</procedure>

is now:

<select id="getValues" parameterMap="getValuesPM" statementType="CALLABLE">
    { ? = call pkgExample.getValues(p_id => ?)}
</select>
If you're calling an insert procedure that returns a value, you can use <select> instead of <insert>, but make sure to specify useCache="false" and flushCache="true". You'll also have to force a commit.

  • for stored procedures that return a cursor, there is a bug (see  issue 30 ) when using nested result maps (i.e. the output parameter's resultMap contains an <association> tag with the resultMap attribute). As long as the issue is not fixed, you have to specify the resultMap of the output parameter on the statement itself as well, or the nested resultMap will not be populated.

Caching

<cacheModel id="myCache" type="LRU">
    <flushInterval hours="24"/>
    <property name="size" value="100" />
</cacheModel>

is now:

<cache flushInterval="86400000" eviction="LRU"/>

Note: you can omit eviction="LRU" since it is the default.

  • the <flushOnExecute> tag is replaced by the flushCache attribute for the statements and the cache will be used by all select statements by default.

Dynamic SQL

The most common dynamic SQL in my project is isNotNull. Here is an example replacement regex:

Pattern:

<isNotNull.*?property=\"(.*?)\">
</isNotNull>

Replacement:

<if test="$1 != null">
</if>

Also common is the use of isEqual, you can replace this by a similar <if> tag.

Java code

SqlMapClient

  • This class doesn't exist anymore. Use SqlSessionFactory instead (see User Guide).

Custom type handler

  • Replace interface TypeHandlerCallback with TypeHandler. It has slightly different but similar methods.

Custom data source factory

Old interface:

com.ibatis.sqlmap.engine.datasource.DataSourceFactory

New interface:

org.apache.ibatis.datasource.DataSourceFactory

Replace method

public void initialize(Map properties)

with

public void setProperties(Properties props)
Comment by joz...@gmail.com, Nov 3, 2010

{quote}Old:

<resultMap id="invoiceRM" type="invoice" extends="Invoice.abstractInvoiceRM">

<result property="client" resultMap="Client.clientRM"/> <result property="accounts" column="invoiceNumber=INVOICE_NUMBER" select="Invoice.getAccountsSql"/> <result property="products" column="productGroup=PRODUCT_GROUP_ID" select="Invoice.getProductsSql"/> </resultMap>
New:

<resultMap id="agreementDetailRM" type="agreement" extends="Agreement.agreementRM">

<association property="client" resultMap="Agreement.clientRM"/> <collection property="accounts" column="agreementNumber=AGREEMENT_NUMBER" select="Agreement.getAccountsSql"/> <collection property="products" column="serviceGroupId=SERVICE_GROUP_ID" select="Agreement.getProductsSql"/> </resultMap>
{qoute}

Can you please reuse the same result map for NEW example? It's not the best way to show difference on different result maps

Comment by peter.ko...@awaro.com, Nov 22, 2010
Comment by patrick....@gmail.com, Jan 13, 2011

Hello. all

I would like to know mybatis how to print sql ?

I set log4.properties [log4j.rootLogger=debug]

Comment by monika.e...@gmail.com, Mar 8, 2011

In iBatis2 we had an attribute named nullValue with result tag. Can you please tell what is the migration for myBatis 3.

Comment by aaron.pi...@gmail.com, May 10, 2011

The migration script will fail with a FileNotFoundException? if your DTD references are out of date. Make sure your DTD references point to ibatis.apache.org (not ibatis.com) before attempting to run the ant script.

Comment by francesc...@gmail.com, May 17, 2011

In iBatis2 we had an attribute named nullValue with result tag. Can you please tell what is the migration for myBatis 3 ?

Comment by project member herman.b...@gmail.com, May 18, 2011

Maybe you should try the mailing list, I think you'll have more chance of getting an answer there.

If not I guess you can work around it with a custom type handler.

Comment by leesooyo...@gmail.com, Oct 6, 2011

I would like to know how to use Conversion Tool. please let me know how to convert iBATIS 2.x files into MyBatis? files.

I downloaded ibatis2mybatis.zip file. and read instruction.txt file. There is some information but it is too simple. like below.

Put Your old ibatis2 sqlmap files in the source folder and execute the ant build file.

please can you tell me more details and further information. thank you in advance for your help.

Comment by reachtov...@gmail.com, Nov 21, 2011

I am having a hard time figuring out how to do batch the stored proc calls in MYBATIS/Spring.

We have used IBATIS/Spring earlier and had used SqlMapExecutor? to start batches and execute the same. MYBATIS doesn't seem to provide similar api. I have tried created Executors of type BATCH as follows :

<bean id="sqlSessionForBatchOperations" class="org.mybatis.spring.SqlSessionTemplate">

<constructor-arg index="0" ref="perfLoggingSqlSessionFactory" /> <constructor-arg index="1" value="BATCH" />
</bean>

Please provide some insight into this..

Comment by apope...@gmail.com, Nov 22, 2011

In ibatis 2 one could map parameter using nullValue to set a default or alternative for null objects. What happen with it? Is there an equivalent?

Comment by johnlmor...@gmail.com, Dec 1, 2011

Hi Folks,

I'm using the ibatis2mybatis tool to migrate the old iBatis 2.3 xml files to mybatis 3.x.. getting Connection timed out: connect...any ideas? Or even is there a trouble shooting guide?

BUILD FAILED javax.xml.transform.TransformerException?: javax.xml.transform.TransformerException?: com.sun.org.apache.xml.internal.utils.WrappedRuntimeEx? eption: Connection timed out: connect

at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl?.transform(TransformerImpl?.java:654) at

Comment by project member eduardo.macarron, Dec 1, 2011

any of your file DTDs has a wrong url?

Comment by johnlmor...@gmail.com, Dec 2, 2011

Hi eduardo,

The dtd referred to in my iBatis sql-map-config.xml is:

<!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN" "http://www.ibatis.com/dtd/sql-map-2.dtd">

..and the dtd referred to in all my iBatis files containing <sqlMap> is

<!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN" "http://www.ibatis.com/dtd/sql-map-2.dtd">

Comment by johnlmor...@gmail.com, Dec 2, 2011

..sorry that should have read

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

<!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN" "http://www.ibatis.com/dtd/sql-map-2.dtd">

Comment by leo.h...@gmail.com, May 8, 2012

johnlmorgan: were you able to get past this problem? I'm seeing the same thing and am wondering if it's a proxy server issue...

Comment by leo.h...@gmail.com, May 8, 2012

Spoke too soon! Yep...that was it. Follow the instructions here to get by this error: http://ant.apache.org/manual/proxy.html


Sign in to add a comment
Powered by Google Project Hosting