随便写着玩玩.不要太认真哦^^
主要还涉及了一点JPA方面的知识,如果不知道JPA是什么的--!ZZZZZZZ
第一件事当然是把hibernate需要的jar包都添加到项目中.如果不太清楚的可以参考下面的截图给的jar
接下来是配置hibernate.要说明的是此处我是参考一个台湾朋友的做法.虽然有点麻烦...XD
在resources下添加一个包,然后把hibernate.properties和jdbc.properties放到这个目录下
hibernate.properties
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=false
hibernate.format_sql=false
hibernate.cache.use_query_cache=true
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
hibernate.connection.provider_class=org.hibernate.connection.C3P0ConnectionProvider
hibernate.hbm2ddl.auto=update
jdbc.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/map?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true
jdbc.username=root
jdbc.password=root
# Time to wait for an open connection before timing out
# (in milliseconds)
cpool.checkoutTimeout=5000
# Connection pool size
cpool.minPoolSize=10
cpool.maxPoolSize=25
# How long to keep unused connections around(in seconds)
# Note: MySQL times out idle connections after 8 hours(28,800 seconds)
# so ensure this value is below MySQL idle timeout
cpool.maxIdleTime=7200
# Acquiring new connections is slow, so eagerly retrieve extra connections
# when current pool size is reached
cpool.acquireIncrement=5
cpool.autoCommitOnClose=true
然后添加dataAccessContext-hibernate.xml到spring这个包下面,注意里面的packagesToScan属性,你需要把你的所以实体类所在包都显示的在这个里面写出来,如果多个包,就有多个value
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans default-autowire="byName" >
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${jdbc.driverClassName}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="initialPoolSize" value="${cpool.minPoolSize}"/>
<property name="minPoolSize" value="${cpool.minPoolSize}" />
<property name="maxPoolSize" value="${cpool.maxPoolSize}" />
<property name="acquireIncrement" value="${cpool.acquireIncrement}" />
<property name="maxIdleTime" value="${cpool.maxIdleTime}"/>
</bean>
<!--Hibernate SessionFatory-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan">
<list>
<value>com.yx.map.client.bean</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.cache.provider_class">${hibernate.cache.provider_class}</prop>
<prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>
<prop key="hibernate.connection.provider_class">${hibernate.connection.provider_class}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
</props>
</property>
</bean>
</beans>
接下来修改applicationContext.xml,在其中添加以下内容
<!-- 支持 @Transactional 标记 -->
<tx:annotation-driven />
<!-- 支持 @AspectJ 标记 -->
<aop:aspectj-autoproxy />
<!-- 属性文件读入 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:config/jdbc.properties</value>
<value>classpath*:config/hibernate.properties</value>
</list>
</property>
</bean>
<!-- <aop:config proxy-target-class="true"> <aop:advisor pointcut="execution(*
com.xxxx.server..*ServiceImpl.*(..))" advice-ref="txAdvice"/>
</aop:config> 基本事务定义,使用transactionManager作事务管理,默认get*方法的事务为readonly,其余方法按默认设置.
默认的设置请参考Spring文档事务一章. <tx:advice id="txAdvice"> <tx:attributes> <tx:method
name="get*" read-only="true"/> <tx:method name="find*" read-only="true"/>
<tx:method name="query*" read-only="true"/> <tx:method name="*"/> </tx:attributes>
</tx:advice> -->
<!-- Transaction 設定 -->
<bean id="txProxyTemplate" abstract="true"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="transactionManager" />
<property name="proxyTargetClass" value="true" />
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED, ISOLATION_READ_COMMITTED,
-Exception</prop>
<prop key="update*">PROPAGATION_REQUIRED, ISOLATION_READ_COMMITTED,
-Exception</prop>
<prop key="remove*">PROPAGATION_REQUIRED, ISOLATION_READ_COMMITTED,
-Exception</prop>
<prop key="*">PROPAGATION_SUPPORTS,ISOLATION_READ_COMMITTED,readOnly
</prop>
</props>
</property>
</bean>
<!--Hibernate TransactionManager -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- Dao 設定 -->
<bean id="abstractSpringHibernateDao"
class="com.chinatenet.gxt.park.server.dao.AbstractSpringHibernateDao"
abstract="true">
<property name="transactionManager" ref="transactionManager" />
<property name="sessionFactory" ref="sessionFactory" />
</bean>
然后添加Hibernate的baseManage和baseDao,由于代码比较大,这2个类放在附件里面
注意这个2个类是需要放在项目的server包下面的
到此,基本的hibernate配置就算完成了.接下来,我们测试一下效果怎么样吧.简单的做法是创建一个实体,然后创建这个实体的数据服务
首先创建一个实体类,我这里叫ParkData.请注意,在gwt中,如果需要传至client端的java类是都需要放到client这个包下面的..所以我这里建立的包叫com.yx.map.client.bean,接下来,你需要把这个类实现序列化,以便gwt使用.我这里用的IsSerializable接口,^^,google自己的序列化方式.然后就是一些JPA的注解了...不了解的可以自己查询JPA的相关资料
package com.yx.map.client.bean;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import com.google.gwt.user.client.rpc.IsSerializable;
@Entity
@Table(name="parkdata")
public class ParkData implements IsSerializable{
/**
*
*/
private static final long serialVersionUID = 7995434803827667106L;
@Id
@GeneratedValue
private int id;
@Column
private String parkName;
@Column
private String address;
@Column
private String descs;
@Column
private String allNum;
@Column
private String lastNum;
@Column
private String tel;
@Column
private String updateTime;
@Column
private double lat;
@Column
private double lng;
@Column
private String icon;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getParkName() {
return parkName;
}
public void setParkName(String parkName) {
this.parkName = parkName;
}
public String getDescs() {
return descs;
}
public void setDescs(String descs) {
this.descs = descs;
}
public String getAllNum() {
return allNum;
}
public void setAllNum(String allNum) {
this.allNum = allNum;
}
public String getLastNum() {
return lastNum;
}
public void setLastNum(String lastNum) {
this.lastNum = lastNum;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getUpdateTime() {
return updateTime;
}
public void setUpdateTime(String updateTime) {
this.updateTime = updateTime;
}
public double getLat() {
return lat;
}
public void setLat(double lat) {
this.lat = lat;
}
public double getLng() {
return lng;
}
public void setLng(double lng) {
this.lng = lng;
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
@Override
public String toString() {
return getParkName();
}
}
接下来就是创建ParkData所对应的dao和manager.具体做法见代码.注意dao上面的org.springframework.stereotype.Repository注解.使用此注解后,spring会生成一个此类的bean,bean的名称就是类名,但是首字母是小写的.然后就是这个manager,你可以更具你的需求.把一些业务代码放到这个manager里面.我这里就不写了~
ParkDataDao
package com.yx.map.server.dao.mapdao;
import org.springframework.stereotype.Repository;
import com.yx.map.client.bean.ParkData;
import com.yx.map.server.dao.AbstractSpringHibernateDao;
@Repository
public class ParkDataDao<E> extends
AbstractSpringHibernateDao<E> {
@Override
protected Class<ParkData> getClazz() {
return ParkData.class;
}
}
ParkDataManager
package com.yx.map.server.dao.mapdao;
import com.yx.map.client.bean.ParkData;
import com.yx.map.server.dao.BaseManager;
public class ParkDataManager extends BaseManager<ParkData> {
}
在创建对应实体所对应的dao和manager时,一定要注意范型的使用...
接下来,需要让spring来管理这个manager,所以打开applicationContext.xml,在里面添加以下代码
<!--Manager 設定 -->
<bean id="parkDataManager" parent="txProxyTemplate" lazy-init="true">
<property name="target">
<bean class="com.yx.map.server.dao.mapdao.ParkDataManager"
lazy-init="true">
<property name="dao" ref="parkDataDao" />
</bean>
</property>
</bean>
好了,准备工作都就绪了,这个时候就可以在RPC服务中调用这些dao层的代码了.
首先打开GreetingServiceImpl,再里面添加一个manager,然后生成此manager对应的set方法并给予@Autowired注释以便spring注入.
private ParkDataManager parkDataManager;
@Autowired
public void setParkDataManager(ParkDataManager parkDataManager) {
this.parkDataManager = parkDataManager;
}
然后在greetServer(String input)中修改return中的一些代码
ParkData data = parkDataManager.findById(1);
if(null==data)
return "Hello, " + input + "!<br>你还没有添加数据进来呢<br>I am running " + serverInfo
+ ".<br><br>It looks like you are using:<br>" + userAgent;
else
return "Hello, " + input + "!<br>欢迎光临"+data.toString()+"<br>I am running " + serverInfo
+ ".<br><br>It looks like you are using:<br>" + userAgent;
对了,在此之前,你需要在数据库中先创建一个名字为map的数据库(这个名字根据你自己的配置文件中的数据库名字而定).表就不建了,JPA会给我们创建的.
并且运行一下吧,不出意外,在数据库中就会生成一张表出来,打开他,添加一些测试数据进去吧.然后在页面中输入文字,点击发送看看效果吧.
过会再发个ext-gwt(gxt)如何整合 google map 第三版.