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 閱讀(558) 評論(0)  編輯  收藏 所屬分類: Ajax 、個人學習日記 、HibernateGWT
          主站蜘蛛池模板: 普陀区| 宁蒗| 巨野县| 鲁甸县| 安宁市| 云南省| 贡觉县| 巴里| 新民市| 登封市| 淮南市| 周宁县| 白沙| 堆龙德庆县| 博野县| 化隆| 礼泉县| 安陆市| 长子县| 西盟| 吴忠市| 凤冈县| 铁岭市| 鹤山市| 张北县| 京山县| 安平县| 章丘市| 保亭| 湖口县| 陆川县| 唐山市| 新干县| 汝阳县| 娄底市| 民县| 苏州市| 龙山县| 禹城市| 贵港市| 柘城县|