You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user-java@ibatis.apache.org by Daniel Guido <dg...@gmail.com> on 2008/06/27 22:50:08 UTC

beginner spring+ibatis help

I'm a student and my grasp of JSP/J2EE, iBatis, and Spring is a little
iffy. I'm attempting to learn all of them as part of a project and I'd
appreciate any help you could offer me (and to save time, yes, I've
read the developer's guide pdf).

I followed the Spring tutorial to create their little inventory
management demo-app. It works fine with JDBC and HSQL and I mostly
understand what's going on. The URL for that tutorial is here:
http://static.springframework.org/docs/Spring-MVC-step-by-step/

Now I'm trying to replace the JDBC DAO with an iBatis DAO. I think
I've got everything put together correctly, but my ProductDao (the
parent class to my SqlMapProductDao) has a method: public
List<Product> getProductList(); It was created in this step of the
tutorial: http://static.springframework.org/docs/Spring-MVC-step-by-step/part5.html#step5.4

How do I create an equivalent function in the SqlMapProductDao? I made
a sql map that looks like this (my columns and properties match, no
need for result maps):
<select id="selectAllProducts" resultMap="ProductResult">
    select * from products
</select>

The following is _completely_ wrong but at least shows what I'd like it to do:


    public List<Product> getProductList() throws DataAccessException {
      logger.info("Getting products! (iBatis)");

      this.sqlMapClient.startTransaction();
      try {
          List<Product> allProducts =
this.sqlMapClient.queryForList("selectAllProducts");
          return allProducts;
      }
      catch (SQLException ex) {}
      this.sqlMapClient.commitTransaction();
    }


based off this:
http://static.springframework.org/spring/docs/2.5.x/reference/orm.html#orm-ibatis-straight

Can anyone help? I can post more of the source I'm working with if you
think it will help. Thanks.


--
Dan Guido

RE: beginner spring+ibatis help

Posted by Poitras Christian <Ch...@ircm.qc.ca>.
Sorry, I've cut/pasted the wrong class name...

Your SqlMaps should extend SqlMapClientTemplate.
Here is one of my working class.

import org.springframework.orm.ibatis.SqlMapClientTemplate;

@Repository("mascotFileDaoTdlc")
public class MascotFileSqlMap extends SqlMapClientTemplate implements MascotFileDao<MascotFileTdlc> {

    @Autowired
    @Override
    public void setSqlMapClient(@Qualifier("sqlMapClient") SqlMapClient sqlMapClient) {
        super.setSqlMapClient(sqlMapClient);
    }

    /*
     *  (non-Javadoc)
     * @see ca.qc.ircm.proteus.persistence.dao.iface.MascotFileDao#get(java.lang.Long)
     */
    public MascotFileTdlc get(Long oid) {
        return (MascotFileTdlc) this.queryForObject("MascotFileTdlc.getMascotFile", oid);
    }
    /*
     * (non-Javadoc)
     * @see ca.qc.ircm.proteus.persistence.dao.MascotFileDao#list(ca.qc.ircm.proteus.business.Experiment, ca.qc.ircm.proteus.business.Eluate)
     */
    public List<MascotFileTdlc> list(Experiment experiment, Eluate eluate) {
        Map<String, Object> map= new HashMap<String, Object>();
        map.put("experiment", experiment);
        map.put("eluate", eluate);

        @SuppressWarnings("unchecked")
        List<MascotFileTdlc> result = (List<MascotFileTdlc>) this.queryForList("MascotFileTdlc.byExperimentAndEluate", map);
        return result;
    }

Christian


-----Original Message-----
From: Daniel Guido [mailto:dguido@gmail.com]
Sent: Monday, June 30, 2008 2:30 PM
To: user-java@ibatis.apache.org
Subject: Re: beginner spring+ibatis help

Thanks Christian,

I took your suggestion to extend the SqlMap class off SqlMapClientDaoSupport, but I must be missing something because queryForList is unresolved when I used it like you did. It looks like this now:

public class SqlMapProductDao extends SqlMapClientDaoSupport {

    protected final Log logger = LogFactory.getLog(getClass());
    //private SqlMapClient sqlMapClient;

    //public void setSqlMapClient(SqlMapClient sqlMapClient) {
    //  this.sqlMapClient = sqlMapClient;
    //}

