CRUD.gwt.xml:

<module>

    <inherits name='com.google.gwt.user.User'/>

    <entry-point class='client.CRUD'/>

    <servlet path="/CRUD/CRUDService" class="server.CRUDServiceImpl"/>
</module>

EntryPoint:CRUD.java,使用VerticalPanel 来显示List:

package client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class CRUD implements EntryPoint {
    VerticalPanel main = new VerticalPanel();
    FlexTable lb = new FlexTable();
    public void onModuleLoad() {
        main.add(lb);
        RootPanel.get().add(main);
              CRUDService.App.getInstance().getStudent(new AsyncCallback(){

                  public void onFailure(Throwable caught) {
                      //To change body of implemented methods use File | Settings | File Templates.
                  }

                  public void onSuccess(Object result) {
                    Student s[] = ( Student[])result ;
                      for (int i=0;i<=s.length;i++){
                     lb.setText(i,0,s[i].id);
                          lb.setText(i,1,s[i].name);
                          lb.setText(i,2,s[i].email);                          
                      }
                  }
              }) ;
    }
}

ENTITY:Student.java:

package client;

import com.google.gwt.user.client.rpc.IsSerializable;

public class Student implements IsSerializable {
    public String id,name,email;
    public Student(){
       
    }
    public Student(String id,String name,String email) {
    this.id=id;
    this.name=name;
    this.email=email;   
    }
}

SERVICE:CRUDService.java:

package client;

import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.core.client.GWT;

public interface CRUDService extends RemoteService {

     Student[] getStudent()     ;

    public static class App {
        private static CRUDServiceAsync ourInstance = null;

        public static synchronized CRUDServiceAsync getInstance() {
            if (ourInstance == null) {
                ourInstance = (CRUDServiceAsync) GWT.create(CRUDService.class);
                ((ServiceDefTarget) ourInstance).setServiceEntryPoint(GWT.getModuleBaseURL() + "CRUD/CRUDService");
            }
            return ourInstance;
        }
    }
}


SERVICEImpl:CRUDServiceImpl.java,这里使用直接连接hibernate的方法用native sql查询数据,不需要专门创建实体类和配置文件:

package server;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import client.CRUDService;
import client.Student;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Hibernate;
import org.hibernate.cfg.Configuration;
import java.util.List;
import java.util.Iterator;

public class CRUDServiceImpl extends RemoteServiceServlet  implements CRUDService {
    private static final SessionFactory sessionFactory;      
    static {
            try {
                sessionFactory = new Configuration().configure().buildSessionFactory();
            } catch (Throwable ex) {
                System.err.println("Initial SessionFactory creation failed." + ex);
                throw new ExceptionInInitializerError(ex);
            }
        }

        public static SessionFactory getSessionFactory() {
            return sessionFactory;
        }

    public List ListStudent(){
       Session session =  getSessionFactory().getCurrentSession() ;
        session.beginTransaction();
         List ls = session.createSQLQuery("select * from t_student")
        .addScalar("id", Hibernate.LONG)
        .addScalar("name", Hibernate.STRING)
        .addScalar("email", Hibernate.STRING).list();
        session.getTransaction().commit();
        return ls;
    }

    public int CountStudent(){
       Session session =  getSessionFactory().getCurrentSession() ;
        session.beginTransaction();
         List ls = session.createSQLQuery("select count(*) from t_student").list();
        session.getTransaction().commit();
        return Integer.parseInt(ls.iterator().next().toString());
    }

        public Student[] getStudent(){
             Student[] student = new Student[this.CountStudent()];
            int i = 0;
          for(Iterator it = this.ListStudent().iterator();it.hasNext();i++) {
             Object[] ob = (Object[] )it.next();
                student[i]=new Student(ob[0].toString(),ob[1].toString(),ob[2].toString());
          }
             return student;
          }

}


异步调用类CRUDServiceAsync.java:

package client;

import com.google.gwt.user.client.rpc.AsyncCallback;

public interface CRUDServiceAsync {
    void getStudent(AsyncCallback async);
}

最后,在src目录下创建hibernate.cfg.xml,这里使用mysql:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
  "-//Hibernate/Hibernate Configuration DTD//EN"
  "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
<property name="connection.driver_class">
  com.mysql.jdbc.Driver
 </property>
 <property name="connection.url">
  jdbc:mysql://localhost:3306/mysql
 </property>
 <property name="connection.username">root</property>
 <property name="connection.password">root</property>

 <!-- JDBC connection pool (use the built-in) -->
 <property name="connection.pool_size">1</property>

 <!-- SQL dialect -->
 <property name="dialect">
  org.hibernate.dialect.MySQLDialect
 </property>

 <!-- Enable Hibernate's automatic session context management -->
 <property name="current_session_context_class">thread</property>

 <!-- Disable the second-level cache  -->
 <property name="cache.provider_class">
  org.hibernate.cache.NoCacheProvider
 </property>

 <!-- Echo all executed SQL to stdout -->
 <property name="show_sql">true</property>

 <!-- Drop and re-create the database schema on startup -->
 <property name="myeclipse.connection.profile">mysql for j</property>
  </session-factory>
</hibernate-configuration>

posted on 2008-11-06 07:28 lzj520 阅读(554) 评论(0)  编辑  收藏 所属分类: Ajax个人学习日记HibernateGWT

只有注册用户登录后才能发表评论。


网站导航: