Solved

Sample Codes for Three Layers Java Project with Abator and iBatis

Posted on 2007-04-06
5
1,112 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
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java Loop 6 59
stackato and cloud 4 85
Apps blocked by Java 9 79
login form jsp example 2 24
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

813 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now