本文共 6828 字,大约阅读时间需要 22 分钟。
com.oracle ojdbc6 11.2.0.2.0
com.pyc transaction 0.0.1-SNAPSHOT transaction jar Demo project for Spring Boot
org.springframework.boot spring-boot-starter-parent 1.3.0.M2
4.0.0 org.springframework.boot spring-boot-starter-parent 1.3.0.M2 com.pyc transaction 0.0.1-SNAPSHOT transaction jar Demo project for Spring Boot UTF-8 1.8 com.oracle ojdbc6 11.2.0.2.0 org.springframework.boot spring-boot-starter-data-jpa org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.junit.vintage junit-vintage-engine org.springframework.boot spring-boot-maven-plugin
# 数据驱动 database driverspring.datasource.driverClassName=oracle.jdbc.OracleDriver# 数据源 data sourcespring.datasource.url=jdbc\:oracle\:thin\:@localhost\:1521\:xe# 数据库登录用户 database account and also name of the goal databasespring.datasource.username=boot# 相应用户的登录密码 password of the corresponding database accountspring.datasource.password=boot# auto maintain data sheet of goal databasespring.jpa.hibernate.ddl-auto=update# show sql in console windowspring.jpa.show-sql=truespring.jackson.serialization.indent-output=truedebug=truelogging.file=log.loglogging.level.org.springframework.web=DEBUG
//IntelliJ IDEA//transaction//PersonRepository//2020/2/11// Author:御承扬//E-mail:2923616405@qq.compackage com.pyc.transaction.dao;import com.pyc.transaction.domain.Person;import org.springframework.data.jpa.repository.JpaRepository;public interface PersonRepository extends JpaRepository{ }
package com.pyc.transaction.service;import com.pyc.transaction.domain.Person;public interface DemoService { public Person savePersonWithRollBack(Person person); public Person savePersonWithoutRollBack(Person person);}
package com.pyc.transaction.service.impl;import com.pyc.transaction.dao.PersonRepository;import com.pyc.transaction.domain.Person;import com.pyc.transaction.service.DemoService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Transactional;@Servicepublic class DemoServiceImpl implements DemoService { @Autowired PersonRepository personRepository; // 使用 @Transaction 注解的 rollback 属性,指定特定异常时,数据回滚 // comment @Transaction and use property rollback to assign transaction rollback // when happen illegal argument exception @Transactional(rollbackFor = { IllegalArgumentException.class}) public Person savePersonWithRollBack(Person person){ Person p = personRepository.save(person); if(person.getName().equals("林秋延")){ throw new IllegalArgumentException("林秋延已经存在,数据回滚"); } return p; } // comment @Transaction and use property rollback to assign transaction no rollback // when happen illegal argument exception @Transactional(noRollbackFor = { IllegalArgumentException.class}) public Person savePersonWithoutRollBack(Person person){ Person p = personRepository.save(person); if(person.getName().equals("pyc")){ throw new IllegalArgumentException("pyc 虽然已经存在,数据不会回滚"); } return p; }}
package com.pyc.transaction.web;import com.pyc.transaction.domain.Person;import com.pyc.transaction.service.DemoService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class WebController { @Autowired DemoService demoService; // 测试回滚 // try rollback transaction @RequestMapping("/rollback") public Person rollback(Person person){ return demoService.savePersonWithRollBack(person); } // trying no rollback transaction @RequestMapping("/norollback") public Person noRollback(Person person){ return demoService.savePersonWithoutRollBack(person); }}
转载地址:http://klqgn.baihongyu.com/