- 浏览: 194670 次
- 性别:
- 来自: 深圳
文章分类
- 全部博客 (291)
- ERP (3)
- JSP/Servlet (13)
- DB (8)
- MongoDB (2)
- Python (3)
- Maven (8)
- GIT (1)
- Windows (8)
- Java (24)
- Informatica (2)
- PHP (1)
- Javascript (25)
- Tomcat (2)
- spring (13)
- HTML5 (11)
- Nginx (2)
- NodeJS (6)
- Linux (40)
- AngularJS (1)
- Android (3)
- Selenium (3)
- 理财 (6)
- 工作心得 (9)
- SQLServer (10)
- Hibernate/JPA (6)
- Bootstrap (1)
- C# (3)
- MySql (4)
- highchart (1)
- hadoop (5)
- ZooKeeper (2)
- Hbase (8)
- Avro (2)
- Hive (2)
- Flume (5)
- Kafka (11)
- Sqoop (3)
- Pig (1)
- Spark (1)
- Storm (2)
- Redis (1)
- Memcached (1)
- Dubbo (2)
- Phoenix (2)
最新评论
-
一尾金鱼:
可以作为查询手册了,页面布局也好~
JSP EL -
darkgost:
您好,我按照你的方法,在Service1.cs中添加如下代码: ...
C#Windows 服务制作安装删除. 用户注销后,程序继续运行
1. 添加lib
2. 修改spring配置
3.添加AccountDao
4.添加RowMapper
添加Service
测试
注意:
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>3.2.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>3.2.6.RELEASE</version> </dependency>
2. 修改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:context="http://www.springframework.org/schema/context" 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-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"> <!-- scan all beans and inject dependence --> <context:component-scan base-package="com.myproject"/> <!-- add aop support --> <aop:aspectj-autoproxy/> <!-- read from database.properties --> <context:property-placeholder location="classpath:database.properties"/> <bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${driverClassName}"></property> <property name="url" value="${url}"/> <property name="username" value="root"/> <property name="password" value="admin"/> <!-- 连接池启动时的初始值 --> <property name="initialSize" value="${initialSize}"/> <!-- 连接池的最大值 --> <property name="maxActive" value="${maxActive}"/> <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 --> <property name="maxIdle" value="${maxIdle}"/> <!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 --> <property name="minIdle" value="${minIdle}"/> </bean> <!-- add Transaction support --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="datasource"/> </bean> <!-- add Transaction support in annotation format --> <tx:annotation-driven transaction-manager="txManager"/> </beans>
3.添加AccountDao
import java.sql.Types; import javax.annotation.Resource; import javax.sql.DataSource; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository; @Repository public class AccountDaoBean implements AccountDao{ private JdbcTemplate jdbcTemplate; @Resource public void setDataSource(DataSource datasource){ this.jdbcTemplate=new JdbcTemplate(datasource); } @Override public int insertAccount(Account account) { String sql="insert into account(username, password) values(?, ?)"; return this.jdbcTemplate.update(sql, new Object[]{account.getUsername(), account.getPassword()}, new int[]{java.sql.Types.VARCHAR, java.sql.Types.VARCHAR}); } @Override public Account queryAccountById(int id) { String sql="select * from account where id =?"; return this.jdbcTemplate.queryForObject(sql, new Object[]{id}, new int[]{java.sql.Types.INTEGER}, new AccountMapper()); } @Override public List<Account> queryAccount(Account account) { String sql="select * from account where 1=1 "; if(account.getUsername()!=null) sql=sql+"and username=? "; if(account.getPassword()!=null) sql=sql+"and password=? "; return this.jdbcTemplate.query(sql, new Object[]{account.getUsername(), account.getPassword()}, new int[]{java.sql.Types.VARCHAR, java.sql.Types.VARCHAR}, new AccountMapper()); } @Override public int updateAccount(Account account) { String sql="update account set username=?, password=? where id=?"; return this.jdbcTemplate.update(sql, new Object[]{account.getUsername(), account.getPassword(), account.getId()}, new int[]{Types.VARCHAR, Types.VARCHAR, Types.INTEGER}); } @Override public int deleteAccount(int id) { String sql="delete from account where id=?"; return this.jdbcTemplate.update(sql, new Object[]{id}, new int[]{Types.INTEGER}); } }
4.添加RowMapper
import org.springframework.jdbc.core.RowMapper; public class AccountMapper implements RowMapper<Account>{ @Override public Account mapRow(ResultSet rs, int index) throws SQLException { Account account = new Account(); account.setId(rs.getInt("id")); account.setUsername("username"); account.setPassword("password"); return account; } }
添加Service
import javax.annotation.Resource; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; @Service @Transactional public class AccountService { @Resource private AccountDao accountDao; //don't need to add transaction in query method @Transactional(propagation=Propagation.NOT_SUPPORTED) public Account Login(String username, String password){ System.out.println(username + " want to login."); Account account = new Account(); account.setUsername(username); account.setPassword(password); List<Account> list= accountDao.queryAccount(account); if(list.size()==1) return list.get(0); else return null; } //in method which already has "throws", we must add rollbackFor if want to rollback for this exception, //otherwise the method will not be rollback @Transactional(rollbackFor=Exception.class) public void reqister(Account account) { accountDao.insertAccount(account); throw new RuntimeException("==no register"); } }
测试
@Test public void test(){ try { ApplicationContext ac=new ClassPathXmlApplicationContext("spring.xml"); AccountService accountService =(AccountService)ac.getBean("accountService"); Account account = new Account(); account.setUsername("tom"); account.setPassword("116"); accountService.reqister(account); } catch (Exception e) { e.printStackTrace(); } }
注意:
- 1. 用@Transactional修饰的class默认情况下会对每个method加上独立的事务,当一个method被另一个method调用时,事务会合并。
- 2. 默认情况下方法内抛出runtime异常,会rollback。 但如果方法定义中有throws *Exception, 不会对抛出的*Exception回滚; 如果需要回滚*Exception,需要对方法加上注释@Transactional(rollbackFor=*Exception.class)。
- 3. 原理同#2, 我们也可以使用@Transactional(noRollbackFor=*Exception.class)指定不回滚某种异常。
- 4. 在#1中指出@Transactional修饰的class默认情况下会对每个method加上独立的事务,对于查询方法我们可以使用注释@Transactional(propagation=Propagation.NOT_SUPPORTED)申明该方法不需要事务管理。 propagation 还有其他属性(见附件):
发表评论
-
获取spring 上下文的几种方法
2015-04-06 15:28 807获取spring 上下文的几种方法: 1. 通过配置 ... -
spring + jpa + hibernate
2014-06-11 20:48 9751. 添加依赖Jar文件。除了必要的spring和Hibern ... -
Spring 集成到web项目中
2014-06-10 20:17 4781. 在web.xml中配置实例化spring容器 & ... -
Spring + hibernate + ehcache
2014-06-10 18:02 557<?xml version="1.0" ... -
Spring XML配置事务管理
2014-06-10 16:55 791以下是XML配置事务管理的核心代码,其它配置参考 http:/ ... -
Spring 事务传播属性
2014-06-10 16:29 469一、Propagation (事务的传 ... -
Spring AOP XML配置实现
2014-06-05 21:06 494<?xml version="1.0" ... -
Spring AOP 注解实现
2014-06-03 19:25 506使用aspectj注解实现的AOP需要引入aspectj的li ... -
Spring 依赖注入
2014-05-30 09:47 508Spring 处理使用XML配置文件进行依赖注入外, 还可以使 ... -
spring2 bean作用域 和 生命周期
2014-05-17 19:14 438一 作用域 1. singleton spring中bean的 ... -
Spring1 Bean实例化
2014-05-17 18:53 3431. Bean实例化的方法 a. 普通方法 public cl ... -
spring 3.0 应用springmvc 构造RESTful URL 详细讲解
2014-04-30 23:10 530博文转载: http://www.blogjava.net/b ...
相关推荐
在spring+hibernate的框架中定时操作数据库,主要是拿到sessionFactory,不会出现no session 和transaction no-bound等问题,由sessionFactory完成对数据的操作,有些包是没有用的,有兴趣的可以自己删除掉
29. //不用此包,在启动时招聘:java.lang.NoClassDefFoundError: javax/transaction/TransactionManager 30. jta.jar 31. 32. //Mysql JDBC驱动 33. mysql-connector.jar 34. 35. //Hibernate必须使用,注意...
commons-collections-3.2.1 commons-dbcp-1.1 commons-logging-1.1.3 commons-pool-1.6 mybatis-3.2.0-SNAPSHOT mybatis-spring-1.1.1 mysql-connector-java-5.1.27 ...org.springframework.transaction-3.1.1.RELEASE
mysql,jdbc,Hibernate,tomcat [mysql+jdbc+jsp+Hibernate3.2+tomcattomcat5.028成功测试-简单] <br> 1 .数据库设计:库mydb 表events <br>EVENT_ID BIGINT(20) NOT NULL AUTOINC EVENT_DATE DATETIME...
SSI(Struts2 + Spring + IBatis)框架集成。
commons-collections-3.2.1 commons-dbcp-1.1 commons-...3.1.1.RELEASE org.springframework.expression-3.1.1.RELEASE org.springframework.jdbc-3.1.1.RELEASE org.springframework.transaction-3.1.1.RELEASE
介绍Hibernate+Transaction+myeclipse6+事例的应用参考工程源码.
class="org.springframework.transaction.interceptor.TransactionInterceptor"> <!-- 事务拦截器bean需要依赖注入一个事务管理器 --> <!-- 下面定义事务传播属性 [ bus* 事务的方法名]--> *">...
spring-jdbc-3.2.0.RC2.jar spring-orm-3.2.0.RC2.jar spring-tx-3.2.0.RC2.jar spring-web-3.2.0.RC2.jar struts2-core-2.3.7.jar struts2-spring-plugin-2.3.4.1.jar xwork-core-2.3.7.jar 总计为43个jar包。可以...
JOTM (Java Open Transaction Manager)是由ObjectWeb协会开发的功能完整的且资源开放的独立的事务管理器。 它提供了 JAVA 应用程序的事务支持,而且与 JTA( JAVA 事务 API)兼容。您可以在JOTM home page 了解到更...
org.springframework.transaction.CannotCreateTransactionException: Could not open JDBC Connection for transaction; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot get a ...
整合mybatis,实现事务控制
commons-collections-3.2.2.jar commons-logging-1.2.jar ...org.springframework.transaction-3.2.2.RELEASE.jar quartz-all-1.8.6.jar slf4j-api-1.5.8.jar slf4j-log4j12.jar 和spring的一些jar包 不一一列出了
SpringMVC+SpringJDBC+SpringTransaction的jar包,打包好了,直接可以用
该软件依赖包,为实例spring aop实现Hinernate事务操作软件包
oracle data server internal
spring-transaction.jar.zip
里面为一个演示spring事务传播机制的小demo。简单实现转账功能,通过添加注解调整传播级别,同时通过日志打印查看sql是否执行,在mysql中的数据是否发生了变化(操作提交还是回滚了)。
Spring在Transaction事务传播行为种类,希望对大家有所帮助
org.springframework.transaction-3.2.2.RELEASE最新版本