首先配置datasource:这个配置和整合hibernate的配置一样,这里就不贴代码了,

直接贴配置sqlMapClient的bean配置:

 

 
  
  1. <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> 
  2.         <property name="dataSource" ref="dataSource" /> 
  3.          <property name="configLocations"> 
  4.         <value>classpath*:/sqlmap-config/sqlmap-config.xml</value> 
  5.     </property> 
  6.     <property name="mappingLocations"> 
  7.         <list> 
  8.             <value>classpath*:/sqlmap/**/*.xml</value> 
  9.         </list> 
  10.     </property> 
  11.     </bean> 

其中configLocations是ibatis的配置文件路径,mappingLocations 可以当成是ibatis的映射文件

,其中这个文件的事例代码如下:

sqlmap-config

 

 
  
  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <!DOCTYPE sqlMapConfig PUBLIC "-//iBatis.com//DTD SQL Map Config 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-config-2.dtd"> 
  3. <!-- Always ensure to use the correct XML header as above! --> 
  4. <sqlMapConfig> 
  5.     <settings useStatementNamespaces="true" enhancementEnabled="true" maxTransactions="100" /> 
  6.  
  7. </sqlMapConfig> 

映射文件的内容(列举其中的demo之一):

 

 
  
  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd"> 
  3. <sqlMap namespace="sqlmap-config"> 
  4.      
  5.     <select id="testSqlServer" parameterClass="java.util.HashMap" resultClass="java.util.HashMap" > 
  6.         SELECT name FROM systypes 
  7.     </select> 
  8.     <select id="testMySQL" parameterClass="java.util.HashMap" resultClass="java.util.HashMap" > 
  9.         select SYSDATE() from dual 
  10.     </select> 
  11. </sqlMap> 

有了这些内容之后,简单配置一个baseDao:

 

 
  
  1. <bean id="baseDao" class="com.lz.db.BaseDao" 
  2.     init-method="initialize"> 
  3.     <property name="dataSource"> 
  4.         <ref bean="dataSource" /> 
  5.     </property> 
  6.     <property name="sqlMapClient"> 
  7.         <ref bean="sqlMapClient" /> 
  8.     </property> 
  9. </bean> 

 

