javascript
Spring jdbc Template和Spring 事务管理
使用jdbcTemplate完成增刪改查操作(重點(diǎn))
package com.it.jdbctemplate;import java.util.List;import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.it.domain.User; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations="classpath:applicationContext.xml") public class JdbcTemplateTest3 {@Autowiredprivate JdbcTemplate jdbcTemplate;//修改 @Testpublic void test1(){jdbcTemplate.execute("update t_user set sex='男' where id=2");}//添加操作 @Testpublic void test2(){jdbcTemplate.execute("insert into t_user values(null,'趙六',20,'女')");}//刪除操作 @Testpublic void test3(){jdbcTemplate.execute("delete from t_user where id =5");}//測試返回簡單數(shù)據(jù)類型 @Testpublic void test4(){String name = jdbcTemplate.queryForObject("select name from t_user where id=?", String.class,1);System.out.println(name);}//測試返回簡單數(shù)據(jù)類型 @Testpublic void test5(){Integer count = jdbcTemplate.queryForObject("select count(*) from t_user",Integer.class);System.out.println(count);}//使用BeanPropertyRowMapper @Testpublic void test6(){ // User user = jdbcTemplate.queryForObject("select * from t_user where id=?", // new BeanPropertyRowMapper<User>(User.class),2); List<User> user = jdbcTemplate.query("select * from t_user", new BeanPropertyRowMapper<User>(User.class));System.out.println(user);}}?
掌握數(shù)據(jù)庫連接池的使用和配置(重點(diǎn))
3.掌握事務(wù)的傳播行為(重點(diǎn))
Spring事務(wù)管理機(jī)制
3.1PlatformTransactionManager(平臺事務(wù)管理器)
3.2TransactionDefinition(事務(wù)的定義信息)
傳播 :它解決的是兩個(gè)被事務(wù)管理的方法互相調(diào)用問題。它與數(shù)據(jù)庫沒有關(guān)系,是程序內(nèi)部維護(hù)的問題
propagation required(傳播請求) : ?默認(rèn)值 兩個(gè)操作處于同一個(gè)事務(wù),如果之前沒有事務(wù),新建一個(gè)事務(wù)
propagation requires new (傳播新請求) : 兩個(gè)操作處于不同的事務(wù)
propagtion nested ?: 它是一種嵌套事務(wù),它是使用SavePoint來實(shí)現(xiàn)的。事務(wù)回滾時(shí)可以回滾到指定的 savepoint
注意:它只對DataSourceTransactionManager有作用
3.2TransactionStatus(事務(wù)狀態(tài)信息)
4.了解基于xml配置聲明式事務(wù)管理(了解)
<!--創(chuàng)建事務(wù)管理器 --><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="c3p0DataSource"></property></bean><!--配置通知 --><tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><tx:method name="account" /></tx:attributes></tx:advice><!--配置切面 --><aop:config><aop:pointcut expression="execution(* com.it.service.IAccountService.account(..))" id="txPointcut"/><aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/></aop:config>?
5.掌握基于annotation聲明式事務(wù)管理(重點(diǎn))
?
? ??
轉(zhuǎn)載于:https://www.cnblogs.com/weihaiyang/p/7113546.html
總結(jié)
以上是生活随笔為你收集整理的Spring jdbc Template和Spring 事务管理的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: wget下载一个目录下的文件
- 下一篇: [cocos2d-x]HelloWorl