Spring2.5整合Hibernate3.0中使用XML以tx来配置事务管理。

来源:互联网 发布:ubuntu安装emacs25 编辑:程序博客网 时间:2024/06/05 23:39

这是一个小白的自我记录。

首先要将项目搭建起来,把jar包导入到项目,用到的jar包去网上自行下载。下载完可能还会有少包的现象,那么就要根据测试项目时的错误提示来找需要的jar包。看错误提示并能理解和解决错误提示也是一种修行方式。

这个项目我用的基本上是最老的方式和配置方法,用注解的方式更简便一些,一步一步来,稳扎稳打。

1.目录结构


2.把实体类、Server和DAO成构建好。

LogDaoImpl

package com.lxp.spring.dao;import java.text.SimpleDateFormat;import java.util.Date;import org.hibernate.Session;import org.hibernate.SessionFactory;import com.lxp.spring.model.User;import com.lxp.spring.model.UserLog;/** * 用户日志表的DAO * @author Administrator * */public class LogDaoImpl implements LogDao {private SessionFactory sessionFactory;public SessionFactory getSessionFactory() {return sessionFactory;}public void setSessionFactory(SessionFactory sessionFactory) {this.sessionFactory = sessionFactory;}public void addLog(User u){//这里把时间和和用户姓名一并插入到日志表中Date data = new Date();SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");String dateString = formatter.format(data);String logInfo = "Create time:" + dateString + ",Create user:" + u.getUsername();UserLog ul = new UserLog();ul.setId(8);ul.setLogInfo(logInfo);// 这里需要注意下,使用事务管理后,要用getCurrentSession(), Session s = sessionFactory.getCurrentSession();s.save(ul);}}


UserDaoImpl

package com.lxp.spring.dao;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import com.lxp.spring.model.User;/** * 用户表 * @author Administrator *  */public class UserDaoImpl implements UserDao {private SessionFactory sessionFactory;public SessionFactory getSessionFactory() {return sessionFactory;}public void setSessionFactory(SessionFactory sessionFactory) {this.sessionFactory = sessionFactory;}//添加用户public void save(User u) {Transaction tx;try {// 这里需要注意下,使用事务管理后,要用getCurrentSession(), // 使用openSession()无法插入数据,至于原因,自行百度。Session s = sessionFactory.getCurrentSession();s.save(u);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
实体类就不给出代码了,把Hibernate的配置放出来吧,

User.hbm.xml

<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping package="com.lxp.spring.model">    <class name="User" table="user">    <!-- name属性中对应的是类中的属性名 column对应的是数据库的字段名     id是主键    --><id name="id" type="int">            <column name="id" />            <generator class="increment" />        </id>        <property name="username" column="username"/>        <property name="password" column="password"/>    </class></hibernate-mapping>
UserLog.hbm.xml

<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping package="com.lxp.spring.model">    <class name="UserLog" table="user_log">    <!-- name属性中对应的是类中的属性名 column对应的是数据库的字段名     id是主键    --><id name="id" column="id"/>                 <property name="logInfo" column="loginfo"/>    </class></hibernate-mapping>

UserService

package com.lxp.spring.service;import com.lxp.spring.dao.LogDao;import com.lxp.spring.dao.UserDao;import com.lxp.spring.model.User;public class UserService {// UserDaoImpl 这个在Spring.xml的配置中存在private UserDao userDao;// LogDaoImplprivate LogDao logDao;public LogDao getLogDao() {return logDao;}public void setLogDao(LogDao logDao) {this.logDao = logDao;}public UserDao getUserDao() {return userDao;}public void setUserDao(UserDao userDao) {this.userDao = userDao;}public void addUser(User u) {// 添加用户userDao.save(u);// 添加loglogDao.addLog(u);}public void destroy() {System.out.println("end destory();");}}


hibernate.cfg.xml


关于<property name="current_session_context_class">thread</property>不能出现的原因,可以去参考这个博客
运行项目前,要把表都建立好,如果你在配置中使用了<property name="hbm2ddl.auto">update</property>,最好在User.hbm.xml和UserLog.hbm.xml中将自动的长度也给出来。


<?xml version='1.0' encoding='utf-8'?><!DOCTYPE hibernate-configuration PUBLIC        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration>    <session-factory>        <!-- JDBC connection pool (use the built-in) 连接池 -->        <property name="connection.pool_size">1</property>        <!-- SQL dialect 方言  最后翻译成每个不同的数据库认识的语言   从文档中查询mysql就可以找到-->        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>              <!-- 切记这个标签一点不要出现,至于原因可以去网上查,上面会有连接        <property name="current_session_context_class">thread</property>        -->               <!-- Echo all executed SQL to stdout         show_sql 是显示sql语句        format_sql 是把sql语句格式化成更好看的样式这俩个兄弟是需要连用的,只有显示出来SQL才能够被格式化。        -->        <property name="show_sql">true</property>        <property name="format_sql">true</property>        <!-- Drop and re-create the database schema on startup  要不要hibernate自动的生成建表语句        <property name="hbm2ddl.auto">update</property>         --><mapping resource="com/lxp/spring/model/User.hbm.xml"/><mapping resource="com/lxp/spring/model/UserLog.hbm.xml"/>     </session-factory></hibernate-configuration>

spring.xml
头部的xmlns和下面的http很重要,别弄少了,缺少的可以从这个头中复制粘贴过去即可。
关于事务管理配置有许多,可以去网上查询,去做实验,实践是检验标准的唯一真理。


<?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:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"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    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"default-autowire="byName" default-lazy-init="true"><!-- 配置数据源 --><beanclass="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="locations"><value>classpath:jdbc.properties</value></property></bean><bean id="dataSource"class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close"><property name="driverClassName"value="${jdbc.driverClassName}"></property><property name="url" value="${jdbc.url}"></property><property name="username" value="${jdbc.username}"></property><property name="password" value="${jdbc.password}"></property></bean><!-- 整合Hibernate --><bean id="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><property name="dataSource" ref="dataSource"></property><property name="configLocation">            <value>classpath:hibernate.cfg.xml</value>        </property></bean>               <!-- IOC --><bean id="userDao" class="com.lxp.spring.dao.UserDaoImpl"> <property name="sessionFactory" ref="sessionFactory"></property></bean><bean id="logDao" class="com.lxp.spring.dao.LogDaoImpl"> <property name="sessionFactory" ref="sessionFactory"></property></bean><bean id="userService" class="com.lxp.spring.service.UserService"><property name="userDao" ref="userDao"></property><property name="logDao" ref="logDao"></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>            <!-- name是方法名,这个配置就是说方法名前缀为add的方法都进行事务管理  --><tx:method name="add*" rollback-for="Exception"/> </tx:attributes> </tx:advice>  <!-- aop配置,expression中要配置到你的要加事务管理的类。 --><aop:config> <aop:pointcut id="serviceMethod" expression="execution(* com.lxp.spring.service.*.*(..))"/> <aop:advisor pointcut-ref="serviceMethod" advice-ref="txAdvice"/> </aop:config></beans>


3.测试


测试中使用的是JUnit。 我这里在用户日志表中的主键设置为不是自动增长的,所以ID需要我们手动插入,当插入相同ID的时候会产生异常,产生异常的时候事务管理就会将UserServer的add方法回滚。 在Spring.xml中我将事务管理配置到UserService的add方法。 这个add方法中有两条插入动作,当其中一条插入动作发生错误时,add方法会回滚,虽然其中有一条插入语句虽然是能够插入的,但还是会跟着方法一起回滚。当你用户日志表的ID设置为不同时,插入成功,这样证明了事务管理配置成功,保证了事务的原子性。


package com.lxp.spring.test;import org.junit.Before;import org.junit.Test;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.lxp.spring.model.User;import com.lxp.spring.service.UserService;public class UserTest {private ClassPathXmlApplicationContext beanFactory = null;@Beforepublic void before() {beanFactory = new ClassPathXmlApplicationContext("spring.xml");}@Testpublic void testAdd() {UserService userService = (UserService) beanFactory.getBean("userService");User u = new User();u.setUsername("username");u.setPassword("password");userService.addUser(u);beanFactory.destroy();}}




原创粉丝点击