Solved

Sample Codes for Three Layers Java Project with Abator and iBatis

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

911 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

23 Experts available now in Live!

Get 1:1 Help Now