spring结合hibernate示例详解
|
单纯Hibernate程序 1、首先是导入hibernate的jar包。 2、 建立用户和用户操作记录实体,Log.Java和User.java。代码如下所示。 Log.java
import java.util.Date;
public class Log {
private int id;
//日志的类别.日志一般起到一个不可否认性.
//操作日志 安全日志 事件日志.
private String type;
private String detail;
private Date time;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
public Date getTime() {
return time;
}
public void setTime(Date time) {
this.time = time;
}
}
User.java
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;
}
}
3、 并建立与之对应的实体配置文件,Log.hbm.xml和Use.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.bjpowernode.usermgr.domain.User" table="t_user">
<id name="id" >
<generator class="native"/>
</id>
<property name="name" />
</class>
</hibernate-mapping>
Log.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.bjpowernode.usermgr.domain.Log" table="t_log">
<id name="id" >
<generator class="native"/>
</id>
<property name="type" />
<property name="detail" />
<property name="time" />
</class>
</hibernate-mapping>
4、Manager层代码如下所示。 LogManager.java接口
public interface LogManager {
//添加日志.方法
public void addLog(Log log);
}
LogManagerImpl实现
public class LogManagerImpl implements LogManager {
@Override
public void addLog(Log log) {
HibernateUtils.getSessionFactory().getCurrentSession().save(log);
}
}
UserManager接口
public interface UserManager {
public void addUser(User user);
}
UserManagerImpl.java实现
public class UserManagerImpl implements UserManager {
@Override
public void addUser(User user) {
Session session = null;
try {
//这个session中是放到threadlocal.
session = HibernateUtils.getSessionFactory().getCurrentSession();
session.beginTransaction();
// 网用户表中添加一条同时网日志中添加一条.
session.save(user);
Log log = new Log();
log.setType("操作日志");
log.setTime(new Date());
log.setDetail("xxx");
LogManager logManager = new LogManagerImpl();
//添加日志.
logManager.addLog(log);
session.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally{
HibernateUtils.closeSession(session);
}
}
}
5、是通过sessionFactory来创建session,通过session来开启提交和关闭回滚事务,我们把session的开启关闭封装到一个工具类中。HibernateUtils.java代码如下所示。
public class HibernateUtils {
private static SessionFactory factory;
static {
try {
//读取hibernate.cfg.xml文件
Configuration cfg = new Configuration().configure();
//建立SessionFactory
factory = cfg.buildSessionFactory();
}catch(Exception e) {
e.printStackTrace();
}
}
public static Session getSession() {
return factory.openSession();
}
public static void closeSession(Session session) {
if (session != null) {
if (session.isOpen()) {
session.close();
}
}
}
public static SessionFactory getSessionFactory() {
return factory;
}
}
6、配置hibernate.cfg.xml文件,包括数据库名称,数据库关联的表,以及用户名密码等。代码如下所示。
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/spring_hibernate_1</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.current_session_context_class">thread</property>
<!--
<property name="hibernate.current_session_context_class">jta</property>
-->
<mapping resource="com/bjpowernode/usermgr/domain/User.hbm.xml"/>
<mapping resource="com/bjpowernode/usermgr/domain/Log.hbm.xml"/>
</session-factory>
</hibernate-configuration>
7、 使用junit进行单元测试,代码如下所示。
import junit.framework.TestCase;
public class UserManagerImplTest extends TestCase {
public void testAddUser() {
UserManager userManager = new UserManagerImpl();
User user = new User();
user.setName("张三");
userManager.addUser(user);
}
}
在上述操作用,对事物的控制边界在业务逻辑层,因为在UserManagerImpl中我们调用addUser()这一方法的同时要把这一操作写入到日志中,也就是调用了addLog()方法,而对于类的方法,一方法一session,一session一事务,那么如果控制事务的呢?我们要执行开启addUser()事务同时再开启addLog()事务吗?在这里我们没有再用以前的openSession方法,选择用的 那OpenSession和getCurrentSession的区别呢? 1、 openSession必须关闭,currentSession在事务结束后自动关闭。 2、 openSession没有和当前线程绑定,currentSession和当前线程绑定。并且使用currentSession需要在我们的hibernate.cfg.xml文件中进行事务的配置,是使用Jdbc事务还是JTA事务。 hibernate和spring结合使用 我们从上述例子中发现,hibernate的事务是独立于hibernate对数据库的增删改查的,并且事务控制在我们的业务逻辑层,对于独立的东西,像是横切性问题,自然想到了AOP,实际上SpringAOP封装了对事务的管理,使用SpringAOP我们不再负责去开启和关闭事务。 下面用SpringAOP来和hibernate结合。 当然也要导入Spring相关jar。 对于事务管理就是一个横切性问题,把事务管理模块化就是我们的aspect,然后再配置文件中进行配置,我们可以把事务单独放到一个配置文件中。 1、代码如下,applicationContext-common.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/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">
<!-- 配置SessionFactoyr -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<!-- 哪些类哪些方法使用事务. -->
<aop:config>
<aop:pointcut id="allManagerMethod" expression="execution(* com.bjpowernode.usermgr.manager.*.*(..))"/>
<aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice"/>
</aop:config>
<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="*" propagation="REQUIRED" read-only="true"/>
</tx:attributes>
</tx:advice>
</beans>
(编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
