2. 加入 Hibernate 0)安装 hibernate 插件·安装方法说明(hibernatetools-4.1.1.Final):注:注意版本要与eclipse版本一致 –Help --> Install New Software... –Click Add... –In dialog Add Site dialog, click Archive... –Navigate to jbosstools-4.2.3.Final_2015-03-26_23-05-30-B264-updatesite-hibernatetools.zip and click Open –Clicking OK in the Add Site dialog will bring you back to the dialog 'Install' –Select the Jboss Tools hibernatetools Nightly Build Update Site that has appeared –Click Next and then Finish –Approve the license Restart eclipse when that is asked1). 同时建立持久化类, 和其对应的 .hbm.xml 文件, 生成对应的数据表 2). Spring 整合 Hibernate 3). 步骤: ①. 加入 jar 包 E:\工具\编程开发\SSH\hibernate-release-4.2.4.Final\hibernate-release-4.2.4.Final\lib\requiredantlr-2.7.7.jar dom4j-1.6.1.jar hibernate-commons-annotations-4.0.2.Final.jar hibernate-core-4.2.4.Final.jar hibernate-jpa-2.0-api-1.0.1.Final.jar javassist-3.15.0-GA.jar jboss-logging-3.1.0.GA.jar jboss-transaction-api_1.1_spec-1.0.1.Final.jar ②. 在类路径下加入 hibernate.cfg.xml 文件, 在其中配置 hibernate 的基本属性 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate./hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- 配置hibernate的基本属性 --> <!-- 方言 --> <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> <!-- 是否显示及格式化SQL --> <property name="hibernate.show_sql">true</property> <property name="hibernate.format_sql">true</property> <!-- 生成数据表的策略 --> <property name="hibernate.hbm2ddl.auto">update</property> <!-- 二级缓存相关 --> </session-factory> </hibernate-configuration> ③. 建立持久化类, 和其对应的 .hbm.xml 文件 新建Employee.java和Department.java生成相应的.hbm.xml ④. 和 Spring 进行整合 i. 加入 c3p0 和 MySQL 的驱动 c3p0,加jar包,E:\编程开发\SSH 相关软件\jar包\c3p0-0.9.1.2.bin\c3p0-0.9.1.2\libc3p0-0.9.1.2.jar MySQL ,加jar包,E:\编程开发\SSH 相关软件\jar包\mysql-connector-java-5.1.7\mysql-connector-java-5.1.7mysql-connector-java-5.1.7-bin.jar ii. 在 Spring 的配置文件中配置: 数据源, SessionFactory, 声明式事务 /SSH/config/applicationContext.xml<!-- 导入资源文件 --> <context:property-placeholder location="classpath:db.properties"/> <!-- 配置 C3P0 数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="user" value="${jdbc.user}"></property> <property name="password" value="${jdbc.password}"></property> <property name="driverClass" value="${jdbc.driverClass}"></property> <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property> <property name="initialPoolSize" value="${jdbc.initPoolSize}"></property> <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property> </bean> <!-- 配置 SessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> <property name="mappingLocations" value="classpath:com/liumw/ssh/entities/*.hbm.xml"></property> </bean> <!-- 配置 Spring 的声明式事务 --> <!-- 1. 配置 hibernate 的事务管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 2. 配置事务属性 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="get*" read-only="true"/> <tx:method name="lastNameIsValid" read-only="true"/> <tx:method name="*"/> </tx:attributes> </tx:advice> <!-- 3. 配置事务切入点, 再把事务属性和事务切入点关联起来 --> <aop:config> <aop:pointcut expression="execution(* com.liumw.ssh.service.*.*(..))" id="txPointcut"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/> </aop:config> ⑤. 启动项目, 会看到生成对应的数据表 |
|