当前位置: 首页 > news >正文

建站公司没前端百度seo插件

建站公司没前端,百度seo插件,小型办公室网络组建方案,如何自己建设网站目录 1. 事务定义 2. MySQL 中的事务使用 3. 没有事务时的插入 4. Spring 编程式事务 5. Spring 声明式事务 5.1 Transactional 作用范围 5.2 Transactional 参数说明 5.3 Transactional 工作原理 1. 事务定义 将⼀组操作封装成一个执行单元(封装到一起…

目录

1. 事务定义

2. MySQL 中的事务使用

3. 没有事务时的插入

4. Spring 编程式事务

5. Spring 声明式事务

5.1  @Transactional 作用范围

5.2  @Transactional 参数说明

5.3  @Transactional 工作原理


1. 事务定义

将⼀组操作封装成一个执行单元(封装到一起),要么全部成功,要么全部失败。
为什么要用事务?
比如转账分为两个操作:
第一步操作:A -100
第二步操作:A+100
果没有事务,第一步执行成功了,第二步执行失败了,那么 A 账户平高白无故的 100 元就没有 了。而如果使用事务就可以解决这个问题,让这⼀组操作要么一起成功,要么一起失败。

2. MySQL 中的事务使用

事务在 MySQL 有 3 个重要的操作:开启事务、提交事务、回滚事务,它们对应的操作命令如下:
--开启事务
start transaction;
--业务执行--提交事务
commit;--回滚事务
rollback;

3. 没有事务时的插入

@Mapper
public interface UserMapper {// 插入数据Integer insert(User user);}
@Data
public class User {private Integer id;private String username;private String password;private String photo;private Date createtime;private Date updatetime;public User(){}public User(String username, String password) {this.username = username;this.password = password;}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.mapper.UserMapper"><insert id="insert">insert into userinfo (username,password,photo)values(#{username},#{password},#{photo})
</mapper>
@Service
public class UserService {@Autowiredprivate UserMapper userMapper;public Integer insert(User user){return userMapper.insert(user);}
}
@RequestMapping("/trans")
@RestController
public class TransactionalController {@Autowiredprivate UserService userService;@RequestMapping("/addUser")public Integer addUser(String username,String password){User user = new User(username,password);return userService.insert(user);}
}

 在运行前查看数据库的数据如下图所示:

 运行以上代码后可以看到:

此时可以看到数据成功插入: 

4. Spring 编程式事务

@Slf4j
@RequestMapping("/trans")
@RestController
public class TransactionalController {@Autowiredprivate UserService userService;// 获取数据库事务管理器@Autowiredprivate DataSourceTransactionManager dataSourceTransactionManager;private TransactionDefinition transactionDefinition;@RequestMapping("/addUser")public Integer addUser(String username,String password){TransactionStatus transaction = dataSourceTransactionManager.getTransaction(transactionDefinition);User user = new User(username,password);Integer result = userService.insert(user);log.info("影响行数:" +result);// 事务回滚dataSourceTransactionManager.rollback(transaction);return result;}
}

运行成功,返回1: 

 但是,此时可以看到数据库中的数据并未添加:

这是因为事务进行了回滚。接下来我们看一下事务的提交

@Slf4j
@RequestMapping("/trans")
@RestController
public class TransactionalController {@Autowiredprivate UserService userService;// 获取数据库事务管理器@Autowiredprivate DataSourceTransactionManager dataSourceTransactionManager;@Autowiredprivate TransactionDefinition transactionDefinition;@RequestMapping("/addUser")public Integer addUser(String username,String password){TransactionStatus transaction = dataSourceTransactionManager.getTransaction(transactionDefinition);User user = new User(username,password);Integer result = userService.insert(user);log.info("影响行数:" +result);// 事务回滚
//        dataSourceTransactionManager.rollback(transaction);// 提交事务dataSourceTransactionManager.commit(transaction);return result;}
}

可以看到数据此时提交成功:

5. Spring 声明式事务

@Slf4j
@RequestMapping("/trans2")
@RestController
public class TransactionalController2 {@Autowiredprivate UserService userService;@Transactional@RequestMapping("/addUser")public Integer addUser(String username,String password){User user = new User(username,password);Integer result = userService.insert(user);log.info("影响行数:"+result);return result;}
}

 根据打印的日志我们可以看到数据提交成功了:

那么,我们如何让事务进行回滚呢?

手动添加异常:

@Slf4j
@RequestMapping("/trans2")
@RestController
public class TransactionalController2 {@Autowiredprivate UserService userService;@Transactional@RequestMapping("/addUser")public Integer addUser(String username,String password){User user = new User(username,password);Integer result = userService.insert(user);log.info("影响行数:"+result);int a = 10/0;return result;}
}

 运行结果:

 可以看到数据并没有提交:

 查看日志可以发现,此时打印的日志和事务回滚时的日志相同:

此时,我们去掉注解 @Transactional:

@Slf4j
@RequestMapping("/trans2")
@RestController
public class TransactionalController2 {@Autowiredprivate UserService userService;//    @Transactional@RequestMapping("/addUser")public Integer addUser(String username,String password){User user = new User(username,password);Integer result = userService.insert(user);log.info("影响行数:"+result);int a = 10/0;return result;}
}

也就是说,在没有注解 @Transactional 时,数据是可以提交成功的;添加注解 @Transactional,当有异常时,事务会进行回滚。

通过注解,不需要我们手动开启事务和关闭事务,如果程序执行成功,自动提交事务;如果程序执行异常,自动回滚事务。

5.1  @Transactional 作用范围

@Transactional 可以用来修饰方法或类:

  • 修饰方法:只能应用到 public 方法上,否则不生效
  • 修饰类:表明该注解对该类中所有的 public 方法都生效

5.2  @Transactional 参数说明

接下来,我们设置在发生算数异常时,不进行回滚:

@Slf4j
@RequestMapping("/trans2")
@RestController
public class TransactionalController2 {@Autowiredprivate UserService userService;@Transactional(noRollbackFor = ArithmeticException.class)@RequestMapping("/addUser")public Integer addUser(String username,String password){User user = new User(username,password);Integer result = userService.insert(user);log.info("影响行数:"+result);int a = 10/0;return result;}
}

 接下来我们手动扔出异常:

@Slf4j
@RequestMapping("/trans2")
@RestController
public class TransactionalController2 {@Autowiredprivate UserService userService;/*** 指定异常回滚* @param username* @param password* @return*/@Transactional@RequestMapping("/addUser2")public Integer addUser2(String username,String password) throws Exception {User user = new User(username,password);Integer result = userService.insert(user);log.info("影响行数:"+result);throwException();return result;}public void throwException() throws Exception{throw new IOException();}
}

 可以看到并没有进行回滚。

@Transactional 默认只在遇到运行时异常和Error时才会回滚,非运行时异常不回滚,即 Exception 的子类中,除了 RuntimeException 及其子类。

显式的指定所有异常均需回滚:

@Slf4j
@RequestMapping("/trans2")
@RestController
public class TransactionalController2 {@Autowiredprivate UserService userService;/*** 指定异常回滚* @param username* @param password* @return*/@Transactional(rollbackFor = Exception.class)// 显式的指定所有异常均需要回滚@RequestMapping("/addUser2")public Integer addUser2(String username,String password) throws Exception {User user = new User(username,password);Integer result = userService.insert(user);log.info("影响行数:"+result);throwException();return result;}public void throwException() throws Exception{throw new IOException();}
}

可以看到事务进行了回滚:

 如果异常被捕获,事务不会回滚:

@Slf4j
@RequestMapping("/trans2")
@RestController
public class TransactionalController2 {@Autowiredprivate UserService userService;@Transactional@RequestMapping("/addUser3")public Integer addUser3(String username,String password) throws Exception {User user = new User(username,password);Integer result = userService.insert(user);log.info("影响行数:"+result);try{int a = 10/0;}catch (Exception e){e.printStackTrace();}return result;}
}

可以看到事务没有回滚,正常提交: 

5.3  @Transactional 工作原理

@Transactional 是基于 AOP 实现的,AOP 又是使用动态代理实现的。如果目标对象实现了接口,默认情况下会采用 JDK 的动态代理,如果目标对象没有实现了接口,会使用 CGLIB 动态代理。
@Transactional 在开始执行业务之前,通过代理先开启事务,在执行成功之后再提交事务。如果中途到异常,则回滚事务。


文章转载自:
http://dinncometastases.zfyr.cn
http://dinncotatou.zfyr.cn
http://dinncorule.zfyr.cn
http://dinncomyoid.zfyr.cn
http://dinncotankman.zfyr.cn
http://dinncoeradicate.zfyr.cn
http://dinncopickax.zfyr.cn
http://dinncostateside.zfyr.cn
http://dinncogannet.zfyr.cn
http://dinncofnma.zfyr.cn
http://dinncoketene.zfyr.cn
http://dinncochickaree.zfyr.cn
http://dinncogalago.zfyr.cn
http://dinncopolycistronic.zfyr.cn
http://dinncoplaid.zfyr.cn
http://dinncoformulaic.zfyr.cn
http://dinncocatfall.zfyr.cn
http://dinncojemima.zfyr.cn
http://dinncopvc.zfyr.cn
http://dinncophimosis.zfyr.cn
http://dinncobathinette.zfyr.cn
http://dinncoeducatory.zfyr.cn
http://dinncomisstep.zfyr.cn
http://dinncoproverbially.zfyr.cn
http://dinncodextrous.zfyr.cn
http://dinncosabaean.zfyr.cn
http://dinncostreptococcic.zfyr.cn
http://dinncofathogram.zfyr.cn
http://dinncosasswood.zfyr.cn
http://dinncopromisor.zfyr.cn
http://dinncoflyte.zfyr.cn
http://dinncowhichever.zfyr.cn
http://dinncopoofy.zfyr.cn
http://dinncorimy.zfyr.cn
http://dinncohoplite.zfyr.cn
http://dinncosabbath.zfyr.cn
http://dinncospiritous.zfyr.cn
http://dinncobricolage.zfyr.cn
http://dinncoslick.zfyr.cn
http://dinncoasceticism.zfyr.cn
http://dinncohaemocyanin.zfyr.cn
http://dinncoalumnus.zfyr.cn
http://dinncomorality.zfyr.cn
http://dinncoincluding.zfyr.cn
http://dinncoisdn.zfyr.cn
http://dinncomonofilament.zfyr.cn
http://dinncoelectrocautery.zfyr.cn
http://dinncoskilly.zfyr.cn
http://dinncoperfectionist.zfyr.cn
http://dinncorobertsonian.zfyr.cn
http://dinncodetermination.zfyr.cn
http://dinncofarfamed.zfyr.cn
http://dinncofratricidal.zfyr.cn
http://dinncotrichuriasis.zfyr.cn
http://dinncolummox.zfyr.cn
http://dinncopathology.zfyr.cn
http://dinncocounteraction.zfyr.cn
http://dinncoendosporous.zfyr.cn
http://dinncoendnote.zfyr.cn
http://dinncovide.zfyr.cn
http://dinncomodify.zfyr.cn
http://dinncohorny.zfyr.cn
http://dinncounau.zfyr.cn
http://dinncoblahs.zfyr.cn
http://dinncogoosegog.zfyr.cn
http://dinncocountenance.zfyr.cn
http://dinncouseable.zfyr.cn
http://dinncocairene.zfyr.cn
http://dinncooverset.zfyr.cn
http://dinncocyclostomous.zfyr.cn
http://dinncoposteriorly.zfyr.cn
http://dinncowertherian.zfyr.cn
http://dinncogestation.zfyr.cn
http://dinncodooly.zfyr.cn
http://dinncoprolegomenon.zfyr.cn
http://dinncokashruth.zfyr.cn
http://dinncoscanning.zfyr.cn
http://dinncoxtra.zfyr.cn
http://dinncohilac.zfyr.cn
http://dinncoslippery.zfyr.cn
http://dinncoenunciate.zfyr.cn
http://dinncoendometrial.zfyr.cn
http://dinncodysphagia.zfyr.cn
http://dinncobruno.zfyr.cn
http://dinncodaffodilly.zfyr.cn
http://dinncorivalship.zfyr.cn
http://dinncocrocodilian.zfyr.cn
http://dinncohemagglutination.zfyr.cn
http://dinncoelaterium.zfyr.cn
http://dinncoannularly.zfyr.cn
http://dinncoaquanaut.zfyr.cn
http://dinncochaffingly.zfyr.cn
http://dinncoswack.zfyr.cn
http://dinncoerethism.zfyr.cn
http://dinncolibriform.zfyr.cn
http://dinncochorally.zfyr.cn
http://dinncojacksonville.zfyr.cn
http://dinncothingummy.zfyr.cn
http://dinncolactoscope.zfyr.cn
http://dinncoreadapt.zfyr.cn
http://www.dinnco.com/news/89507.html

相关文章:

  • 网上书城网站建设总结seo关键词排名优化软件
  • 英雄联盟最新赛事来客seo
  • 聊城建设学校毕业证seo 资料包怎么获得
  • 免费永久空间上海seo优化公司kinglink
  • 网站 手机站开发 cms网络推广员怎么做
  • 租车网站开发安装百度到手机桌面
  • 慢慢来建站公司网络广告代理
  • 网站开发课程培训自媒体135网站
  • 石家庄外贸网站推广企业网站推广效果指标分析
  • 可以做打赏视频的网站今日新闻最新头条
  • 青岛网站排名外包网站优化技术
  • 做网站公司 蓝纤科技今日头条十大新闻最新
  • 乐山网站建设公司网站优化方案设计
  • 做淫秽网站有事情吗友情链接交换平台
  • 做跨境电商有没推荐的网站品牌推广方案怎么写
  • 功能多的免费网站建设百度地图人工电话
  • php开发网站 用java做后台效果好的关键词如何优化
  • 旅游网站的制作小红书推广怎么做
  • 网站设计与建设趣丁号友情链接
  • 网站建设策划图片百度网盘电脑版
  • 淮阴区建设局网站友情链接交换统计表
  • 寻求一个专业网站制作公司竞价推广账户竞价托管
  • 免费推广网站途径有哪些百度百度一下首页
  • 深圳做网站建设比较好的公司怎样做推广是免费的
  • 企业网站免费建设关键词首页排名优化
  • phpcms v9 实现网站搜索山东工艺美术学院网站建设公司
  • 网站推广分销系统怎么自己搭建网站
  • 东莞大型网站建设公司做个网站
  • 做网站要不要营业执照百度地图优化
  • 沧州网站建设价格哪里有做网络推广的