锘??xml version="1.0" encoding="utf-8" standalone="yes"?> package client; import com.google.gwt.user.client.rpc.ServiceDefTarget; public interface CRUDService extends RemoteService { String getPagenum(); public static class App { public static synchronized CRUDServiceAsync getInstance() { package server; import com.google.gwt.user.server.rpc.RemoteServiceServlet; public class CRUDServiceImpl extends RemoteServiceServlet implements CRUDService { public static SessionFactory getSessionFactory() { public List ListStudent(String PageNum){ public int CountStudent(){ public Student[] getStudent(String PageNum){ return student; public void setPagenum(String pagenum){ public String getPagenum() { } package client; import com.google.gwt.user.client.rpc.AsyncCallback; public interface CRUDServiceAsync { void getStudent(String PageNum, AsyncCallback async); void getPagenum(AsyncCallback async); package client; import com.google.gwt.core.client.EntryPoint; public class CRUD implements EntryPoint { public void setPagenum(String pagenum){ public void onModuleLoad() { prepage.addClickListener(new ClickListener(){ nextpage.addClickListener(new ClickListener(){ private void showstudentlist(String pagenum) { CRUDService.App.getInstance().getPagenum(new AsyncCallback(){ } }
CRUDService.java錛?br />
import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.core.client.GWT;
Student[] getStudent(String PageNum) ;
private static CRUDServiceAsync ourInstance = null;
if (ourInstance == null) {
ourInstance = (CRUDServiceAsync) GWT.create(CRUDService.class);
((ServiceDefTarget) ourInstance).setServiceEntryPoint(GWT.getModuleBaseURL() + "CRUD/CRUDService");
}
return ourInstance;
}
}
}
CRUDServiceImpl.java錛?br />
import client.CRUDService;
import client.Student;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.cfg.Configuration;
import java.util.List;
import java.util.Iterator;
private static final SessionFactory sessionFactory;
String Pagenum = "1";
static {
try {
// Create the SessionFactory from hibernate.cfg.xml
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
return sessionFactory;
}
Session session = getSessionFactory().getCurrentSession() ;
session.beginTransaction();
Query query = session.createSQLQuery("select * from t_student")
.addScalar("id", Hibernate.LONG)
.addScalar("name", Hibernate.STRING)
.addScalar("email", Hibernate.STRING);
int PageSize = 10;
try{
if (Integer.parseInt(PageNum)!=0 | PageNum!=null ){
query.setFirstResult((Integer.parseInt(PageNum)-1) * PageSize);
query.setMaxResults(PageSize);
}else{
query.setFirstResult(0);
query.setMaxResults(PageSize);
}
}catch(Exception e){
query.setFirstResult(0);
query.setMaxResults(PageSize);
}
List ls = query.list();
session.getTransaction().commit();
return ls;
}
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());
}
this.setPagenum(PageNum);
Student[] student = new Student[this.CountStudent()];
int i = 0;
for(Iterator it = this.ListStudent(PageNum).iterator();it.hasNext();i++) {
Object[] ob = (Object[] )it.next();
student[i]=new Student(ob[0].toString(),ob[1].toString(),ob[2].toString());
}
}
this.Pagenum = pagenum;
}
return Pagenum; //To change body of implemented methods use File | Settings | File Templates.
}
CRUDServiceAsync.java :
}
import com.google.gwt.user.client.ui.*;
import com.google.gwt.user.client.rpc.AsyncCallback;
private CRUDServiceAsync crudServiceAsync ;
VerticalPanel main = new VerticalPanel();
FlexTable lb = new FlexTable();
HorizontalPanel hp = new HorizontalPanel();
Button nextpage = new Button("nextpage");
Button prepage = new Button("prepage");
private String pagenum = "1";
this.pagenum=pagenum;
}
public String getPagenum(){
return this.pagenum;
}
int prepagenum = 1;
int nextpagenum =1;
main.add(lb);
main.add(hp);
hp.add(prepage);
hp.add(nextpage);
RootPanel.get().add(main);
showstudentlist("1");
public void onClick (Widget sender){
prepagenum = Integer.parseInt(getPagenum())-1;
showstudentlist(String.valueOf(prepagenum));
}
});
public void onClick (Widget sender){
nextpagenum = Integer.parseInt(getPagenum())+1 ;
showstudentlist(String.valueOf(nextpagenum));
}
});
}
CRUDService.App.getInstance().getStudent(pagenum,new AsyncCallback(){
public void onFailure(Throwable caught) {
}
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);
}
}
});
public void onFailure(Throwable caught) {}
public void onSuccess(Object result) {
setPagenum((String)result);
}
});
]]>
<module>
<inherits name='com.google.gwt.user.User'/>
<entry-point class='client.CRUD'/>
<servlet path="/CRUD/CRUDService" class="server.CRUDServiceImpl"/>
</module>
EntryPoint錛欳RUD.java錛屼嬌鐢╒erticalPanel 鏉ユ樉紺篖ist錛?/strong>
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錛歋tudent.java錛?/strong>
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錛欳RUDService.java錛?/strong>
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錛?/strong>CRUDServiceImpl.java錛岃繖閲屼嬌鐢ㄧ洿鎺ヨ繛鎺ibernate鐨勬柟娉曠敤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;
}
}
寮傛璋冪敤綾籆RUDServiceAsync.java錛?/strong>
package client;
import com.google.gwt.user.client.rpc.AsyncCallback;
public interface CRUDServiceAsync {
void getStudent(AsyncCallback async);
}
鏈鍚庯紝鍦╯rc鐩綍涓嬪垱寤篽ibernate.cfg.xml錛岃繖閲屼嬌鐢╩ysql錛?/strong>
<?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>