Spring 3.0整合ibatis2.0

1、定义数据库连接池的属性文件,pool.properties

jdbc.user=hgw
jdbc.password=hgw
jdbc.jdbcUrl=jdbc:oracle:thin:@192.168.0.164:1530:dde1
jdbc.driverClass=oracle.jdbc.driver.OracleDriver
jdbc.poolmax=3
jdbc.poolmin=1
jdbc.poolinit=1

2、Spring中配置数据源 和sqlMapClient ,下面是一个配置文件AppContext.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"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:oxm="http://www.springframework.org/schema/oxm"
	xmlns:flow="http://www.springframework.org/schema/webflow-config"
	xmlns:int-mail="http://www.springframework.org/schema/integration/mail"
	xmlns:int-jms="http://www.springframework.org/schema/integration/jms"
	xmlns:int-stream="http://www.springframework.org/schema/integration/stream"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	xmlns:jee="http://www.springframework.org/schema/jee"
	xmlns:jms="http://www.springframework.org/schema/jms"
	default-autowire="byName" default-lazy-init="false"
	xmlns:lang="http://www.springframework.org/schema/lang"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:task="http://www.springframework.org/schema/task"
	xmlns:int-file="http://www.springframework.org/schema/integration/file"
	xsi:schemaLocation="http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
		http://www.springframework.org/schema/integration/mail http://www.springframework.org/schema/integration/mail/spring-integration-mail.xsd
		http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
		http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd
		http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
		http://www.springframework.org/schema/webflow-config http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.0.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
		http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
		http://www.springframework.org/schema/integration/file http://www.springframework.org/schema/integration/file/spring-integration-file.xsd
		http://www.springframework.org/schema/integration/jms http://www.springframework.org/schema/integration/jms/spring-integration-jms.xsd
		http://www.springframework.org/schema/integration/stream http://www.springframework.org/schema/integration/stream/spring-integration-stream.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 
          
      <!-- 自动扫描bean,把作了注解的类转换为bean -->  
      <mvc:annotation-driven />
      <context:component-scan base-package="com.mvc" />  
      <!--可使用标签加载properties文件<context:property-placeholder location='classpath:cfg.properties' /> -->   
      <mvc:resources mapping="/resources/**" location="/resources/"/>
      <aop:config proxy-target-class="true"/>
     <import resource="comm.xml" /><!--其他bean的配置信息-->   
     <!--单数据源事务控制 -->
      <tx:annotation-driven transaction-manager="txManager"/>
       <bean id="propertyConfigurer"  
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
         <property name="locations">   
            <list>   
                <value>classpath:cfg.properties</value>
            </list>   
          </property>  
       </bean>
      <!-- 核心系统数据源(单数据源事务控制方式) --> 
     

<bean id="dataSource" name="dataSource"    class="com.mchange.v2.c3p0.ComboPooledDataSource">    <property name="user" value="${jdbc.user}"></property>    <property name="password" value="${jdbc.password}"></property>    <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>    <property name="driverClass" value="${jdbc.driverClass}"></property>    <property name="maxPoolSize" value="${jdbc.poolmax}"></property>    <property name="minPoolSize" value="${jdbc.poolmin}"></property>    <property name="initialPoolSize" value="${jdbc.poolinit}"></property>   </bean> <!--配置sqlMapClient-->       <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">         <property name="configLocation">             <value>classpath:sql-map-config.xml</value>         </property>                 property name="configLocation">             <value>classpath:sql-map-config.xml</value>         </property>

<!--加载多个ibatis配置文件-->

<property name="mappingLocations">          <value>classpath*:/com/mvc/**/*-ibatis2.xml</value>         </property>

<property name="dataSource" ref="dataSource"/>         <property name="useTransactionAwareDataSource" value="true"></property><!--启用注解事务-->     </bean>

</beans>

3、ibatis2.0配置文件信息

