springboot开发重点笔记01

 

  • SpringBoot中在Controller中使用事务

//1. 在入口类xxApplication使用注解@EnableTransactionManagement开启事务


//2.在controller里面
@Autowired
private PlatformTransactionManager txManager; 
 
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
TransactionStatus status = txManager.getTransaction(def);
try {
 // 同步完成的数据操作代码
} catch (Exception ex) {
     txManager.rollback(status);
}
txManager.commit(status);

// 3.如果在同步完成代码块中有处理特殊情况返回的语句,如 “return modelandview;”之类的直接跳出事务时,这里要根据具体情况,在该语句前加上“txManager.rollback(status);”或“txManager.commit(status);”。不然这里会滞留多个事务,并最后积累导致错误!!!
  • 分页

// 分页 package com.github.pagehelper;中的Page和PageInfo 类
PageRequest pageRequest = PageRequest.of(page, size);
Page zcList = zcService.findByConditions(zc);
PageInfo info = new PageInfo<>(zcList.getResult());

map.put("zcList", zcList);   //返回的数据表
map.put("total", info.getTotal()); //共多少条数据
map.put("currentPage", currentPage);
map.put("totalPages", info.getPages() ); //分页后,共多少页
map.put("pagesize", pagesize);

 

你可能感兴趣的:(Java,java)