關於spring和ibatis的整合

snake_hand發表於2013-04-08

沒大段時間寫部落格,工作的時候記錄一些,等有時間了再寫部落格總結吧。現在都是加班來會議一天到底學到了什麼,然後記錄一些... 覺得盲目的工作實在是太無趣了。

spring現在普及度很廣,在專案中就像千手觀音一般,無所不能。

而ibatis幾十年來的orm,現已經轉成myBitis,鑑於現在orm的數目是在太多,ibatis也越來越少人用了,事實上一個orm在spring看來只是千手觀音中的一個手上的法寶罷了。

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <import resource="classpath:applicationContext-dao.xml" />
    <import resource="classpath:applicationContext-resources.xml" />
    <import resource="classpath:applicationContext-service.xml" />
    
</beans>

相當清晰。

spring拿到ibatis法寶:

applicationContext-dao.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"
    default-lazy-init="true">

    <!-- Transaction manager for a single JDBC DataSource -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>


    <!-- SqlMap setup for iBATIS Database Layer -->
    <bean id="sqlMapClient"
        class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
        <property name="configLocation">
            <value>classpath:/sql-map-config.xml</value>
        </property>
        <property name="dataSource" ref="dataSource" />
    </bean>


    <bean id="userDao" class="com.syezon.webapp.dao.ibatis.UserDaoImpl">
        <property name="dataSource" ref="dataSource" />
        <property name="sqlMapClient" ref="sqlMapClient" />
    </bean>
</beans>

配置過單獨的ibatis的話,就知道我們是要自己配置transactionManagersqlMapClient的。

現在我們把這兩個物件交給spring管理了

注意到sqlMapClient引數的configLocation:classpath:/sql-map-config.xml

我們來看看:

 

<?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 enhancementEnabled="true" maxTransactions="20"
        maxRequests="32" maxSessions="10" useStatementNamespaces="true" />

    <!-- Identify all SQL Map XML files to be loaded by this SQL map. Relative to classpath -->

    
    <sqlMap resource="sqlmaps/SaleS2dSQL.xml" />
    <sqlMap resource="sqlmaps/DealerSQL.xml" />
</sqlMapConfig>

 

resource就是寫sql文的檔案啦,那麼就讓我們開始寫sql吧。
來兩條select語句吧:其中蘊含了大部分可能遇到的問題,包括
isEqual 判斷,<![CDATA[是為了尖括號能夠使用
    <select id="getUsedAdmins" parameterClass="java.util.Map"
        resultMap="adminNameResult">
        <![CDATA[
        select id,name from tb_admin where  status = 1
    ]]>
        <isEqual property="type" compareValue="3">
            <![CDATA[  and roleid=3 ]]>
        </isEqual>
        <isEqual property="type" compareValue="4">
            <![CDATA[ and roleid>3 and roleid<6 ]]>
        </isEqual>
    </select>
 <select id="getAdmins" parameterClass="java.util.Map" resultMap="adminResult">
        select * from (select rownum rn,m.* from(select *
        from tb_admin where
        status=1 and roleid=1
        <isNotEqual property="roleid" compareValue="0" prepend="and">
            id
            in(select user_id from qx_user_role where role_id=#roleid#)
        </isNotEqual>
    <![CDATA[         
        order by id desc)m )
        where rn>#firstrow# and rn<#maxrow#
    ]]>
    </select>

    <select id="getAdmin" parameterClass="java.lang.Long" resultMap="adminResult">
        <![CDATA[
        select * from tb_admin where id = #value# and status = 1
    ]]>
    </select>

insert語句可能會牽涉到自增長自然主鍵

http://www.cnblogs.com/killbug/archive/2012/12/23/2830252.html

 

相關文章