Spring的Hibernate事务-XML


一、拷贝必要的jar包到工程的lib目录

二、创建spring的配置文件并导入约束

<?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.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
</beans>

三、准备数据库表和实体类

创建数据库:
create database spring;
use spring;
创建表:
create table account(
    id int primary key auto_increment,
    name varchar(40),
    money float
)character set utf8 collate utf8_general_ci;

package com.yiidian.domain;import java.io.Serializable;
/*** @author http://www.yiidian.com* */
public class Account implements Serializable{private Integer id;private String name;private Float money;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Float getMoney() {return money;}public void setMoney(Float money) {this.money = money;}@Overridepublic String toString() {return "Account [id=" + id + ", name=" + name + ", money=" + money+ "]";}}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- http://www.yiidian.com-->
<hibernate-mapping><class name="com.yiidian.domain.Account" table="account"><id name="id" column="id"><generator class="native"></generator></id><property name="name" column="name"></property><property name="money" column="money"></property></class>
</hibernate-mapping>

四、编写业务层接口和实现类

AccountService:

package com.yiidian.service;import com.yiidian.domain.Account;
/*** @author http://www.yiidian.com* */
public interface AccountService {/*** 根据id查询账户信息* @param id* @return*/Account findAccountById(Integer id);//查/*** 转账* @param sourceName  转出账户名称* @param targeName       转入账户名称* @param money           转账金额*/void transfer(String sourceName,String targeName,Float money);//增删改
}

AccountServiceImpl:

package com.yiidian.service.impl;import com.yiidian.dao.AccountDao;
import com.yiidian.domain.Account;
import com.yiidian.service.AccountService;
/*** @author http://www.yiidian.com* */
public class AccountServiceImpl implements AccountService {private AccountDao accountDao;public void setAccountDao(AccountDao accountDao) {this.accountDao = accountDao;}@Overridepublic Account findAccountById(Integer id) {return accountDao.findAccountById(id);}@Overridepublic void transfer(String sourceName, String targeName, Float money) {// 1.根据名称查询两个账户Account source = accountDao.findAccountByName(sourceName);Account target = accountDao.findAccountByName(targeName);// 2.修改两个账户的金额source.setMoney(source.getMoney() - money);// 转出账户减钱target.setMoney(target.getMoney() + money);// 转入账户加钱// 3.更新两个账户accountDao.updateAccount(source);//模拟异常int i = 1 / 0;accountDao.updateAccount(target);}
}

五、编写Dao接口和实现类

AccountDao:

package com.yiidian.dao;import com.yiidian.domain.Account;/*** * @author http://www.yiidian.com* */
public interface AccountDao {/*** 根据id查询账户信息* @param id* @return*/Account findAccountById(Integer id);/*** 根据名称查询账户信息* @return*/Account findAccountByName(String name);/*** 更新账户信息* @param account*/void updateAccount(Account account);
}

AccountDaoImpl:

package com.yiidian.dao.impl;import org.springframework.orm.hibernate5.support.HibernateDaoSupport;import com.yiidian.dao.AccountDao;
import com.yiidian.domain.Account;
/*** * @author http://www.yiidian.com* */
public class AccountDaoImpl extends HibernateDaoSupport  implements AccountDao{@Overridepublic Account findAccountById(Integer id) {return this.getHibernateTemplate().get(Account.class, id);}@Overridepublic Account findAccountByName(String name) {return (Account) this.getHibernateTemplate().find("from Account where name = ?", name).get(0);}@Overridepublic void updateAccount(Account account) {this.getHibernateTemplate().update(account);}
}

六、配置业务层和持久层,加上Spring事务管理

<?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.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"><bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="location" value="classpath:jdbc.properties"/></bean> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="${jdbc.driverClass}"/><property name="url" value="${jdbc.url}"/><property name="username" value="${jdbc.username}"/><property name="password" value="${jdbc.password}"/></bean><bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"><property name="dataSource" ref="dataSource"></property><property name="hibernateProperties"><props><prop key="hibernate.show_sql">true</prop><prop key="hibernate.format_sql">true</prop><prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop></props></property><property name="mappingResources"><list><value>com/yiidian/domain/Account.hbm.xml</value></list></property></bean>  <bean id="accountDao" class="com.yiidian.dao.impl.AccountDaoImpl"><property name="sessionFactory" ref="sessionFactory"/></bean><bean id="accountService" class="com.yiidian.service.impl.AccountServiceImpl"><property name="accountDao" ref="accountDao"/></bean><!-- 配置一个Hibernate事务管理器 --><bean id="transactionManager"class="org.springframework.orm.hibernate5.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory"></property></bean><!-- 事务的配置 --><tx:advice id="txAdvice" transaction-manager="transactionManager"><!--在tx:advice标签内部 配置事务的属性 --><tx:attributes><!-- 指定方法名称:是业务核心方法 read-only:是否是只读事务。默认false,不只读。 isolation:指定事务的隔离级别。默认值是使用数据库的默认隔离级别。 propagation:指定事务的传播行为。 timeout:指定超时时间。默认值为:-1。永不超时。 rollback-for:用于指定一个异常,当执行产生该异常时,事务回滚。产生其他异常,事务不回滚。没有默认值,任何异常都回滚。 no-rollback-for:用于指定一个异常,当产生该异常时,事务不回滚,产生其他异常时,事务回滚。没有默认值,任何异常都回滚。 --><tx:method name="*" read-only="false" propagation="REQUIRED" /><tx:method name="find*" read-only="true" propagation="SUPPORTS" /></tx:attributes></tx:advice><!-- 配置aop --><aop:config><!-- 配置切入点表达式 --><aop:pointcut expression="execution(* com.yiidian.service.impl.*.*(..))"id="pt1" /><aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"/></aop:config></beans>

七、编写测试类

package com.yiidian.test;import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import com.yiidian.service.AccountService;
/*** @author http://www.yiidian.com**/
public class HibernateTemplateDemo {@Testpublic void test1() {ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");AccountService accountService = (AccountService)ac.getBean("accountService");accountService.transfer("小苍", "小泽", 300f);}
}

源码下载:http://pan.baidu.com/s/1qYiIiCk

Spring4.x(9)--Spring的Hibernate事务-XML相关推荐

