• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

iBatis & DAO....

 
Peter Primrose
Ranch Hand
Posts: 755
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,
I'm using iBatis as my database bridge, and DAO as the design-pattern to communicate with the database. I wonder if this is the correct way the code suppose to look like?

this is done in one of the Action Classes:


User user = new User();
user.setTitle(title);
user.setFname(fname);
user.setMname(mname);
user.setLname(lname);
user.setAddress1(address1);
user.setAddress2(address2);
user.setCity(city);
user.setState(state);
user.setZip(zip);
user.setPhone(phone);
user.setEmail(email.toLowerCase());
user.setPassword(password);


//this is the code to communicate....is this right??? looks too much to me
UserDAO userDAO = (UserDAO) DBDaoManager.getInstance().getDaoManager().getDao(UserDAO.class, "sqlmap");
Long id = userDAO.insert(user);



HttpSession session = request.getSession();
session.setAttribute(Properties.USER_LOGGED, user);


So, my question is this: does the code looks ok? special note to the place where it says 'this is the code to communicate'
I was under the impression that the DAO will do that without this extra line of code (but...this is just an observation on my side).

thanks for any input.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not bad.

UserDAO userDAO = (UserDAO) DBDaoManager.getInstance().getDaoManager().getDao(UserDAO.class, "sqlmap");

Without knowing where the DBDaoManager comes from (what API or if it your own) it's hard to say what it should be doing and if it is too verbose. I typically use Spring to handle all my DAO wiring up. So my code is a bit less verbose (more so even using Stripes for my web framework).
 
Peter Primrose
Ranch Hand
Posts: 755
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I typically use Spring to handle all my DAO wiring up


mmm....that's interesting, mind to elaborate how Spring gets into action here? beside, i get it that the code is fine (nothing to do better?)

thanks again
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code is fine. You could write a getDao method in your DBDaoManager class, to return the DAO :

Dao getDao(java.lang.Class iface, java.lang.String contextId) {
return getDaoManager().getDao(iface, contextId);
}

and then call :
UserDAO userDAO = (UserDAO) DBDaoManager.getInstance().getDao(UserDAO.class, "sqlmap");
 
Peter Primrose
Ranch Hand
Posts: 755
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for your reply. I thought I can significantly reduce the code, but if you and the sheriff say it's ok....then it's ok.

thanks
 
pascal betz
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
or you can write a static getDAO() method... then you can strip the getInstance() call...

but i recomend you to check out the springframework in general and then the ibatis support and TX management section.

pascal
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic