Sample Codes for Three Layers Java Project with Abator and iBatis

Greetings Expert,

Base Information:
- Linux mandriva 2007 32bit
- Apache httpd 2.2.4
- Tomcat 5.5
- Eclipse 3.2.2
- MySQL5
- Jdk 1.6.0/jre
- iBatis 2.3
- AbatorForEclipse 1.0
- Jsf 1.1

I am new for all above environments and trying to learn more :).
I have a table called F0001. it is master countries table.
I use abator as the ibatis code generator for F0001.

this is my simple code MasterCountry.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@ page import="com.ibatis.common.resources.Resources"%>
<%@ page import="java.io.Reader"%>

<%@page import="mysql.model.F0001"%>
<%@page import="mysql.dao.F0001DAO"%>

<%@page import="com.ibatis.dao.client.DaoManager"%>
<%@page import="com.ibatis.dao.client.DaoManagerBuilder"%>
<%@page import="mysql.model.F0001Example"%>
<%@page import="java.util.List"%>
<html>

      <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>Insert title here</title>
      </head>
      <body>
            <form>
                  <%
                  //SECTION 1
                  String resource = "dao.xml";
                  Reader reader = Resources.getResourceAsReader(resource);
                  DaoManager daoManager = DaoManagerBuilder.buildDaoManager(reader);
                  
                  //SECTION 2
                  F0001DAO objF0001DAO = (F0001DAO)
daoManager.getDao(F0001DAO.class, "sqlmap");
                  F0001 objF0001 = new F0001();
                  F0001Example objF0001Example = new F0001Example();
                  List list = objF0001DAO.selectByExample(objF0001Example);
                  
                  //SECTION 3
                  out.println("Selected " + list.size() + " records.<br><br>");
                  for (int i=0; i < list.size(); i++) {
                        objF0001 = (F0001) list.get(i);
                        
                        out.println(objF0001.getCountryid() + " - ");
                        out.println(objF0001.getCountrynm() + "<br>");
                  }
                  
                  %>
            </form>
      </body>
</html>
------------------------------------------------------------------------------------------------------------

Currently I use 1 project for all of this.
I want to split them to 3 project:
- SECTION 1 = Data Access Layer
- SECTION 2 = Business Layer
- SECTION 3 = Presentation layer.
I don't know if this is the right pattern for good java project and until now
I am not succeeded in splitting them :p.

Can anyone show me how and give me some code examples for above code for each
project?

Thanks in advance.

OHSuria.
aciangAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mayank SAssociate Director - Product EngineeringCommented:
Well, that's asking for a lot of source code :) maybe you can go through some of the concepts and online sample code, and then come up with something on your own first.

Look at the MVC design pattern: http://java.sun.com/blueprints/patterns/MVC.html

Some code available here as well:

http://www.onjava.com/pub/a/onjava/2004/07/07/genericmvc.html
0
aciangAuthor Commented:
sorry for my blur questions :). I am very new with java environment.
For example I try to make a class in my data access layer project:
--------------------------------------------------------------------------------------------
package com.test;

import java.io.Reader;

import com.ibatis.common.resources.Resources;
import com.ibatis.dao.client.DaoManager;
import com.ibatis.dao.client.DaoManagerBuilder;

public class DaoHelper {
      
      String resource = "dao.xml";
      
      public DaoManager getDaoManager() {
            Reader reader = Resources.getResourceAsReader(resource);
            DaoManager daoManager = DaoManagerBuilder.buildDaoManager(reader);
            
            return daoManager;
      }
}
--------------------------------------------------------------------------------------------
above class returns error: Unhandled exception type IOException :)
on this statement: Resources.getResourceAsReader(resource)
what is this error about?

well with above class, I plan to use DaoHelper.getDaoManager in my Business layer.
for example:
--------------------------------------------------------------------------------------------
public class F0001BL {
      
      public List getListSelectF0001() {
            List list;
            
            DaoManager daoManager;

            daoManager = DaoHelper.getDaoManager();
            
            F0001DAO objF0001DAO = (F0001DAO) daoManager.getDao(F0001DAO.class, "sqlmap");
            F0001Example objF0001Example = new F0001Example();
            F0001 objF0001 = new F0001();
            
            list = objF0001DAO.selectByExample(objF0001Example);
            
            return list;
      }
}
--------------------------------------------------------------------------------------------

Hope this will explain a little.
sorry for my awful codes and questions :)

Thank you.

OHSuria
0
aciangAuthor Commented:
I think I have found a little answer on this.
although the codes are awful :).

Data Access Layer:
public class DaoHelper {
      
      String resource = "dao.xml";
      
      DaoManager myDaoManager;

      public DaoHelper(){
            try {
                  Reader reader = Resources.getResourceAsReader(resource);
                  myDaoManager = DaoManagerBuilder.buildDaoManager(reader);
                  
            } catch (Exception e) {
                  // TODO: handle exception
            }
      }
            
      public DaoManager getDao() {
            return myDaoManager;
      }
}
-------------------------------------------------------------------------------------------

On Business Layer:
public class F0001BL {
      
      public F0001BL() {
            
      }
      
      public List getSelectF0001() {
            List list;
            
            DaoManager daoManager;
            DaoHelper daoHelper = new DaoHelper();

            daoManager = daoHelper.getDao();
            
            F0001DAO objF0001DAO = (F0001DAO) daoManager.getDao(F0001DAO.class, "sqlmap");
            F0001Example objF0001Example;
                        
            objF0001Example = new F0001Example();
            
            list = objF0001DAO.selectByExample(objF0001Example);
            
            return list;
      }
}
-------------------------------------------------------------------------------------------

And on presentation layer:
<%
F0001 objF0001 = new F0001();
                  
F0001BL f0001Helper = new F0001BL();

List list = f0001Helper.getSelectF0001();

out.println("Selected " + list.size() + " records.<br><br>");
for (int i=0; i < list.size(); i++) {
      objF0001 = (F0001) list.get(i);
      
      out.println(objF0001.getCountryid() + " - ");
      out.println(objF0001.getCountrynm() + "<br>");
}
%>
-------------------------------------------------------------------------------------------

but on the presentation layer.. it is still use object model F0001 from data access layer.
How can I remove this object model? so there will be no reference between presentation and data access layer?
I need some suggestions from expert about it.

Thanks in advance.

OHSuria.

0
Mayank SAssociate Director - Product EngineeringCommented:
>> Unhandled exception type IOException :)

All checked exceptions need to be handled in Java, you need to either wrap it around a try catch block or declare that your method throws the exception and it should be handled outside.

>> it is still use object model F0001 from data access layer.

It is ok to access the bean F0001 if that is going to contain just some data members with get/ set methods. Because it contains information based on which the presentation will be rendered. However, as long as you do not access the DaoHelper and data access methods in the presentation layer, you should be good
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
aciangAuthor Commented:
Thank you so much for the answer mayankeagle.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.