1、创建一个java项目,并加入Spring的依赖库。
[quote]
* SPRING_HOME/dist/spring.jar
* SPRING_HOME/lib/jakarta-commons/commons-logging.jar
* SPRING_HOME/lib/log4j/log4j-1.2.15.jar
* SPRING_HOME/lib/aspectj/*.jar
* SPRING_HOME/lib/cglib/*.jar
[/quote]
2、加入Hibernate的依赖库
[quote]
* Hibernate_HOME/hibernate3.jar
* Hibernate_HOME/lib/*.jar
[/quote]
3、编写实体类
4、配置Hibernate映射文件
5、配置Hibernate数据源文件,并将映射文件添加到此文件
6、执行以下代码,将数据库表反向映射到数据库中
7、编写数据层操作接口
8、编写数据层具体操作
9、编写事物配置文件
10、把数据库操作注入到Hibernate
11、编写客户端调用
12、了解事务的几种传播特性
1.PROPAGATION_REQUIRED: 如果存在一个事务,则支持当前事务。如果没有事务则开启
2.PROPAGATION_SUPPORTS: 如果存在一个事务,支持当前事务。如果没有事务,则非事务的执行
3.PROPAGATION_MANDATORY: 如果已经存在一个事务,支持当前事务。如果没有一个活动的事务,则抛出异常。
4.PROPAGATION_REQUIRES_NEW: 总是开启一个新的事务。如果一个事务已经存在,则将这个存在的事务挂起。
5.PROPAGATION_NOT_SUPPORTED: 总是非事务地执行,并挂起任何存在的事务。
6.PROPAGATION_NEVER: 总是非事务地执行,如果存在一个活动事务,则抛出异常
7.PROPAGATION_NESTED:如果一个活动的事务存在,则运行在一个嵌套的事务中. 如果没有活动事务,则按TransactionDefinition.PROPAGATION_REQUIRED 属性执行
[quote]
* SPRING_HOME/dist/spring.jar
* SPRING_HOME/lib/jakarta-commons/commons-logging.jar
* SPRING_HOME/lib/log4j/log4j-1.2.15.jar
* SPRING_HOME/lib/aspectj/*.jar
* SPRING_HOME/lib/cglib/*.jar
[/quote]
2、加入Hibernate的依赖库
[quote]
* Hibernate_HOME/hibernate3.jar
* Hibernate_HOME/lib/*.jar
[/quote]
3、编写实体类
package com.yx.zzg.model;
public class User {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
4、配置Hibernate映射文件
<?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 package="com.yx.zzg.model">
<class name="User" table="t_user">
<id name="id">
<generator class="native"/>
</id>
<property name="name"/>
</class>
</hibernate-mapping>
5、配置Hibernate数据源文件,并将映射文件添加到此文件
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.url">
jdbc:mysql://localhost/spring_hibernate_1
</property>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</property>
<property name="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.current_session_context_class">
thread
</property>
<!--
<property name="hibernate.current_session_context_class">jta</property>
-->
<mapping resource="com/yx/zzg/model/User.hbm.xml" />
</session-factory>
</hibernate-configuration>
6、执行以下代码,将数据库表反向映射到数据库中
package com.yx.zzg.util;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class ExportDB {
public static void main(String[] args) {
//读取hibernate.cfg.xml文件
Configuration cfg = new Configuration().configure();
SchemaExport export = new SchemaExport(cfg);
export.create(true, true);
}
}
7、编写数据层操作接口
package com.yx.zzg.dao.bo;
import com.yx.zzg.model.User;
public interface UserDao {
public void addUser(User user);
}
8、编写数据层具体操作
package com.yx.zzg.dao.impl;
import java.util.Date;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.yx.zzg.dao.bo.LogDao;
import com.yx.zzg.dao.bo.UserDao;
import com.yx.zzg.model.User;
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
public void addUser(User user) {
this.getHibernateTemplate().save(user);
}
}
9、编写事物配置文件
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://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">
<!-- 配置sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:config/hibernate.cfg.xml</value>
</property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<!-- 配置事务的传播特性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="modify*" propagation="REQUIRED"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 那些类的哪些方法参与事务 -->
<aop:config>
<aop:pointcut id="allManagerMethod" expression="execution(* com.yx.zzg.dao.bo.*.*(..))"/>
<aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice"/>
</aop:config>
</beans>
10、把数据库操作注入到Hibernate
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://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="userDao" class="com.yx.zzg.dao.impl.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
</beans>
11、编写客户端调用
package com.yx.zzg.client;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.yx.zzg.dao.bo.UserDao;
import com.yx.zzg.model.User;
public class Client {
public static void main(String[] args) {
User user = new User();
user.setName("张三");
BeanFactory factory = new ClassPathXmlApplicationContext(
"/config/applicationContext-*.xml");
UserDao userDao = (UserDao) factory.getBean("userDao");
userDao.addUser(user);
}
}
12、了解事务的几种传播特性
1.PROPAGATION_REQUIRED: 如果存在一个事务,则支持当前事务。如果没有事务则开启
2.PROPAGATION_SUPPORTS: 如果存在一个事务,支持当前事务。如果没有事务,则非事务的执行
3.PROPAGATION_MANDATORY: 如果已经存在一个事务,支持当前事务。如果没有一个活动的事务,则抛出异常。
4.PROPAGATION_REQUIRES_NEW: 总是开启一个新的事务。如果一个事务已经存在,则将这个存在的事务挂起。
5.PROPAGATION_NOT_SUPPORTED: 总是非事务地执行,并挂起任何存在的事务。
6.PROPAGATION_NEVER: 总是非事务地执行,如果存在一个活动事务,则抛出异常
7.PROPAGATION_NESTED:如果一个活动的事务存在,则运行在一个嵌套的事务中. 如果没有活动事务,则按TransactionDefinition.PROPAGATION_REQUIRED 属性执行