    public List<Product> getProductList() {
      logger.info("Getting products! (iBatis)");

      List<Product> allProducts = queryForList("selectAllProducts");
//unresolved
      return allProducts;

    }
...
}

I'm in the middle of reading docs and digging through sample code on Google Code to figure it out, but any additional help would be appreciated.

Also, my service layer has code (without the annotations) that looks very similar to what you wrote:

public class SimpleProductManager implements ProductManager {
  private ProductDao productDao;
  public List<Product> getProducts() {
    return productDao.getProductList();
  }
  public void setProductDao(ProductDao productDao) {
    this.productDao = productDao;
  }
...
}

public interface ProductManager extends Serializable {
    public void increasePrice(int percentage);
    public List<Product> getProducts();
}




--
Dan Guido


On Mon, Jun 30, 2008 at 7:48 AM, Poitras Christian <Ch...@ircm.qc.ca> wrote:
> Hi,
>
> First I'd make my SqlMap class extends SqlMapClientDaoSupport from Spring since it will relieve you of writing a try-catch block.
>
>    public List<Product> getProductList() throws DataAccessException {
>        logger.info("Getting products! (iBatis)");
>
>        List<Product> allProducts = queryForList("selectAllProducts");
>        return allProducts;
>    }
>
>
> Second, I'd handle transaction in a service class.
>
> @Service
> Public class ProductService {
>    @Autowired
>    ProductDao productDao;
>
>    -- Transaction managed by Spring.
>    @Transactional
>    public List<Product> getProductList() throws DataAccessException {
>        return productDao.getProductList();
>    }
> }
>
> This works if you use annotations in Spring. (See annotations in
> section 3.11 and transaction in section 9.5)
>
>
> Christian
>
>
> -----Original Message-----
> From: Daniel Guido [mailto:dguido@gmail.com]
> Sent: Friday, June 27, 2008 4:50 PM
> To: user-java@ibatis.apache.org
> Subject: beginner spring+ibatis help
>
> I'm a student and my grasp of JSP/J2EE, iBatis, and Spring is a little iffy. I'm attempting to learn all of them as part of a project and I'd appreciate any help you could offer me (and to save time, yes, I've read the developer's guide pdf).
>
> I followed the Spring tutorial to create their little inventory management demo-app. It works fine with JDBC and HSQL and I mostly understand what's going on. The URL for that tutorial is here:
> http://static.springframework.org/docs/Spring-MVC-step-by-step/
>
> Now I'm trying to replace the JDBC DAO with an iBatis DAO. I think
> I've got everything put together correctly, but my ProductDao (the
> parent class to my SqlMapProductDao) has a method: public
> List<Product> getProductList(); It was created in this step of the
> tutorial:
> http://static.springframework.org/docs/Spring-MVC-step-by-step/part5.h
> tml#step5.4
>
> How do I create an equivalent function in the SqlMapProductDao? I made a sql map that looks like this (my columns and properties match, no need for result maps):
> <select id="selectAllProducts" resultMap="ProductResult">
>    select * from products
> </select>
>
> The following is _completely_ wrong but at least shows what I'd like it to do:
>
>
>    public List<Product> getProductList() throws DataAccessException {
>      logger.info("Getting products! (iBatis)");
>
>      this.sqlMapClient.startTransaction();
>      try {
>          List<Product> allProducts =
> this.sqlMapClient.queryForList("selectAllProducts");
>          return allProducts;
>      }
>      catch (SQLException ex) {}
>      this.sqlMapClient.commitTransaction();
>    }
>
>
> based off this:
> http://static.springframework.org/spring/docs/2.5.x/reference/orm.html
> #orm-ibatis-straight
>
> Can anyone help? I can post more of the source I'm working with if you think it will help. Thanks.
>
>
> --
> Dan Guido
>

Re: beginner spring+ibatis help

Posted by Daniel Guido <dg...@gmail.com>.
Thanks Christian,

I took your suggestion to extend the SqlMap class off
SqlMapClientDaoSupport, but I must be missing something because
queryForList is unresolved when I used it like you did. It looks like
this now:

public class SqlMapProductDao extends SqlMapClientDaoSupport {

    protected final Log logger = LogFactory.getLog(getClass());
    //private SqlMapClient sqlMapClient;

    //public void setSqlMapClient(SqlMapClient sqlMapClient) {
    //  this.sqlMapClient = sqlMapClient;
    //}

    public List<Product> getProductList() {
      logger.info("Getting products! (iBatis)");

      List<Product> allProducts = queryForList("selectAllProducts");
//unresolved
      return allProducts;

    }
...
}

I'm in the middle of reading docs and digging through sample code on
Google Code to figure it out, but any additional help would be
appreciated.

Also, my service layer has code (without the annotations) that looks
very similar to what you wrote:

public class SimpleProductManager implements ProductManager {
  private ProductDao productDao;
  public List<Product> getProducts() {
    return productDao.getProductList();
  }
  public void setProductDao(ProductDao productDao) {
    this.productDao = productDao;
  }
...
}

public interface ProductManager extends Serializable {
    public void increasePrice(int percentage);
    public List<Product> getProducts();
}




--
Dan Guido