<?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
	    cacheModelsEnabled="false"
	    lazyLoadingEnabled="false"
	    enhancementEnabled="false"
	    maxSessions="1500"
	    maxTransactions="1500"
	    maxRequests="1500"
             useStatementNamespaces="true"/>
    <!--这里无需配置sqlMap,可以通过spring的配置加载某个目录的多个-ibatis2.xml,在这里定义可以指定Spring按顺序加载配置文件,解决出现Ibatis
   中是用include出现的报错-->
    <sqlMap resource="sqlmap/commons-ibatis2.xml"/>
    <sqlMap resource="sqlmap/DpBaseSoftwareDao-ibatis2.xml"/>
    <sqlMap resource="sqlmap/DpDeployHistoryDao-ibatis2.xml"/>
    <sqlMap resource="sqlmap/DpDirectoryDao-ibatis2.xml"/>
    <sqlMap resource="sqlmap/DpDomainDao-ibatis2.xml"/>
    <sqlMap resource="sqlmap/DpEnvironmentDao-ibatis2.xml"/>
    <sqlMap resource="sqlmap/DpInstanceDao-ibatis2.xml"/>
    <sqlMap resource="sqlmap/DpMachineDao-ibatis2.xml"/>
    <sqlMap resource="sqlmap/DpServicePortDao-ibatis2.xml"/>
    <sqlMap resource="sqlmap/DpSubsystemDao-ibatis2.xml"/>
    <sqlMap resource="sqlmap/DpSubsysVersionDao-ibatis2.xml"/>
    <sqlMap resource="sqlmap/DpClusterDao-ibatis2.xml"/>
    <sqlMap resource="sqlmap/deploy-cmd-ibatis2.xml"/>
    <sqlMap resource="sqlmap/PreDeployDao-ibatis2.xml"/>
</sqlMapConfig>

4、使用ibatis实现CRUD,通过定义DAO继承Spring 的SqlMapClientDaoSupport来完成。

import java.util.Map;
import java.util.List;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import org.springframework.stereotype.Repository;

import com.sinolife.deployplatform.basic.domain.DpBaseSoftware;

@Repository("dpBaseSoftware")
public class DpBaseSoftwareDao extends SqlMapClientDaoSupport {
	
    @SuppressWarnings("unchecked")
    public List<Map<String,String>> getPaginateInfoByObject(DpBaseSoftware dpBaseSoftware) {
	return getSqlMapClientTemplate().queryForList(DpBaseSoftwareDao.class.getName() + ".gatPaginateInfo",dpBaseSoftware);
    }

    public int getPaginateInfoCount(DpBaseSoftware dpBaseSoftware) {
	
	return (Integer) super.getSqlMapClientTemplate().queryForObject(DpBaseSoftwareDao.class.getName() + ".getPaginateInfoCount",dpBaseSoftware);
    }
    
    public void insertDpBaseSoftwareData(DpBaseSoftware dpBaseSoftware){
	
	getSqlMapClientTemplate().insert(DpBaseSoftwareDao.class.getName()+".insertDpBaseSoftwareData", dpBaseSoftware);
    }   
    public void updateDpBaseSoftwareData(DpBaseSoftware dpBaseSoftware){
	
	getSqlMapClientTemplate().update(DpBaseSoftwareDao.class.getName()+".updateDpBaseSoftwareData", dpBaseSoftware);
    }
    
    public void deleteDpBaseSoftwareData(DpBaseSoftware dpBaseSoftware){
	
	getSqlMapClientTemplate().delete(DpBaseSoftwareDao.class.getName()+".deleteDpBaseSoftwareData", dpBaseSoftware);	
    }

}


5、一个与上述DAO对应的ibatis配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" 
    "http://ibatis.apache.org/dtd/sql-map-2.dtd">

