搭建hibernate工程:
1. 解压hibernate-3.2.0.ga.zip
2.拷贝
hibernate-3.2.0.ga\hibernate-3.2\hibernate3.jar+hibernate-3.2.0.ga\hibernate-3.2\lib(此目录下所有jar包)+数据库链接驱动到一个新建目录下。
3.取ide中新建一个userlibraries 指定到刚才新建目录下的所有jar文件。
4.拷贝hibernate-3.2.0.ga\hibernate-3.2\etc\hibernate.cfg.xml到工程src目录下。
5.打开hibernate-3.2.0.ga\hibernate-3.2\etc\hibernate.properties来配置hibernate.cfg.xml(根据需要链接数据库进行配置)
Eg:
<hibernate-configuration>
<session-factory >
<property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.username">userorcl</property>
<property name="hibernate.connection.password">pass123</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@172.18.2.91:1521:userorcl</property>
</session-factory>
</hibernate-configuration>
6.拷贝hibernate-3.2.0.ga\hibernate-3.2\eg\org\hibernate\auction\目录下:
User.hbm.xml+User.java到自定义包中。改为自定义名称,并修改内容。
实体代码:
publicclass Person {
private String pno;
private String name;
private String address;
privateintage
}
Person.hbm.xml(对象关系映射文件)代码:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping >
<class name="com.etc.entity.Person" table="t_hibernate_person">
<id name="pno"><!-- 表示定义主键 -->
<generator class="uuid"/><!-- UUID生成策略是hibernate自带一个主键生成策略32,64,128位唯一字符序列 -->
</id>
<property name="name"/><!-- 普通属性 -->
<property name="address"/>
<property name="age"/>
</class>
</hibernate-mapping>
7.将映射文件的路径加入到hibernate.cfg.xml中
Eg:
<mappingresource="com/etc/entity/Person.hbm.xml"/>
8.引入spring的jar包
spring-framework-2.0-with-dependencies\dist\spring.jar+
spring-framework-2.0-with-dependencies\lib\jakarta-commons\commons-logging.jar+
spring-framework-2.0-with-dependencies\lib\log4j\log4j-1.2.14.jar+
aspectjweaver.jar+ cglib-nodep-2.2.jar+aspectjrt.jar
到工程中
9. 拷贝spring-framework-2.0-with-dependencies\samples\jpetstore\war\WEB-INF\applicationContext.xml文件到工程src目录下(并且修改)。
Eg:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="" class="">
</bean>
</beans>
配置applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.0.xsd"
>
<!-- 配置session工厂 -->
<bean id="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transcationmanager"class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置事务传播特征 -->
<tx:advice id="txadvice"transaction-manager="transcationmanager">
<tx:attributes>
<tx:method name="add*"propagation="REQUIRED"/>
<tx:method name="dele*"propagation="REQUIRED"/>
<tx:method name="update*"propagation="REQUIRED"/>
<tx:method name="*"read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 配置那些方法会参与事务 -->
<aop:config>
<aop:pointcut id="myaop" expression="execution(*com.etc.service.impl.*.add*(..))"/>
<aop:advisor pointcut-ref="myaop" advice-ref="txadvice"/>
</aop:config>
<bean id="myuserdao" class="com.etc.dao.impl.UserDaoImpl">
<property name="sessionFactory"ref="sessionFactory"></property>
</bean>
<bean id="myservice" class="com.etc.service.impl.UserServiceImpl">
<property name="userdao"ref="myuserdao"></property>
</bean>
</beans>
Dao层代码(必须HibernateDaoSupport类需要sessionFactory注入进去)
publicclass UserDaoImpl extends HibernateDaoSupport implements IUserDao{
@Override
publicvoid adduser(Person p) {
Session session = this.getSession();
session.save(p);
}
}