1.copy hibernate.java和spring.java包物理文件到本机
2.新建一个java web project,然后右击该工程,选择MyEclipse,选择添加spring capabilities.配置文件比如名为applicationContext.xml,放到wwwroot下的web-inf下(如果使用class加载,不使用Tomcat,则要将该xml文件放入SRC文件,其它的xml配置文件也是如此).然后添加一个package,取名(因为SessionFactory类不能添加在缺省包里面).
3.右击该工程,选择MyEclipse,选择添加hibernate capabilities.使用和2一样的配置文件,指定SessionFactoryID,指明数据库联接项
在3中配置BeanID,即是数据库的连接字符串,生成的项目在配置文件中如下:
<bean id="fidsDatasource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:@192.168.1.199:1521:fidstj</value>
</property>
<property name="username">
<value>fidsadmin</value>
</property>
<property name="password">
<value>fidsadmin</value>
</property>
<property name="maxActive">
<value>100</value>
</property>
<property name="maxWait">
<value>10000</value>
</property>
<property name="maxIdle">
<value>40</value>
</property>
<property name="minIdle">
<value>20</value>
</property>
</bean>
还需要建立session,见例子文件
同时在src文件夹下建立hibernateSessionFactory类
4.在web-inf下的lib里面拷贝如下文件:
commons-collections-3.1.jar
commons-dbcp-1.2.1.jar
commons-pool-1.2.jar
hibernate3.jar
jconn3.jar
spring.jar
ojdbc14.jar---oracle10g
然后在工程名处,右键单击refresh
否则容易出现类找不到的情况.
如果仍然找不到,点击project-->clean,然后在工程名处,右键单击refresh.可解决此类问题.
5.web-inf
如果生成web工程的话,需要配置web.xml 用来加载 applicationcontext.xml---参考fids程序--需要配置TomCat
如果不使用web工程,applicationcontext.xml的加载需要使用class---参考广播程序
该类的语句如下
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
*
*
* 访问Spring Bean的工具类
*/
public class BeanLocator {
private static final String[] CONFIG_SET = { "applicationContext.xml" };//可以放入多个xml配置文件
/**
* Spring Bean工厂引用
*/
private static BeanFactory factory = null;
static {
factory = new ClassPathXmlApplicationContext(CONFIG_SET);
}
/**
* 查找name对应的bean
*
* @param name
* bean名称
* @return 对应的bean引用
*/
public static Object getBean(String name) {
return factory.getBean(name);
}
}
6.如果想实现执行结果,则建立一个类比如名为test,里面放入main方法,
然后在方法中放入语句
BeanLocator.getBean("****");
****指代DAO的ID,该ID在applicationContext.xml中有定义
如DeviceDAO,
<bean id="DeviceDAO" class="com.abcd.webmoitor.model.DeviceDAO">
<property name="sessionFactory">
<ref bean="MonitorSessionFactory" />
</property>
7.applicationContext.xml文件中
<property name="mappingResources">
<list>
<value>com/abcd/webmoitor/model/Device.hbm.xml</value></list>
</property>
value节点中可以加入一个包名,里面放入所有的配置文件,即所有数据库表的pojo文件对应的映射文件hbm.xml,这样在操作多个表时,只需要在applicationContext.xml文件中加入一行指明包名即可.
8.在DAO中放入数据库HQL语句
写一个接口,封装业务逻辑,见com.abcd.fidsconsole.service.devmanage中的IDeviceManager
然后实现这个接口,见 com.abcd.fidsconsole.service.impl.devmanage中的DeviceManagerImpl
9.在表的映射文件*.hbm.xml文件中需要注意,该文件必须和这个表的pojo文件(即表的所有字段和get/set方法的封装类,即message表对应的message.java文件)放在一个包内,如果不放在一个包内,需要配置hbm.xml文件,指明pojo文件的包名
10.业务逻辑--->DAO---->pojo文件---->数据库表
web.xml(或者BeanLocator文件)--->applicationContext.xml--->*.hbm.xml
有关applicationContext.xml的内部bean的调用方式如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans><!--
通过DeviceDAO找到MonitorSessionFactory,然后找到MonitorDevice,从而建立数据库连接
--><bean id="MonitorDevice"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>com.sybase.jdbc2.jdbc.SybDriver</value>
</property>
<property name="url">
<value>jdbc:sybase:Tds:192.168.0.60:5000/AODB?charset=eucgb&jconnect_version=4</value>
</property>
<property name="username">
<value>fidsadmin</value>
</property>
<property name="password">
<value>fidsadmin</value>
</property>
<property name="maxActive">
<value>100</value>
</property>
<property name="maxWait">
<value>10000</value>
</property>
<property name="maxIdle">
<value>40</value>
</property>
<property name="minIdle">
<value>20</value>
</property>
</bean>
<bean id="MonitorSessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="MonitorDevice" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.SybaseDialect
</prop>
</props>
</property><!--
对所有的数据库配置文件指明包名即可
--><property name="mappingResources">
<list>
<value>com/abcd/webmoitor/model/Device.hbm.xml</value></list>
</property></bean>
<bean id="DeviceDAO" class="com.abcd.webmoitor.dao.DeviceDAO">
<property name="sessionFactory">
<ref bean="MonitorSessionFactory" />
</property>
</bean>
<bean id="FlightDAO" class="FlightDAO">
<property name="sessionFactory">
<ref bean="MonitorSessionFactory" />
</property>
</bean></beans>
11.hbm.xml的定义样例:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.abcd.cabis.common.domain.BroadcastEvent" table="C8_BROADCASTEVENT">
//主键使用ID标签,sequence定义自增长特性
<id name="eventid" type="java.lang.Long">
<column name="EVENTID" precision="22" scale="0" />
<generator class="sequence">
<param name="sequence">C8_BROADCASTEVENT_SEQ</param>
</generator>
</id>
//非主键使用property标签
<property name="operationDate" type="java.util.Date">
<column name="OPERATION_DATE" length="7" not-null="true" />
</property>
<property name="flightNo" type="java.lang.String">
<column name="FLIGHT_NO" length="10" not-null="true" />
</property>
<property name="AORD" type="java.lang.String">
<column name="A_OR_D" length="2" not-null="true" />
</property>
<property name="DORI" type="java.lang.String">
<column name="D_OR_I" length="1" not-null="true" />
</property>
<property name="typeid" type="java.lang.String">
<column name="TYPEID" length="4" not-null="true" />
</property>
<property name="gate1" type="java.lang.String">
<column name="Gate1" length="5" />
</property>
<property name="gate2" type="java.lang.String">
<column name="Gate2" length="5" />
</property>
<property name="gate3" type="java.lang.String">
<column name="Gate3" length="5" />
</property>
</class>
</hibernate-mapping>
12.对于hibernate的DAO的注入方式(一次加注多个DAO类,实现对多个表的数据操作)
参看CabisControlProject中的
cabis_audio_bc.xml
<bean id="autoBcEngine" class="com.abcd.cabis.control.service.auto.AutoBroadcastEngine" lazy-init="false">
<property name="flightDao">
<ref bean="FlightDAO" />
</property>
<property name="systemInfo">
<ref bean="systemInfoCache" />
</property>
<property name="languageGroup">
<ref bean="languagesCache" />
</property>
<property name="audioNodeFactory">
<ref bean="audioNodeFactory" />
</property>
<property name="queueDao">
<ref bean="BroadcastQueueDAO" />
</property>
...
多个DAO通过调用autoBcEngine一次注入,实现灵活定制DAO,将来可以任意添加和删除某个DAO(对某个表的增删查改操作)
见包com.abcd.cabis.control.service.auto;
13.type指的是hibernate中的类型,不是java中的String类型,必须小写,同样如integer也是要小写
eg:
我的映射文件中是这样写的:
<property
name= "operatorName "
type= "java.lang.String "
column= "operator_name "
length= "10 "
>
</property>
数据库是sybase的,对应数据类型为:varchar(10)。
启动后出现上面的错误,请问是什么原因?谢谢。
org.hibernate.MappingException: Could not determine type for: java.lang.string, for columns: [org.hibernate.mapping.Column(...
14.如果出现"Dialect class not found "错误,说明数据库方言配置错误,或者类没有找到.
打开hibernate.jar包(在eclipse中即可打开),找到org.hibernate.dialect包,看一下相关数据库方言类库的名字,然后放入applicationContext.xml中的相应节点的值
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.OracleDialect
</prop>
</props>
</property>
15.oracle用户权限:resource/dba/connect,用来建立表和表空间,在代码中使用这个用户,而非超级用户.
16.多个表的DAO调用,参考
com.abcd.cabis.common.dao中的,是对一个表的封装
CabisCommonProject中的cabis_dao.xml
和CabisControlProject中的
com.abcd.cabis.control.service.auto中的autobroadcastEngine.java做为总的调用和封装
cabis_auto_bc.xml
///
在applicationContext.xml中添加一块:
<bean id="M3110Impl"
class="com.abcd.monitor.engine.dao.hibernate.M3110Impl">
<property name="sessionFactory">
<ref bean="myDBSource" />
</property>
</bean>
<bean id="M3100Impl"
class="com.abcd.monitor.engine.dao.hibernate.M3100Impl">
<property name="sessionFactory">
<ref bean="myDBSource" />
</property>
</bean>
<bean id="M3111Impl"
class="com.abcd.monitor.engine.dao.hibernate.M3111Impl">
<property name="sessionFactory">
<ref bean="myDBSource" />
</property>
</bean>
<bean id="M3101Impl"
class="com.abcd.monitor.engine.dao.hibernate.M3101Impl">
<property name="sessionFactory">
<ref bean="myDBSource" />
</property>
</bean>
<bean id="M3112Impl"
class="com.abcd.monitor.engine.dao.hibernate.M3112Impl">
<property name="sessionFactory">
<ref bean="myDBSource" />
</property>
</bean>
<bean id="Engine" class="com.abcd.monitor.engine.service.Engine" lazy-init="false">
<property name="m3110impl"> //engine文件中的变量名
<ref bean="M3110Impl" /> /engine文件中的类名
</property>
<property name="m3100impl">
<ref bean="M3100Impl" />
</property>
<property name="m3111impl">
<ref bean="M3111Impl" />
</property>
<property name="m3101impl">
<ref bean="M3101Impl" />
</property>
<property name="m3112impl">
<ref bean="M3112Impl" />
</property>
</bean>
添加实现类,每个表都要做实现
例如 在model块中实现每个表的DAO.
engine模块的代码:
public class Engine{// implements InitializingBean, DisposableBean {
//private boolean isDestroy = false;
private M3110Impl m3110impl;
private M3100Impl m3100impl;
private M3101Impl m3101impl;
private M3111Impl m3111impl;
private M3112Impl m3112impl;
public M3110Impl getM3110impl() {
return m3110impl;
}
public void setM3110impl(M3110Impl m3110impl) {
this.m3110impl = m3110impl;
}
public M3100Impl getM3100impl() {
return m3100impl;
}
public void setM3100impl(M3100Impl m3100impl) {
this.m3100impl = m3100impl;
}
public M3101Impl getM3101impl() {
return m3101impl;
}
public void setM3101impl(M3101Impl m3101impl) {
this.m3101impl = m3101impl;
}
public M3111Impl getM3111impl() {
return m3111impl;
}
public void setM3111impl(M3111Impl m3111impl) {
this.m3111impl = m3111impl;
}
public M3112Impl getM3112impl() {
return m3112impl;
}
public void setM3112impl(M3112Impl m3112impl) {
this.m3112impl = m3112impl;
}
}
17.如果一个表没有主键和外键,则在定义其hbm.xml文件时,class不能按照常规方式定义,很麻烦.我目前是强行定义了主键,算是回避了xml的配置问题.
18.java严格遵循大小写,包括hbm.xml的文件名,在applicationContext.xml中用value指明时,都要和工程中的文件名大小写对应.