<sqlMap namespace="com.sinolife.deployplatform.basic.dao.DpBaseSoftwareDao">		
	<resultMap id="DpBaseSoftware.resultMap" class="com.sinolife.deployplatform.basic.domain.DpBaseSoftwareBase">
         <result property="softwareId" column="softwareId"/>
         <result property="softwareName" column="softwareName"/>
         <result property="softwareVersion" column="softwareVersion"/>
         <result property="softwareManufactourer" column="softwareManufactourer"/>
         <result property="softwareDesc" column="softwareDesc"/>
     </resultMap>
          
      <!--  IMAGE_BUSINESS_TYPE表的信息-->
     <sql id="DpBaseSoftware">
      <![CDATA[ 
            select 
              SOFTWARE_ID as softwareId
              ,
              SOFTWARE_NAME as softwareName
              ,
              SOFTWARE_VERSION as softwareVersion
              ,
              SOFTWARE_MANUFACTOURER as softwareManufactourer
              ,
              SOFTWARE_DESC as softwareDesc
              
          from DP_BASE_SOFTWARE                               
        ]]>
        <dynamic prepend="WHERE">
	  <isNotEmpty prepend="AND" property="dpBaseSoftwareBase.softwareId">
		SOFTWARE_ID =#dpBaseSoftwareBase.softwareId#
	  </isNotEmpty>
	  <isNotEmpty prepend="AND" property="dpBaseSoftwareBase.softwareName">
		SOFTWARE_NAME =#dpBaseSoftwareBase.softwareName#
	   </isNotEmpty>
	  <isNotEmpty prepend="AND" property="dpBaseSoftwareBase.softwareVersion">
		SOFTWARE_VERSION =#dpBaseSoftwareBase.softwareVersion#
	  </isNotEmpty>
	 <isNotEmpty prepend="AND" property="dpBaseSoftwareBase.softwareManufactourer">
		SOFTWARE_MANUFACTOURER =#dpBaseSoftwareBase.softwareManufactourer#
	 </isNotEmpty>
	 <isNotEmpty prepend="AND" property="dpBaseSoftwareBase.softwareDesc">
	        SOFTWARE_DESC =#dpBaseSoftwareBase.softwareDesc#
	 </isNotEmpty>
        </dynamic>	                        
      </sql>        
     <select id="gatPaginateInfo"  parameterClass="com.sinolife.deployplatform.basic.domain.DpBaseSoftware"  
                                   resultMap="DpBaseSoftware.resultMap">                                               
	   <include refid="commons.pagePrefix"/> 
	   <include refid="DpBaseSoftware" />
          <include refid="commons.pageSuffix"/> 
     </select>
     
     <select id="getPaginateInfoCount" parameterClass="com.sinolife.deployplatform.basic.domain.DpBaseSoftware"  
                                       resultClass="java.lang.Integer" >                                             
        <include refid="commons.countPrefix" />
        <include refid="DpBaseSoftware" />
        <include refid="commons.countSuffix" />
     </select> 
     <insert id="insertDpBaseSoftwareData" parameterClass="com.sinolife.deployplatform.basic.domain.DpBaseSoftware">
           <![CDATA[
		        INSERT INTO DP_BASE_SOFTWARE (
		        	SOFTWARE_ID ,
		        	SOFTWARE_NAME ,
		        	SOFTWARE_VERSION ,
		        	SOFTWARE_MANUFACTOURER ,
		        	SOFTWARE_DESC 
		        ) VALUES (
		        	#dpBaseSoftwareBase.softwareId# ,
		        	#dpBaseSoftwareBase.softwareName# ,
		        	#dpBaseSoftwareBase.softwareVersion# ,
		        	#dpBaseSoftwareBase.softwareManufactourer# ,
		        	#dpBaseSoftwareBase.softwareDesc# 
		        )
		    ]]>
	</insert>
	<update id="updateDpBaseSoftwareData" parameterClass="com.sinolife.deployplatform.basic.domain.DpBaseSoftware">
         <![CDATA[
	        UPDATE DP_BASE_SOFTWARE SET
		          SOFTWARE_NAME = #dpBaseSoftwareBase.softwareName# ,
		          SOFTWARE_VERSION = #dpBaseSoftwareBase.softwareVersion# ,
		          SOFTWARE_MANUFACTOURER = #dpBaseSoftwareBase.softwareManufactourer# ,
		          SOFTWARE_DESC = #dpBaseSoftwareBase.softwareDesc# 
	        WHERE 
		         SOFTWARE_ID = #dpBaseSoftwareBase.softwareId# 
    	  ]]>
	</update>  	
	 <delete id="deleteDpBaseSoftwareData" parameterClass="com.sinolife.deployplatform.basic.domain.DpBaseSoftware">	   
	    <![CDATA[
	        DELETE FROM DP_BASE_SOFTWARE WHERE
		          SOFTWARE_ID = #dpBaseSoftwareBase.softwareId# 
    	]]>
	</delete>
	
</sqlMap>


                                                                                                          

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值