4.3. 添加 Hibernate 功能支持
操作:[右击项目] MyEclipse/Project Capabilities/Add Hibernate Capabilities
Hibernate 版本(Hibernate Specification)选择 "Hibernate 3.2" ,开发包(libraries)选择 Hibernate 3.2 Annotations & Entity Manager、Hibernate 3.2 Core Libraries两项(默认的);
JAR Library Installation 选择 "copy checked..." ,"Library Folder" 选择 "/WebRoot/WEB-INF/lib" 。
点击 "下一步(Next)" 设置配置文件,选择 "Spring configuration file (applicationContext.xml)" 。
"下一步(Next)" 设置 Spring-Hibernate,选择 "Existing Spring configuration file" ,"SessionFactory ID" 项输入 "sessionFactory" (MyEclipse8.0默认已经写好了)。
"下一步(Next)" 创建数据源对象,在 Bean Id 中输入 dataSource,"DataSource" 项选择 "Use JDBC Dirver" ,DB Driver 项选择 "sql2000" ,其余项会自动填充;
记得选中 "Copy DB driver jar(s) to project and add to buidpath" 项,以便将数据连接的库文件复制到项目,方便以后的布署。
"下一步(Next)" 不创建 SessionFactory 类,把”Create SessionFactory class?”选项的状态改为不选中。
点击 "完成(Finish)"。此时会自动建立hibernate.HibernateSessionFactory.java类
applicationContext.xml文件内会根据先前的配置自动添加dataSource和sessionFactory节点
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean name="/login" class="com.zlk.struts.action.LoginAction"></bean>
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.microsoft.jdbc.sqlserver.SQLServerDriver">
</property>
<property name="url"
value="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=test">
</property>
<property name="username" value="sa"></property>
<property name="password" value="sa"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.SQLServerDialect
</prop>
</props>
</property>
</bean></beans>
此时如果报错: Class ‘org.apache.commons.dbcp.BasicDataSource’ not found,是因为没有导入依赖包commons-dbcp.jar,导入此包即可