首先配置datasource:这个配置和整合hibernate的配置一样,这里就不贴代码了,
直接贴配置sqlMapClient的bean配置:
- <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <property name="configLocations">
- <value>classpath*:/sqlmap-config/sqlmap-config.xml</value>
- </property>
- <property name="mappingLocations">
- <list>
- <value>classpath*:/sqlmap/**/*.xml</value>
- </list>
- </property>
- </bean>
其中configLocations是ibatis的配置文件路径,mappingLocations 可以当成是ibatis的映射文件
,其中这个文件的事例代码如下:
sqlmap-config
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE sqlMapConfig PUBLIC "-//iBatis.com//DTD SQL Map Config 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
- <!-- Always ensure to use the correct XML header as above! -->
- <sqlMapConfig>
- <settings useStatementNamespaces="true" enhancementEnabled="true" maxTransactions="100" />
- </sqlMapConfig>
映射文件的内容(列举其中的demo之一):
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd">
- <sqlMap namespace="sqlmap-config">
- <select id="testSqlServer" parameterClass="java.util.HashMap" resultClass="java.util.HashMap" >
- SELECT name FROM systypes
- </select>
- <select id="testMySQL" parameterClass="java.util.HashMap" resultClass="java.util.HashMap" >
- select SYSDATE() from dual
- </select>
- </sqlMap>
有了这些内容之后,简单配置一个baseDao:
- <bean id="baseDao" class="com.lz.db.BaseDao"
- init-method="initialize">
- <property name="dataSource">
- <ref bean="dataSource" />
- </property>
- <property name="sqlMapClient">
- <ref bean="sqlMapClient" />
- </property>
- </bean>
接下来是baseDao里面的代码:
- public class BaseDao extends SqlMapClientDaoSupport implements
- IBaseDao {
- public void initialize() throws Exception {
- SqlMapClient sqlMapClient = getSqlMapClientTemplate()
- .getSqlMapClient();
- }
- public long getObjectTotal(String selectQuery, Object parameterObject) {
- prepareCountQuery(selectQuery);
- return (Long) getSqlMapClientTemplate().queryForObject(
- CountStatementUtil.getCountStatementId(selectQuery),
- parameterObject);
- }
- public long getObjectTotal(String selectQuery) {
- prepareCountQuery(selectQuery);
- return (Long) getSqlMapClientTemplate().queryForObject(
- CountStatementUtil.getCountStatementId(selectQuery));
- }
- protected void prepareCountQuery(String selectQuery) {
- String countQuery = CountStatementUtil.getCountStatementId(selectQuery);
- if (logger.isDebugEnabled()) {
- logger.debug("Convert " + selectQuery + " to " + countQuery);
- }
- SqlMapClient sqlMapClient = getSqlMapClientTemplate().getSqlMapClient();
- if (sqlMapClient instanceof ExtendedSqlMapClient) {
- SqlMapExecutorDelegate delegate = ((ExtendedSqlMapClient) sqlMapClient)
- .getDelegate();
- try {
- delegate.getMappedStatement(countQuery);
- } catch (SqlMapException e) {
- delegate.addMappedStatement(CountStatementUtil
- .createCountStatement(delegate
- .getMappedStatement(selectQuery)));
- }
- }
- }
- Map param = new HashMap();
- private static final Logger log = LogManager.getLogger(BaseDao.class);
- public void addParameter(String key, Object value) {
- // TODO Auto-generated method stub
- this.param.put(key, value);
- }
- public void clearParameters() {
- // TODO Auto-generated method stub
- this.param.clear();
- }
- public int delete(String sqlId, Object param) {
- // TODO Auto-generated method stub
- try {
- return getSqlMapClientTemplate().getSqlMapClient().delete(sqlId, param);
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- log.error("删除数据错误:sqlId=" + sqlId + " param:" + param);
- e.printStackTrace();
- throw new DBException("delete error::sqlId=" + sqlId + " param:"
- + param);
- }
- }
- public int delete(String sqlId) {
- // TODO Auto-generated method stub
- int n = this.delete(sqlId, this.param);
- this.clearParameters();
- return n;
- }
- public Object insert(String sqlId, Object param) {
- // TODO Auto-generated method stub
- try {
- return getSqlMapClientTemplate().getSqlMapClient().insert(sqlId, param);
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- log.error("插入数据错误:sqlId=" + sqlId + " param:" + param);
- e.printStackTrace();
- throw new DBException("insert error::sqlId=" + sqlId + " param:"
- + param);
- }
- }
- public Object insert(String sqlId) {
- // TODO Auto-generated method stub
- Object obj = this.insert(sqlId, this.param);
- this.clearParameters();
- return obj;
- }
- public List queryForList(String sqlId, Object param) {
- // TODO Auto-generated method stub
- try {
- return getSqlMapClientTemplate().getSqlMapClient().queryForList(sqlId, param);
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- log.error("查询数据错误:sqlId=" + sqlId + " param:" + param);
- e.printStackTrace();
- throw new DBException("queryForList error::sqlId=" + sqlId
- + " param:" + param);
- }
- }
- public List queryForListPage(String sqlId, Object param,int skip,int max) {
- // TODO Auto-generated method stub
- try {
- return getSqlMapClientTemplate().getSqlMapClient().queryForList(sqlId, param,skip,max);
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- log.error("查询数据错误:sqlId=" + sqlId + " param:" + param);
- e.printStackTrace();
- throw new DBException("queryForList error::sqlId=" + sqlId
- + " param:" + param);
- }
- }
- /**
- * 通过页索引和每页记录数查询页数据(从0开始索引)
- * @param sqlId
- * @param param
- * @param pageIndex
- * @param length
- * @return
- */
- public List queryForListByPageIndexAndLength(String sqlId, Object param,int pageIndex,int length){
- return queryForListPage(sqlId,param,pageIndex*length,(pageIndex+1)*length);
- }
- public List queryForList(String sqlId) {
- // TODO Auto-generated method stub
- List list = this.queryForList(sqlId, this.param);
- this.clearParameters();
- return list;
- }
- public int update(String sqlId, Object param) {
- // TODO Auto-generated method stub
- try {
- return getSqlMapClientTemplate().getSqlMapClient().update(sqlId, param);
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- log.error("更新数据错误:sqlId=" + sqlId + " param:" + param);
- e.printStackTrace();
- throw new DBException("update error::sqlId=" + sqlId + " param:"
- + param);
- }
- }
- public int update(String sqlId) {
- // TODO Auto-generated method stub
- int n = this.update(sqlId, this.param);
- this.clearParameters();
- return n;
- }
- public Map queryForMap(String sqlId, Object param) {
- // TODO Auto-generated method stub
- return (Map) this.queryForObject(sqlId, param);
- }
- public Map queryForMap(String sqlId) {
- // TODO Auto-generated method stub
- return (Map) this.queryForObject(sqlId);
- }
- public Object queryForObject(String sqlId, Object param) {
- // TODO Auto-generated method stub
- List lt = this.queryForList(sqlId, param);
- if (lt != null && lt.size() > 0) {
- return (lt.get(0));
- }
- return null;
- }
- public Object queryForObject(String sqlId) {
- // TODO Auto-generated method stub
- Object obj = this.queryForObject(sqlId, this.param);
- this.clearParameters();
- return obj;
- }
- }