接下来是baseDao里面的代码:

 

 

 
  
  1. public  class BaseDao extends SqlMapClientDaoSupport implements 
  2.         IBaseDao { 
  3.  
  4.  
  5.  
  6.     public void initialize() throws Exception { 
  7.         
  8.             SqlMapClient sqlMapClient = getSqlMapClientTemplate() 
  9.                     .getSqlMapClient(); 
  10.       
  11.     } 
  12.  
  13.     public long getObjectTotal(String selectQuery, Object parameterObject) { 
  14.         prepareCountQuery(selectQuery); 
  15.         return (Long) getSqlMapClientTemplate().queryForObject( 
  16.                 CountStatementUtil.getCountStatementId(selectQuery), 
  17.                 parameterObject); 
  18.     } 
  19.  
  20.     public long getObjectTotal(String selectQuery) { 
  21.         prepareCountQuery(selectQuery); 
  22.         return (Long) getSqlMapClientTemplate().queryForObject( 
  23.                 CountStatementUtil.getCountStatementId(selectQuery)); 
  24.     } 
  25.  
  26.     protected void prepareCountQuery(String selectQuery) { 
  27.         String countQuery = CountStatementUtil.getCountStatementId(selectQuery); 
  28.         if (logger.isDebugEnabled()) { 
  29.             logger.debug("Convert " + selectQuery + " to " + countQuery); 
  30.         } 
  31.         SqlMapClient sqlMapClient = getSqlMapClientTemplate().getSqlMapClient(); 
  32.         if (sqlMapClient instanceof ExtendedSqlMapClient) { 
  33.             SqlMapExecutorDelegate delegate = ((ExtendedSqlMapClient) sqlMapClient) 
  34.                     .getDelegate(); 
  35.             try { 
  36.                 delegate.getMappedStatement(countQuery); 
  37.             } catch (SqlMapException e) { 
  38.                 delegate.addMappedStatement(CountStatementUtil 
  39.                         .createCountStatement(delegate 
  40.                                 .getMappedStatement(selectQuery))); 
  41.             } 
  42.  
  43.         } 
  44.     } 
  45.     Map param = new HashMap(); 
  46.     private static final Logger log = LogManager.getLogger(BaseDao.class); 
  47.     public void addParameter(String key, Object value) { 
  48.         // TODO Auto-generated method stub 
  49.         this.param.put(key, value); 
  50.     } 
  51.  
  52.     public void clearParameters() { 
  53.         // TODO Auto-generated method stub 
  54.         this.param.clear(); 
  55.     } 
  56.  
  57.     public int delete(String sqlId, Object param) { 
  58.         // TODO Auto-generated method stub 
  59.         try { 
  60.             return getSqlMapClientTemplate().getSqlMapClient().delete(sqlId, param); 
  61.         } catch (SQLException e) { 
  62.             // TODO Auto-generated catch block 
  63.             log.error("删除数据错误:sqlId=" + sqlId + " param:" + param); 
  64.             e.printStackTrace(); 
  65.             throw new DBException("delete error::sqlId=" + sqlId + " param:" 
  66.                     + param); 
  67.         } 
  68.     } 
  69.  
  70.     public int delete(String sqlId) { 
  71.  
  72.         // TODO Auto-generated method stub 
  73.         int n = this.delete(sqlId, this.param); 
  74.         this.clearParameters(); 
  75.         return n; 
  76.  
  77.     } 
  78.  
  79.     public Object insert(String sqlId, Object param) { 
  80.         // TODO Auto-generated method stub 
  81.         try { 
  82.             return getSqlMapClientTemplate().getSqlMapClient().insert(sqlId, param); 
  83.         } catch (SQLException e) { 
  84.             // TODO Auto-generated catch block 
  85.             log.error("插入数据错误:sqlId=" + sqlId + " param:" + param); 
  86.             e.printStackTrace(); 
  87.             throw new DBException("insert error::sqlId=" + sqlId + " param:" 
  88.                     + param); 
  89.         } 
  90.     } 
  91.  
  92.     public Object insert(String sqlId) { 
  93.         // TODO Auto-generated method stub 
  94.         Object obj = this.insert(sqlId, this.param); 
  95.         this.clearParameters(); 
  96.         return obj; 
  97.     } 
  98.  
  99.     public List queryForList(String sqlId, Object param) { 
  100.         // TODO Auto-generated method stub 
  101.         try { 
  102.             return getSqlMapClientTemplate().getSqlMapClient().queryForList(sqlId, param); 
  103.         } catch (SQLException e) { 
  104.             // TODO Auto-generated catch block 
  105.             log.error("查询数据错误:sqlId=" + sqlId + " param:" + param); 
  106.             e.printStackTrace(); 
  107.             throw new DBException("queryForList error::sqlId=" + sqlId 
  108.                     + " param:" + param); 
  109.         } 
  110.     } 
  111.     public List queryForListPage(String sqlId, Object param,int skip,int max) { 
  112.         // TODO Auto-generated method stub 
  113.         try { 
  114.             return getSqlMapClientTemplate().getSqlMapClient().queryForList(sqlId, param,skip,max); 
  115.         } catch (SQLException e) { 
  116.             // TODO Auto-generated catch block 
  117.             log.error("查询数据错误:sqlId=" + sqlId + " param:" + param); 
  118.             e.printStackTrace(); 
  119.             throw new DBException("queryForList error::sqlId=" + sqlId 
  120.                     + " param:" + param); 
  121.         } 
  122.     } 
  123.      
  124.     /** 
  125.      * 通过页索引和每页记录数查询页数据(从0开始索引) 
  126.      * @param sqlId 
  127.      * @param param 
  128.      * @param pageIndex 
  129.      * @param length 
  130.      * @return 
  131.      */ 
  132.     public List queryForListByPageIndexAndLength(String sqlId, Object param,int pageIndex,int length){       
  133.         return queryForListPage(sqlId,param,pageIndex*length,(pageIndex+1)*length); 
  134.     } 
  135.      
  136.     public List queryForList(String sqlId) { 
  137.         // TODO Auto-generated method stub 
  138.         List list = this.queryForList(sqlId, this.param); 
  139.         this.clearParameters(); 
  140.         return list; 
  141.     } 
  142.  
  143.     public int update(String sqlId, Object param) { 
  144.         // TODO Auto-generated method stub 
  145.         try { 
  146.             return getSqlMapClientTemplate().getSqlMapClient().update(sqlId, param); 
  147.         } catch (SQLException e) { 
  148.             // TODO Auto-generated catch block 
  149.             log.error("更新数据错误:sqlId=" + sqlId + " param:" + param); 
  150.             e.printStackTrace(); 
  151.             throw new DBException("update error::sqlId=" + sqlId + " param:" 
  152.                     + param); 
  153.         } 
  154.     } 
  155.  
  156.     public int update(String sqlId) { 
  157.         // TODO Auto-generated method stub 
  158.         int n = this.update(sqlId, this.param); 
  159.         this.clearParameters(); 
  160.         return n; 
  161.     } 
  162.  
  163.     public Map queryForMap(String sqlId, Object param) { 
  164.         // TODO Auto-generated method stub 
  165.         return (Map) this.queryForObject(sqlId, param); 
  166.     } 
  167.  
  168.     public Map queryForMap(String sqlId) { 
  169.         // TODO Auto-generated method stub 
  170.         return (Map) this.queryForObject(sqlId); 
  171.     } 
  172.  
  173.     public Object queryForObject(String sqlId, Object param) { 
  174.         // TODO Auto-generated method stub 
  175.         List lt = this.queryForList(sqlId, param); 
  176.         if (lt != null && lt.size() > 0) { 
  177.             return (lt.get(0)); 
  178.         } 
  179.         return null
  180.     } 
  181.  
  182.     public Object queryForObject(String sqlId) { 
  183.         // TODO Auto-generated method stub 
  184.         Object obj = this.queryForObject(sqlId, this.param); 
  185.         this.clearParameters(); 
  186.         return obj; 
  187.     } 
  188.