spring注解事务使用总结
在使用spring的注解事务的时候,需要考虑到事务的传播行为、遇到什么类型的异常时,事务才起作用、事务方法之间的嵌套调用时,怎么样才生效等等诸多问题。网上搜到很多的主要还是一堆理论文字描述,我这里给出亲测的代码,是借助公司真实的系统来做测试。
系统之间调用图如下:
事务和异步处理都在server模块里面。
接口如下:
- /**
- * 测试事务行为接口
- *
- * @author plg
- *
- */
- public interface TestService {
- public void methodA();
- public void methodB();
- public void methodC();
- .
- .
- .
- }
/**
* 测试事务行为接口
*
* @author plg
*
*/
public interface TestService { public void methodA(); public void methodB(); public void methodC(); .
.
.
}
使用两个表:
- CREATE TABLE `mall_order_statistics` (
- `id` bigint(20) NOT NULL AUTO_INCREMENT,
- `shop_id` bigint(20) NOT NULL,
- `order_num` bigint(20) NOT NULL,
- `order_amount` bigint(20) NOT NULL,
- `avg_order_amount` bigint(20) NOT NULL,
- `pay_type` int(11) NOT NULL,
- `order_type` int(11) NOT NULL',
- `order_date` date NOT NULL,
- `create_time` datetime NOT NULL,
- PRIMARY KEY (`id`),
- KEY `idx_shop_id` (`shop_id`)
- ) ENGINE=InnoDB
- CREATE TABLE `mall_goods_ranking` (
- `id` bigint(20) NOT NULL AUTO_INCREMENT,
- `shop_id` bigint(20) NOT NULL,
- `order_date` date NOT NULL,
- `mall_goods_id` bigint(20) NOT NULL,
- `goods_name` varchar(20) NOT NULL,
- `sales_volume` bigint(20) NOT NULL,
- `sales_amount` bigint(20) NOT NULL,
- `create_time` datetime NOT NULL,
- PRIMARY KEY (`id`),
- KEY `idx_shop_id` (`shop_id`)
- ) ENGINE=InnoDB
CREATE TABLE `mall_order_statistics` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`shop_id` bigint(20) NOT NULL,
`order_num` bigint(20) NOT NULL,
`order_amount` bigint(20) NOT NULL,
`avg_order_amount` bigint(20) NOT NULL,
`pay_type` int(11) NOT NULL,
`order_type` int(11) NOT NULL',
`order_date` date NOT NULL,
`create_time` datetime NOT NULL,
PRIMARY KEY (`id`),
KEY `idx_shop_id` (`shop_id`)
) ENGINE=InnoDB CREATE TABLE `mall_goods_ranking` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`shop_id` bigint(20) NOT NULL,
`order_date` date NOT NULL,
`mall_goods_id` bigint(20) NOT NULL,
`goods_name` varchar(20) NOT NULL,
`sales_volume` bigint(20) NOT NULL,
`sales_amount` bigint(20) NOT NULL,
`create_time` datetime NOT NULL,
PRIMARY KEY (`id`),
KEY `idx_shop_id` (`shop_id`)
) ENGINE=InnoDB
1.事务与异常类型
这里用的是注解式事务@Transactional。
1.1 正常的处理
先来一个正常的处理,事先已把数据库表清空,代码调用如下:
- web端代码如下:
- try {
- testService.methodA();
- } catch (Exception e) {
- logger.error("========================= " + e);
- }
- server端代码如下:
- @Transactional
- @Override
- public void methodA() {
- MallGoodsRanking mallGoodsRanking = new MallGoodsRanking();
- mallGoodsRanking.setShopId(1L);
- mallGoodsRanking.setOrderDate(new Date());
- mallGoodsRanking.setMallGoodsId(1L);
- mallGoodsRanking.setGoodsName("测试");
- mallGoodsRanking.setSalesVolume(1L);
- mallGoodsRanking.setSalesAmount(1L);
- mallGoodsRanking.setCreateTime(new Date());
- mallGoodsRankingMapper.insert(mallGoodsRanking);
- MallOrderStatistics mallOrderStatistics = new MallOrderStatistics();
- mallOrderStatistics.setShopId(1L);
- mallOrderStatistics.setOrderNum(1L);
- mallOrderStatistics.setAvgOrderAmount(1L);
- mallOrderStatistics.setOrderAmount(1L);
- mallOrderStatistics.setPayType();
- mallOrderStatistics.setOrderType();
- mallOrderStatistics.setOrderDate(new Date());
- mallOrderStatistics.setCreateTime(new Date());
- mallOrderStatisticsMapper.insert(mallOrderStatistics);
- }
web端代码如下:
try {
testService.methodA();
} catch (Exception e) {
logger.error("========================= " + e);
} server端代码如下:
@Transactional
@Override
public void methodA() {
MallGoodsRanking mallGoodsRanking = new MallGoodsRanking();
mallGoodsRanking.setShopId(1L);
mallGoodsRanking.setOrderDate(new Date());
mallGoodsRanking.setMallGoodsId(1L);
mallGoodsRanking.setGoodsName("测试");
mallGoodsRanking.setSalesVolume(1L);
mallGoodsRanking.setSalesAmount(1L);
mallGoodsRanking.setCreateTime(new Date());
mallGoodsRankingMapper.insert(mallGoodsRanking); MallOrderStatistics mallOrderStatistics = new MallOrderStatistics();
mallOrderStatistics.setShopId(1L);
mallOrderStatistics.setOrderNum(1L);
mallOrderStatistics.setAvgOrderAmount(1L);
mallOrderStatistics.setOrderAmount(1L);
mallOrderStatistics.setPayType(1);
mallOrderStatistics.setOrderType(1);
mallOrderStatistics.setOrderDate(new Date());
mallOrderStatistics.setCreateTime(new Date());
mallOrderStatisticsMapper.insert(mallOrderStatistics);
}
执行结果:
1.2异常处理-不插入必填字段,抛出RuntimeException
- @Transactional
- @Override
- public void methodA() {
- MallGoodsRanking mallGoodsRanking = new MallGoodsRanking();
- mallGoodsRanking.setShopId(1L);
- mallGoodsRanking.setOrderDate(new Date());
- mallGoodsRanking.setMallGoodsId(1L);
- mallGoodsRanking.setGoodsName("测试");
- mallGoodsRanking.setSalesVolume(1L);
- mallGoodsRanking.setSalesAmount(1L);
- mallGoodsRanking.setCreateTime(new Date());
- mallGoodsRankingMapper.insert(mallGoodsRanking);
- MallOrderStatistics mallOrderStatistics = new MallOrderStatistics();
- // mallOrderStatistics.setShopId(1L);必填字段
- mallOrderStatistics.setOrderNum(1L);
- mallOrderStatistics.setAvgOrderAmount(1L);
- mallOrderStatistics.setOrderAmount(1L);
- mallOrderStatistics.setPayType();
- mallOrderStatistics.setOrderType();
- mallOrderStatistics.setOrderDate(new Date());
- mallOrderStatistics.setCreateTime(new Date());
- mallOrderStatisticsMapper.insert(mallOrderStatistics);
- }
@Transactional
@Override
public void methodA() {
MallGoodsRanking mallGoodsRanking = new MallGoodsRanking();
mallGoodsRanking.setShopId(1L);
mallGoodsRanking.setOrderDate(new Date());
mallGoodsRanking.setMallGoodsId(1L);
mallGoodsRanking.setGoodsName("测试");
mallGoodsRanking.setSalesVolume(1L);
mallGoodsRanking.setSalesAmount(1L);
mallGoodsRanking.setCreateTime(new Date());
mallGoodsRankingMapper.insert(mallGoodsRanking); MallOrderStatistics mallOrderStatistics = new MallOrderStatistics();
// mallOrderStatistics.setShopId(1L);必填字段
mallOrderStatistics.setOrderNum(1L);
mallOrderStatistics.setAvgOrderAmount(1L);
mallOrderStatistics.setOrderAmount(1L);
mallOrderStatistics.setPayType(1);
mallOrderStatistics.setOrderType(1);
mallOrderStatistics.setOrderDate(new Date());
mallOrderStatistics.setCreateTime(new Date());
mallOrderStatisticsMapper.insert(mallOrderStatistics);
}
如上面的代码,把第二条插入语句的一个必填字段注释掉,这样执行之后,抛出了异常:
- ; SQL []; Column 'shop_id' cannot be null; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'shop_id' cannot be null, dubbo version: 2.8., current host: 192.168.9.141
- org.springframework.dao.DataIntegrityViolationException:
; SQL []; Column 'shop_id' cannot be null; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'shop_id' cannot be null, dubbo version: 2.8.4, current host: 192.168.9.141
org.springframework.dao.DataIntegrityViolationException:
抛出了DataIntegrityViolationException类型的异常,这个异常是RuntimeException类型的异常,spring的@Transactional默认就是捕捉RuntimeException异常,遇到RuntimeException异常才会处理事务回滚。
既然抛出了RuntimeException异常,那数据库自然没有插入成功了。接下来演示抛出Exception异常,看事务能否生效。
1.3异常处理-不插入必填字段,抛出Exception
- @Transactional
- @Override
- public void methodA() throws Exception {
- try {
- MallGoodsRanking mallGoodsRanking = new MallGoodsRanking();
- mallGoodsRanking.setShopId(1L);
- mallGoodsRanking.setOrderDate(new Date());
- mallGoodsRanking.setMallGoodsId(1L);
- mallGoodsRanking.setGoodsName("测试");
- mallGoodsRanking.setSalesVolume(1L);
- mallGoodsRanking.setSalesAmount(1L);
- mallGoodsRanking.setCreateTime(new Date());
- mallGoodsRankingMapper.insert(mallGoodsRanking);
- MallOrderStatistics mallOrderStatistics = new MallOrderStatistics();
- // mallOrderStatistics.setShopId(1L);必填字段
- mallOrderStatistics.setOrderNum(1L);
- mallOrderStatistics.setAvgOrderAmount(1L);
- mallOrderStatistics.setOrderAmount(1L);
- mallOrderStatistics.setPayType();
- mallOrderStatistics.setOrderType();
- mallOrderStatistics.setOrderDate(new Date());
- mallOrderStatistics.setCreateTime(new Date());
- mallOrderStatisticsMapper.insert(mallOrderStatistics);
- } catch (Exception e) {
- logger.error(" ================== " + e);
- throw new Exception("异常");
- }
- }
@Transactional
@Override
public void methodA() throws Exception {
try {
MallGoodsRanking mallGoodsRanking = new MallGoodsRanking();
mallGoodsRanking.setShopId(1L);
mallGoodsRanking.setOrderDate(new Date());
mallGoodsRanking.setMallGoodsId(1L);
mallGoodsRanking.setGoodsName("测试");
mallGoodsRanking.setSalesVolume(1L);
mallGoodsRanking.setSalesAmount(1L);
mallGoodsRanking.setCreateTime(new Date());
mallGoodsRankingMapper.insert(mallGoodsRanking); MallOrderStatistics mallOrderStatistics = new MallOrderStatistics();
// mallOrderStatistics.setShopId(1L);必填字段
mallOrderStatistics.setOrderNum(1L);
mallOrderStatistics.setAvgOrderAmount(1L);
mallOrderStatistics.setOrderAmount(1L);
mallOrderStatistics.setPayType(1);
mallOrderStatistics.setOrderType(1);
mallOrderStatistics.setOrderDate(new Date());
mallOrderStatistics.setCreateTime(new Date());
mallOrderStatisticsMapper.insert(mallOrderStatistics);
} catch (Exception e) {
logger.error(" ================== " + e);
throw new Exception("异常");
}
}
捕捉异常后,然后 throw new Exception。执行后,抛出异常:
- java.lang.Exception: 异常
- at yunnex.saofu.mall.service.impl.TestServiceImpl.methodA(TestServiceImpl.java:)
- at yunnex.saofu.mall.service.impl.TestServiceImpl$$FastClassBySpringCGLIB$$8fcf06b.invoke(<generated>)
- at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:)
- at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:)
java.lang.Exception: 异常
at yunnex.saofu.mall.service.impl.TestServiceImpl.methodA(TestServiceImpl.java:55)
at yunnex.saofu.mall.service.impl.TestServiceImpl$$FastClassBySpringCGLIB$$8fcf06b.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:717)
看到了吗,第一条SQL还是插入成功了,第二条语句插入出错,抛出Exception异常,但是事务并没有回滚。 接下来测试一下@Transactional(rollbackFor=Exception.class)这种情况。
1.4异常处理-不插入必填字段,抛出Exception,@Transactional指定异常类型
数据库表没有插入数据。
2.事务方法嵌套使用
这里在引入一个表(mall_config),这个表原先是空的。
2.1事务方法调用私有方法,私有方法有数据库操作,私有方法抛出异常
- @Transactional
- @Override
- public void methodA() throws Exception {
- MallConfig mallConfig = new MallConfig();
- mallConfig.setCanFetch(true);
- mallConfig.setCanDeliver(true);
- mallConfigMapper.insert(mallConfig);
- this.insert();
- }
- private void insert() {
- MallGoodsRanking mallGoodsRanking = new MallGoodsRanking();
- mallGoodsRanking.setShopId(1L);
- mallGoodsRanking.setOrderDate(new Date());
- mallGoodsRanking.setMallGoodsId(1L);
- mallGoodsRanking.setGoodsName("测试");
- mallGoodsRanking.setSalesVolume(1L);
- mallGoodsRanking.setSalesAmount(1L);
- mallGoodsRanking.setCreateTime(new Date());
- mallGoodsRankingMapper.insert(mallGoodsRanking);
- MallOrderStatistics mallOrderStatistics = new MallOrderStatistics();
- // mallOrderStatistics.setShopId(1L);必填字段
- mallOrderStatistics.setOrderNum(1L);
- mallOrderStatistics.setAvgOrderAmount(1L);
- mallOrderStatistics.setOrderAmount(1L);
- mallOrderStatistics.setPayType();
- mallOrderStatistics.setOrderType();
- mallOrderStatistics.setOrderDate(new Date());
- mallOrderStatistics.setCreateTime(new Date());
- mallOrderStatisticsMapper.insert(mallOrderStatistics);//这里抛出异常
- }
@Transactional
@Override
public void methodA() throws Exception { MallConfig mallConfig = new MallConfig();
mallConfig.setCanFetch(true);
mallConfig.setCanDeliver(true);
mallConfigMapper.insert(mallConfig); this.insert();
} private void insert() {
MallGoodsRanking mallGoodsRanking = new MallGoodsRanking();
mallGoodsRanking.setShopId(1L);
mallGoodsRanking.setOrderDate(new Date());
mallGoodsRanking.setMallGoodsId(1L);
mallGoodsRanking.setGoodsName("测试");
mallGoodsRanking.setSalesVolume(1L);
mallGoodsRanking.setSalesAmount(1L);
mallGoodsRanking.setCreateTime(new Date());
mallGoodsRankingMapper.insert(mallGoodsRanking); MallOrderStatistics mallOrderStatistics = new MallOrderStatistics();
// mallOrderStatistics.setShopId(1L);必填字段
mallOrderStatistics.setOrderNum(1L);
mallOrderStatistics.setAvgOrderAmount(1L);
mallOrderStatistics.setOrderAmount(1L);
mallOrderStatistics.setPayType(1);
mallOrderStatistics.setOrderType(1);
mallOrderStatistics.setOrderDate(new Date());
mallOrderStatistics.setCreateTime(new Date());
mallOrderStatisticsMapper.insert(mallOrderStatistics);//这里抛出异常
}
执行结果:
数据没有插入,事务生效!在同一个类中,一个事务方法调用私用方法,私用方法里有对数据库的操作,私有方法里有异常(RuntimeException)产生,这种情况事务是起效果的。
2.2 事务方法调用私有方法,私有方法有数据库操作,事务方法抛出异常
- @Transactional
- @Override
- public void methodA() throws Exception {
- MallConfig mallConfig = new MallConfig();
- mallConfig.setCanFetch(true);
- mallConfig.setCanDeliver(true);
- mallConfigMapper.insert(mallConfig);
- this.insert();
- //第三部分
- MallOrderStatistics mallOrderStatistics = new MallOrderStatistics();
- // mallOrderStatistics.setShopId(1L);必填字段
- mallOrderStatistics.setOrderNum(1L);
- mallOrderStatistics.setAvgOrderAmount(1L);
- mallOrderStatistics.setOrderAmount(1L);
- mallOrderStatistics.setPayType();
- mallOrderStatistics.setOrderType();
- mallOrderStatistics.setOrderDate(new Date());
- mallOrderStatistics.setCreateTime(new Date());
- mallOrderStatisticsMapper.insert(mallOrderStatistics);
- }
- private void insert() {
- MallGoodsRanking mallGoodsRanking = new MallGoodsRanking();
- mallGoodsRanking.setShopId(1L);
- mallGoodsRanking.setOrderDate(new Date());
- mallGoodsRanking.setMallGoodsId(1L);
- mallGoodsRanking.setGoodsName("测试");
- mallGoodsRanking.setSalesVolume(1L);
- mallGoodsRanking.setSalesAmount(1L);
- mallGoodsRanking.setCreateTime(new Date());
- mallGoodsRankingMapper.insert(mallGoodsRanking);
- }
@Transactional
@Override
public void methodA() throws Exception { MallConfig mallConfig = new MallConfig();
mallConfig.setCanFetch(true);
mallConfig.setCanDeliver(true);
mallConfigMapper.insert(mallConfig); this.insert(); //第三部分
MallOrderStatistics mallOrderStatistics = new MallOrderStatistics();
// mallOrderStatistics.setShopId(1L);必填字段
mallOrderStatistics.setOrderNum(1L);
mallOrderStatistics.setAvgOrderAmount(1L);
mallOrderStatistics.setOrderAmount(1L);
mallOrderStatistics.setPayType(1);
mallOrderStatistics.setOrderType(1);
mallOrderStatistics.setOrderDate(new Date());
mallOrderStatistics.setCreateTime(new Date());
mallOrderStatisticsMapper.insert(mallOrderStatistics);
} private void insert() {
MallGoodsRanking mallGoodsRanking = new MallGoodsRanking();
mallGoodsRanking.setShopId(1L);
mallGoodsRanking.setOrderDate(new Date());
mallGoodsRanking.setMallGoodsId(1L);
mallGoodsRanking.setGoodsName("测试");
mallGoodsRanking.setSalesVolume(1L);
mallGoodsRanking.setSalesAmount(1L);
mallGoodsRanking.setCreateTime(new Date());
mallGoodsRankingMapper.insert(mallGoodsRanking);
}
看以上代码,只有第三部分那里抛出异常,执行结果和2.1一样如下。在同一个类里面,一个事务方法里调用私有方法,相当于代码都写在事务方法里。
2.3 接口方法调用私有方法,私有方法添加事务注解
- @Override
- public void methodA() throws Exception {
- MallConfig mallConfig = new MallConfig();
- mallConfig.setCanFetch(true);
- mallConfig.setCanDeliver(true);
- mallConfigMapper.insert(mallConfig);
- this.insert();
- }
- @Transactional
- private void insert() {
- MallGoodsRanking mallGoodsRanking = new MallGoodsRanking();
- mallGoodsRanking.setShopId(1L);
- mallGoodsRanking.setOrderDate(new Date());
- mallGoodsRanking.setMallGoodsId(1L);
- mallGoodsRanking.setGoodsName("测试");
- mallGoodsRanking.setSalesVolume(1L);
- mallGoodsRanking.setSalesAmount(1L);
- mallGoodsRanking.setCreateTime(new Date());
- mallGoodsRankingMapper.insert(mallGoodsRanking);
- MallOrderStatistics mallOrderStatistics = new MallOrderStatistics();
- // mallOrderStatistics.setShopId(1L);必填字段
- mallOrderStatistics.setOrderNum(1L);
- mallOrderStatistics.setAvgOrderAmount(1L);
- mallOrderStatistics.setOrderAmount(1L);
- mallOrderStatistics.setPayType();
- mallOrderStatistics.setOrderType();
- mallOrderStatistics.setOrderDate(new Date());
- mallOrderStatistics.setCreateTime(new Date());
- mallOrderStatisticsMapper.insert(mallOrderStatistics);
- }
@Override
public void methodA() throws Exception { MallConfig mallConfig = new MallConfig();
mallConfig.setCanFetch(true);
mallConfig.setCanDeliver(true);
mallConfigMapper.insert(mallConfig); this.insert();
} @Transactional
private void insert() {
MallGoodsRanking mallGoodsRanking = new MallGoodsRanking();
mallGoodsRanking.setShopId(1L);
mallGoodsRanking.setOrderDate(new Date());
mallGoodsRanking.setMallGoodsId(1L);
mallGoodsRanking.setGoodsName("测试");
mallGoodsRanking.setSalesVolume(1L);
mallGoodsRanking.setSalesAmount(1L);
mallGoodsRanking.setCreateTime(new Date());
mallGoodsRankingMapper.insert(mallGoodsRanking); MallOrderStatistics mallOrderStatistics = new MallOrderStatistics();
// mallOrderStatistics.setShopId(1L);必填字段
mallOrderStatistics.setOrderNum(1L);
mallOrderStatistics.setAvgOrderAmount(1L);
mallOrderStatistics.setOrderAmount(1L);
mallOrderStatistics.setPayType(1);
mallOrderStatistics.setOrderType(1);
mallOrderStatistics.setOrderDate(new Date());
mallOrderStatistics.setCreateTime(new Date());
mallOrderStatisticsMapper.insert(mallOrderStatistics);
}
执行结果如下:
出现异常后,前面两个表还是插入成功了。@Transactional放在private方法上是不起效果的,并且也不报错,spring官网也说明了这一点。实际上,像这种情况,内部调用带有事务注解的public方法,事务也不生效,也就是
- @Transactional
- public void insert() {
- 改成
- @Transactional
- private void insert() {
@Transactional
public void insert() { 改成 @Transactional
private void insert() {
具体原因请查看:http://blog.csdn.net/seelye/article/details/40144817。
方法内部调用带有事务注解的方法(无论是private还是public),事务是不生效的。
spring异步(@Async)处理也是一样,有时候在进行业务处理的过程,有些业务可以作为异步来处理,这时候就有一些同学就在同一类中新建一个方法,使用@Async注解,然后调用这个异步方法,其实这是不起作用的,一定要新建另外一个类,这个类新建公共方法,用@Async注解,然后调用才起到异步的作用。
spring注解事务使用总结的更多相关文章
- Spring注解(事务)
spring操作数据库 jdbc <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc --> & ...
- Spring -- 注解事务 以及 7个传播行为
注解事务: 1.开启注解事务配置: <!-- 事务管理器 --> <bean id="transactionManager" class="org.sp ...
- spring 注解事务
前提:在applicationContext.xml中配置<tx:annotation-driven transaction-manager="transactionManager&q ...
- spring注解事务管理
使用步骤: 步骤一.在spring配置文件中引入<tx:>命名空间<beans xmlns="http://www.springframework.org/schema/b ...
- 阶段3 2.Spring_10.Spring中事务控制_8 spring基于纯注解的声明式事务控制
新建项目 把之前项目src下的内容全部复制过来 pom.xml内复制过来 开始配置 新建一个config的包,然后再新建配置文件类SpringConfiguration @Configuration这 ...
- spring,mybatis事务管理配置与@Transactional注解使用[转]
spring,mybatis事务管理配置与@Transactional注解使用[转] spring,mybatis事务管理配置与@Transactional注解使用 概述事务管理对于企业应用来说是至关 ...
- 记录一次Spring boot 搭建框架连接Mysql数据库注解事务不回滚的故障
搭建了一个新框架,使用了spring boot 替换以简化原来繁杂的spring配置,使用Spring注解管理事务,持久层使用mybatis. 连接mysql数据库完成项目的过程中发现不支持事务,因为 ...
- Spring中注解事务方面的问题
我们可以在spring的配置文件beans.xml中对事务进行注解配置,这样在相应的类中就不用对事务进行管事,对于某个类,想单独交给spring来管理,那么就在相应的类上加入@Transactiona ...
- spring中注解事务认识
1.配置事务管理器 <!-- 设定transactionManager事务管理器 --> <bean id="txManager" class="org ...
随机推荐
- python 利用爬虫获取页面上下拉框里的所有国家
前段时间,领导说列一下某页面上的所有国家信息,话说这个国家下拉框里的国家有两三百个,是第三方模块导入的,手动从页面拷贝,不切实际,于是想着用爬虫去获取这个国家信息,并保存到文件里. 下面是具体的代码, ...
- acdream1174 合并同类项
这题说的是 给出N,a[1]... a[N],还有M,b[1]... b[M]long long ans = 0;for(int i = 1; i <= N; i ++) for(int ...
- Java 异步处理 三种实现
new Thread((new Runnable() { @Override public void run() { // 批量同步数据 try { logger.info("^^^^^^^ ...
- 20145118 《Java程序设计》第5周学习总结 教材学习内容总结
20145118 <Java程序设计>第5周学习总结 教材学习内容总结 1.Java中所有错误都会被打包成对象,可以通过try.catch语法对错误对象作处理,先执行try,如果出错则跳出 ...
- 《Effective Java 2nd》第7章 方法
目录 第38条 检查参数的有效性 第39条 必要时进行保护性拷贝 第40条 谨慎设计方法签名 第41条 慎用重载 第42条 慎用可变参数 第43条 返回零长度的数组或集合,而不是null 第44条 为 ...
- HDU 6438 网络赛 Buy and Resell(贪心 + 优先队列)题解
思路:维护一个递增队列,如果当天的w比队首大,那么我们给收益增加 w - q.top(),这里的意思可以理解为w对总收益的贡献而不是真正获利的具体数额,这样我们就能求出最大收益.注意一下,如果w对收益 ...
- 安装 bochs
sudo apt-get install bochs 以后接着安装bochs-x
- python 去除字符串两端的引号
a='"srting"' print(a) b=eval(a) print(b) 输出 "srting" srting
- javaScript 真经 小感 this 指向
编程世界只存在两种基本元素:一个是数据.一个是代码. (能写代码算入门,能处理复杂场景或者数据算合格,能不变应万变是不朽) 最流行的编程思想莫过于面向对象编程,因为面向对象编程思想把数据和代码结合成统 ...
- Django框架(上传Excel文件并读取)
博主今天整理下Django框架中上传Excel文件并读取 博主是要在管理平台中新增用例的维护功能,想着通过上传Excel文件来展示用例,下面是项目的路径图: 首先先建数据库模型 model.py 可以 ...