Menu

How do I extend frm SqlMapClientImpl?

nilesh
2004-10-21
2013-04-11
  • nilesh

    nilesh - 2004-10-21

    Does iBatis allow any way of specifying the implementation object for SqlMapClient?

    I want to keep iBatis in my framework level package, so that none of my client code will directly call iBatis. This way I could change the OR Mapping implementation without touching any of client code.

    Only way I could find out to achieve this --

    public class SqlMapProcessor
    {
        protected static SqlMapClient sqlMap;
        private static Logger logger = Logger.getLogger(SqlMapProcessor.class);
        private static final String IBATIS_CONFIG = "iBatis_config";
        private static SqlMapProcessor sqlMapProcessor;
       
        private SqlMapProcessor()
        {
            try
            {
                String configFile = System.getProperty(IBATIS_CONFIG);
                initSqlMap(configFile);
            }catch(Exception e)
            {
                logger.error("Error initializing iBatis client " + e);
            }
        }
       
        private void initSqlMap(String configFile)
        throws Exception
        {
            Reader reader = null;
            try
            {
                reader = Resources.getResourceAsReader(configFile);
                sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
            }finally
            {
                if(reader!=null)
                    reader.close();
            }
        }
       
        public static SqlMapProcessor getInstance()
        {
            if(sqlMapProcessor==null)
                sqlMapProcessor = new SqlMapProcessor();
           
            return sqlMapProcessor;
        }
       
        public Object findByKey(String id, Map data)
        throws Exception
        {
            return sqlMap.queryForObject(id, data);
        }
       
        public Object findLogicalSequence(String id, Map data)
        throws Exception
        {
            Object o = sqlMap.queryForObject(id, data);
            return o==null ? new Integer(1) : o;
        }
       
        public List select(String id, Map data)
        throws Exception
        {
            return sqlMap.queryForList(id, data);
        }
       
        public Object insert(String id, Map data)
        throws Exception
        {
            return sqlMap.insert(id, data);
        }
      
                 .......

                 .......
    }

    This way, SqlMapProcessor became a delegator object and i end up defining each method in here which calls SqlMapClient method...

    Is there any better way of doing this? Please let me know. Thanks.

     
    • roberto r.

      roberto r. - 2004-10-21

      Have you looked at the iBATIS DAO framework or Spring?

      http://www.ibatis.com/common/dao.html
      http://www.springframework.org/

      Roberto

       
    • Clinton Begin

      Clinton Begin - 2004-10-21

      1) You shouldn't have a dependency on the SqlMapClientImpl, so it should not be a problem.

      2) SqlMapClient is a non-final interface, so you should be able to easily extend it and use your extended interface.

      3) Doing either of the above is a poor way to isolate yourself from the persistence framework.  You should use a DAO pattern, either iBATIS DAO, Spring or your own custom DAO implementation.

      Cheers,
      Clinton

       
    • Brandon Goodin

      Brandon Goodin - 2004-10-22

      THIS IS A USER SUPPORT QUESTION AND IS NOT RELATED TO THE DEVELOPMENT OF THE IBATIS FRAMEWORK. PLEASE POST ON THE USER SUPPORT FORUM IN THE FUTURE.

       

Log in to post a comment.