zoukankan      html  css  js  c++  java
  • 关于spring和ibatis的整合

    没大段时间写博客,工作的时候记录一些,等有时间了再写博客总结吧。现在都是加班来会议一天到底学到了什么,然后记录一些... 觉得盲目的工作实在是太无趣了。

    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

  • 相关阅读:
    Django —— DateTimeField格式
    Django——权限组件(中间件判断用户权限--URL初级)
    linux命令
    性能测试--测试分类
    web安全之csrf攻击
    web安全之xss攻击
    测试用例规范
    禅道操作手册
    fiddler弱网测试
    Web测试系列之测试工具
  • 原文地址:https://www.cnblogs.com/killbug/p/2831715.html
Copyright © 2011-2022 走看看