On Mon, Jun 30, 2008 at 7:48 AM, Poitras Christian
<Ch...@ircm.qc.ca> wrote:
> Hi,
>
> First I'd make my SqlMap class extends SqlMapClientDaoSupport from Spring since it will relieve you of writing a try-catch block.
>
>    public List<Product> getProductList() throws DataAccessException {
>        logger.info("Getting products! (iBatis)");
>
>        List<Product> allProducts = queryForList("selectAllProducts");
>        return allProducts;
>    }
>
>
> Second, I'd handle transaction in a service class.
>
> @Service
> Public class ProductService {
>    @Autowired
>    ProductDao productDao;
>
>    -- Transaction managed by Spring.
>    @Transactional
>    public List<Product> getProductList() throws DataAccessException {
>        return productDao.getProductList();
>    }
> }
>
> This works if you use annotations in Spring. (See annotations in section 3.11 and transaction in section 9.5)
>
>
> Christian
>
>
> -----Original Message-----
> From: Daniel Guido [mailto:dguido@gmail.com]
> Sent: Friday, June 27, 2008 4:50 PM
> To: user-java@ibatis.apache.org
> Subject: beginner spring+ibatis help
>
> I'm a student and my grasp of JSP/J2EE, iBatis, and Spring is a little iffy. I'm attempting to learn all of them as part of a project and I'd appreciate any help you could offer me (and to save time, yes, I've read the developer's guide pdf).
>
> I followed the Spring tutorial to create their little inventory management demo-app. It works fine with JDBC and HSQL and I mostly understand what's going on. The URL for that tutorial is here:
> http://static.springframework.org/docs/Spring-MVC-step-by-step/
>
> Now I'm trying to replace the JDBC DAO with an iBatis DAO. I think I've got everything put together correctly, but my ProductDao (the parent class to my SqlMapProductDao) has a method: public List<Product> getProductList(); It was created in this step of the
> tutorial: http://static.springframework.org/docs/Spring-MVC-step-by-step/part5.html#step5.4
>
> How do I create an equivalent function in the SqlMapProductDao? I made a sql map that looks like this (my columns and properties match, no need for result maps):
> <select id="selectAllProducts" resultMap="ProductResult">
>    select * from products
> </select>
>
> The following is _completely_ wrong but at least shows what I'd like it to do:
>
>
>    public List<Product> getProductList() throws DataAccessException {
>      logger.info("Getting products! (iBatis)");
>
>      this.sqlMapClient.startTransaction();
>      try {
>          List<Product> allProducts =
> this.sqlMapClient.queryForList("selectAllProducts");
>          return allProducts;
>      }
>      catch (SQLException ex) {}
>      this.sqlMapClient.commitTransaction();
>    }
>
>
> based off this:
> http://static.springframework.org/spring/docs/2.5.x/reference/orm.html#orm-ibatis-straight
>
> Can anyone help? I can post more of the source I'm working with if you think it will help. Thanks.
>
>
> --
> Dan Guido
>

RE: beginner spring+ibatis help

Posted by Poitras Christian <Ch...@ircm.qc.ca>.
Hi,

First I'd make my SqlMap class extends SqlMapClientDaoSupport from Spring since it will relieve you of writing a try-catch block.

    public List<Product> getProductList() throws DataAccessException {
        logger.info("Getting products! (iBatis)");

        List<Product> allProducts = queryForList("selectAllProducts");
        return allProducts;
    }


Second, I'd handle transaction in a service class.

@Service
Public class ProductService {
    @Autowired
    ProductDao productDao;

    -- Transaction managed by Spring.
    @Transactional
    public List<Product> getProductList() throws DataAccessException {
        return productDao.getProductList();
    }
}

This works if you use annotations in Spring. (See annotations in section 3.11 and transaction in section 9.5)


Christian


-----Original Message-----
From: Daniel Guido [mailto:dguido@gmail.com]
Sent: Friday, June 27, 2008 4:50 PM
To: user-java@ibatis.apache.org
Subject: beginner spring+ibatis help

I'm a student and my grasp of JSP/J2EE, iBatis, and Spring is a little iffy. I'm attempting to learn all of them as part of a project and I'd appreciate any help you could offer me (and to save time, yes, I've read the developer's guide pdf).

I followed the Spring tutorial to create their little inventory management demo-app. It works fine with JDBC and HSQL and I mostly understand what's going on. The URL for that tutorial is here:
http://static.springframework.org/docs/Spring-MVC-step-by-step/

Now I'm trying to replace the JDBC DAO with an iBatis DAO. I think I've got everything put together correctly, but my ProductDao (the parent class to my SqlMapProductDao) has a method: public List<Product> getProductList(); It was created in this step of the
tutorial: http://static.springframework.org/docs/Spring-MVC-step-by-step/part5.html#step5.4

How do I create an equivalent function in the SqlMapProductDao? I made a sql map that looks like this (my columns and properties match, no need for result maps):
<select id="selectAllProducts" resultMap="ProductResult">
    select * from products
</select>

The following is _completely_ wrong but at least shows what I'd like it to do:


    public List<Product> getProductList() throws DataAccessException {
      logger.info("Getting products! (iBatis)");

      this.sqlMapClient.startTransaction();
      try {
          List<Product> allProducts =
this.sqlMapClient.queryForList("selectAllProducts");
          return allProducts;
      }
      catch (SQLException ex) {}
      this.sqlMapClient.commitTransaction();
    }


based off this:
http://static.springframework.org/spring/docs/2.5.x/reference/orm.html#orm-ibatis-straight

Can anyone help? I can post more of the source I'm working with if you think it will help. Thanks.


--
Dan Guido