Solved

Sample Codes for Three Layers Java Project with Abator and iBatis

Posted on 2007-04-06
5
1,130 Views
Last Modified: 2013-11-24
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.
0
Comment
Question by:aciang
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 30

Expert Comment

by:Mayank S
ID: 18864048
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
 

Author Comment

by:aciang
ID: 18864522
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
 

Author Comment

by:aciang
ID: 18864857
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
 
LVL 30

Accepted Solution

by:
Mayank S earned 125 total points
ID: 18866176
>> 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
 

Author Comment

by:aciang
ID: 18869654
Thank you so much for the answer mayankeagle.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question