  1. 在eclipse中关于Spring和Hibernate 的XML配置如何提示类的包路径的办法

    转载自  在eclipse中关于Spring和Hibernate 的XML配置如何提示类的包路径的办法 我们在配Spring 或者Hibernate 配置文件的时候,发觉在配置类路径的时候,在双引号下 ...

  2. (转载)spring配置hibernate 事务。

    Spring和Hibernate整合后,通过Hibernate API进行数据库操作时发现每次都要opensession,close,beginTransaction,commit,这些都是重复的工作 ...

  3. spring整合hibernate事务编程中错误分析

    2019独角兽企业重金招聘Python工程师标准>>> 在spring整合hibernate过程中,我们的配置文件: <?xml version="1.0" ...

  4. spring struts hibernate web.xml配置文件模板

    1.applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <beans ...

  5. ssh(Spring+Spring mvc+hibernate)——applicationContext.xml

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  6. java app的强制更新吗_java – Spring JPA / Hibernate事务强制插入而不是更新

    编辑.虽然扩展基础存储库类并添加插入方法可以使更优雅的解决方案似乎在实体中实现Persistable.见可能的解决方案2 我正在使用springframework.data.jpa创建一个服务,使用H ...

  7. Spring对Hibernate事务管理

    http://www.cnblogs.com/m-xy/archive/2013/05/14/3077627.html(挺好的) 还有一种用parent的配置方式,parent配置到哪个层面,事务就控 ...

  8. spring 中 Hibernate 事务和JDBC事务嵌套问题

    http://www.iteye.com/topic/11063?page=2 ---mixed ORM and JDBC usage is a feature of Spring DAO 这是Rod ...

  9. 写Struts2、Spring、Hibernate的xml配置文件时无提示

    为什么80%的码农都做不了架构师?>>>    导入本地的dtd和xsd约束文件: Eclipse--Window--preferences--XML--XML Catalog 右边 ...

最新文章

  1. Celery简介及Docker测试环境搭建
  2. 2vec需要归一化吗_LTSM模型预测数据如何归一化?(知乎回答)
  3. [转] getBoundingClientRect判断元素是否可见
  4. 查找包含指定关键字的BDOC
  5. Java开发笔记(一百零三)线程间的通信方式
  6. Jupyter Lab——无法显示matplotlib绘制的图像
  7. tornado的views(templates)
  8. 兼容IE8遇到的问题
  9. 获取目录-Winform
  10. C++ printf输出
  11. 常用网站网址、名称、logo列表
  12. 直击固定资产管理痛点,让企业轻松管理海量固定资产
  13. 移动端设计的基础尺寸单位与转化
  14. 公司电子邮箱可以定制邮箱地址吗?
  15. vim 多窗口切换和其他的一些快捷方法
  16. 来自星星的宝贝,我要如何发现你?
  17. BAT机器学习面试1000题系列(详细版)
  18. 联网下载jar包导入本地Maven库
  19. response响应,常用方法,分发器重定向,错误提示
  20. 促销活动表结果的学习探讨

热门文章

  1. 关于DG32f103C8T6 不启动的问题-调试可以运行自启动不行
  2. 小程序 房租水电费记录管理_移民局小程序:中国出入境记录的官方查询利器...
  3. Hash魔法:分布式哈希算法
  4. [C++] - C++11 多线程 - Condition Variable
  5. Mybatisplus插件
  6. vue环境搭建以及vue-cli使用
  7. Selenium with Python 006 - 操作浏览器
  8. Android 百度地图开发问题----解决地图有时候加载不出来问题
  9. Big Event in HDU
  10. 修改xampp中phpmyadmin用户管理