Commit 8ca8845d by 温金柱

feat:增加注解失效的测试案例

parent c41b631c
import leader.CompanyAudit;
import leader.Leader;
public class HelloWorld {
public static void main(String[] args) {
Leader leader = new CompanyAudit();
leader.audit("", 1);
}
}
package MonthCard;
import java.util.Objects;
public class MonthCardRequest {
// 月卡开始时间
String startTime;
// 月卡结束时间
String endTime;
// 月卡申请人
String name;
public String getStartTime() {
return startTime;
}
public void setStartTime(String startTime) {
this.startTime = startTime;
}
public String getEndTime() {
return endTime;
}
public void setEndTime(String endTime) {
this.endTime = endTime;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MonthCardRequest that = (MonthCardRequest) o;
return Objects.equals(startTime, that.startTime) &&
Objects.equals(endTime, that.endTime) &&
Objects.equals(name, that.name);
}
@Override
public int hashCode() {
return Objects.hash(startTime, endTime, name);
}
}
package leader;
public class CompanyAudit extends Leader {
@Override
public void audit(String applyUuid, Integer auditStatus) {
// 0-待审核 1-通过 2-拒绝 3-驳回
if (auditStatus == 1){
System.out.println("您的申请在一级审核被通过");
}else if(auditStatus == 2){
System.out.println("您的申请已经被拒绝");
}
this.setNextLeader(new CompanyGroupAudit());
this.nextLeader.audit(applyUuid, auditStatus);
}
}
package leader;
public class CompanyGroupAudit extends Leader {
@Override
public void audit(String applyUuid, Integer auditStatus) {
// 0-待审核 1-通过 2-拒绝 3-驳回
if (auditStatus == 1){
System.out.println("您的申请在二级审核被通过");
}else if(auditStatus == 2){
System.out.println("您的申请在二级已经被拒绝");
}
this.setNextLeader(new MCTAudit());
this.nextLeader.audit(applyUuid, auditStatus);
}
}
package leader;
public abstract class Leader {
// 执行对象
protected Leader nextLeader;
public abstract void audit(String applyUuid, Integer auditStatus);
public Leader getNextLeader() {
return nextLeader;
}
public void setNextLeader(Leader nextLeader) {
this.nextLeader = nextLeader;
}
}
package leader;
public class MCTAudit extends Leader {
@Override
public void audit(String applyUuid, Integer auditStatus) {
// 0-待审核 1-通过 2-拒绝 3-驳回
if (auditStatus == 1){
System.out.println("您的申请在已通过");
}else if(auditStatus == 2){
System.out.println("您的申请已被拒绝");
}
}
}
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.Scheduler;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.internal.schedulers.SchedulerWhen;
import javax.annotation.Resource;
public class HelloRX{
public static void main(String[] args) {
Observable<UserMessage> observable = new Observable<UserMessage>() {
@Override
protected void subscribeActual(Observer<? super UserMessage> observer) {
UserMessage us1 = new UserMessage("zhangSan","hello");
UserMessage us2 = new UserMessage("liSi","rxJava");
UserMessage us3 = new UserMessage("wangWu","end");
observer.onNext(us1);
observer.onNext(us2);
observer.onNext(us3);
}
};
Consumer<UserMessage> consumer = new Consumer<UserMessage>() {
public void accept(UserMessage userMessage) throws Exception {
String message = userMessage.getMessage();
String userName = userMessage.getUserName();
System.out.println("用户:"+userName+",说了一句:"+message);
}
};
System.out.println("-----------------------------");
Test<UserMessage> test = new Test<UserMessage>() {
@Override
protected String test1(Test2<? super UserMessage> var) {
UserMessage userMessage = new UserMessage("wjz","wjz");
return var.sayHello(userMessage);
}
};
System.out.println(test.test1(new Test2<UserMessage>()));
}
}
public class MassageMain {
private static void log(Integer level, MessageBuilder messageBuilder, UserMessage userMessage){
if(level == 1){
System.out.println(messageBuilder.buildMessage(userMessage));
}
}
public static void main(String[] args) {
UserMessage um = new UserMessage();
um.setMessage("1");
log(1, (var) -> {
String message = var.getMessage();
if(message.equals("1")){
return "好厉害";
}
return message;
}, um);
}
}
@FunctionalInterface
public interface MessageBuilder {
String buildMessage(UserMessage um);
}
import io.reactivex.Observer;
public abstract class Test<T> {
protected abstract String test1(Test2<? super T> var);
}
public class Test2<T> {
public String sayHello(T var){
return "HHHHHH";
}
}
public class UserMessage {
private String userName;
private String message;
public UserMessage(String userName, String message) {
this.userName = userName;
this.message = message;
}
public UserMessage() {
super();
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
# Default ignored files
/workspace.xml
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="transactional" />
</profile>
</annotationProcessing>
</component>
<component name="JavacSettings">
<option name="ADDITIONAL_OPTIONS_OVERRIDE">
<module name="transactional" options="-parameters" />
</option>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>icu.wjz</groupId>
<artifactId>transactional</artifactId>
<version>1.0-SNAPSHOT</version>
<name>transactional</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.28</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludeDevtools>false</excludeDevtools>
</configuration>
</plugin>
</plugins>
</build>
</project>
package icu.wjz;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
/**
* Hello world!
*
*/
@SpringBootApplication
@ComponentScan(basePackages = {"icu.wjz.controller","icu.wjz.service"})
@MapperScan(basePackages = {"icu.wjz.mapper"})
public class App {
public static void main( String[] args ) {
SpringApplication.run(App.class);
}
}
package icu.wjz.controller;
import icu.wjz.service.UserService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
public class UserController {
@Resource
private UserService userService;
@GetMapping("syn1")
public ResponseEntity<Object> test(){
return userService.syn1("诸葛大力");
}
}
package icu.wjz.entity.dto.user;
import lombok.Data;
@Data
public class UserDTO {
// 姓名
private String name;
// 年纪
private Integer age;
}
package icu.wjz.entity.po.user;
import lombok.Data;
@Data
public class UserPO {
// 姓名
private Integer id;
// 姓名
private String name;
// 年纪
private Integer age;
}
package icu.wjz.mapper;
import icu.wjz.entity.dto.user.UserDTO;
import icu.wjz.entity.po.user.UserPO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
@Mapper
@Repository
public interface UserMapper {
// 增加一个用户
int insert(UserDTO body);
UserPO select(@Param("name") String name);
// 这是一条错误的SQL语句
int errorInsert(UserDTO body);
}
package icu.wjz.service;
import icu.wjz.entity.dto.user.UserDTO;
import org.springframework.http.ResponseEntity;
public interface UserService {
ResponseEntity<Object> a1(UserDTO body);
ResponseEntity<Object> a2(UserDTO body);
ResponseEntity<Object> a3(UserDTO body);
ResponseEntity<Object> a4(UserDTO body);
ResponseEntity<Object> a5(UserDTO body);
ResponseEntity<Object> b3(UserDTO body);
ResponseEntity<Object> syn1(String name);
}
package icu.wjz.service.impl;
import icu.wjz.entity.dto.user.UserDTO;
import icu.wjz.entity.po.user.UserPO;
import icu.wjz.mapper.UserMapper;
import icu.wjz.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.binding.BindingException;
import org.springframework.aop.framework.AopContext;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionTemplate;
import javax.annotation.Resource;
import java.util.Objects;
import java.util.Optional;
@Slf4j
@Service
@Transactional
public class UserServiceImpl implements UserService {
@Resource
private UserServiceImpl userService;
@Resource
private UserMapper userMapper;
@Override
@Transactional
public ResponseEntity<Object> a1(UserDTO body) {
b1(body);
return null;
}
private void b1(UserDTO body){
/**
* 此时b1中SQL是会被a1开启的事物所管理
* 如果b1()抛出异常,则a1中执行的变更也会被回退。
* 但如果抛出的不是RuntimeException 则事务不会回退。
* 下面会解释。
*/
userMapper.insert(body);
userMapper.errorInsert(body);
}
@Override
public ResponseEntity<Object> a2(UserDTO body) {
b2(body);
return null;
}
@Transactional
private void b2(UserDTO body){
/**
* 此时b2虽然打了@Transactional标签 但事务仍然不会启用
* 原因:@Transactional 无法作用在私有方法上(private)
*/
userMapper.insert(body);
userMapper.errorInsert(body);
}
@Override
public ResponseEntity<Object> a3(UserDTO body) {
b3(body);
return null;
}
@Override
@Transactional
public ResponseEntity<Object> b3(UserDTO body) {
/**
* 此时b2虽然打了@Transactional标签,并且为public 但事务仍然不会启用
* 原因:默认情况下,只有来自外部的方法调用才会被aOP代理所捕获,
* 也就是类内部调用本类方法,并不会引起事务行为,即使加了@Transactional
*
* 补充:如果想要事务生效需要使用上面注释调用方式
*/
userMapper.insert(body);
userMapper.errorInsert(body);
return null;
}
@Override
@Transactional(rollbackFor = Exception.class)
public ResponseEntity<Object> a4(UserDTO body) {
b4(body); // 事务只会处理未被处理的异常
return null;
}
public void b4(UserDTO body) {
/**
* 此时a4正常开启事务, 但是如果b4方法抛出Exception异常 事务将不会回退
* @Transactional 默认情况下只会回退继承自RuntimeException的异常
* 如果想回退Exception, 需要设置
* @Transactional(rollbackFor = Exception.class)
* 此时抛出Exception以及子类的异常,事务都将会回退
*
* 补充:抛出Error错误时,事务都将回退。
*/
userMapper.insert(body);
userMapper.errorInsert(body);
}
@Override
public ResponseEntity<Object> a5(UserDTO body) {
userService.b3(body); //使用容器对象调用。
return null;
}
@Override
@Transactional
public ResponseEntity<Object> syn1(String name) {
UserPO user = userMapper.select(name);
if(Objects.isNull(user)){
log.info("这个用户不存在");
synchronized (user.getName().intern()){
UserDTO body = new UserDTO();
body.setName("诸葛大力");
body.setAge(22);
userMapper.insert(body);
}
}
return new ResponseEntity<Object>("执行完成", HttpStatus.OK);
}
}
package icu.wjz.util;
import icu.wjz.mapper.UserMapper;
import icu.wjz.service.UserService;
import lombok.Data;
import org.springframework.context.annotation.ComponentScan;
@ComponentScan
@Data
public class UserThread implements Runnable {
private UserService userService;
@Override
public void run() {
userService.syn1("诸葛大力");
}
}
server:
port: 8080
tomcat:
uri-encoding: UTF-8
servlet:
context-path: /v1
#spring配置
spring:
#mysql配置
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://rm-wz9rcaor575hp790oqo.mysql.rds.aliyuncs.com:3306/wjz?useUnicode=true&characterEncoding=UTF-8&useSSL=false&tinyInt1isBit=false&useOldAliasMetadataBehavior=true&testOnBorrow=true&serverTimezone=Asia/Shanghai
username: wjz
password: wjzmysql1314
defaultAutoCommit: false
connectionInitSqls: set names utf8mb4;
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 600000
testWhileIdle: true
validationQuery: SELECT 1 FROM DUAL
initialSize: 10
maxActive: 10
minIdle: 0
autoReconnect: true
mybatis:
mapper-locations: classpath:mapper/*.xml
configuration:
map-underscore-to-camel-case: true
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
<?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="icu.wjz.mapper.UserMapper">
<insert id="insert" parameterType="icu.wjz.entity.dto.user.UserDTO">
insert into user
( `name`, age)
values
(#{name}, #{age})
</insert>
<select id="select" parameterType="string" resultType="icu.wjz.entity.po.user.UserPO">
select
id,
`name`,
age
from
user
where
name = #{name}
limit 1
</select>
<insert id="errorInsert" parameterType="icu.wjz.entity.dto.user.UserDTO">
insert into user
(`name`, age1)
values
(#{name}, #{age})
</insert>
</mapper>
package icu.wjz;
import static org.junit.Assert.assertTrue;
import icu.wjz.entity.dto.user.UserDTO;
import icu.wjz.service.UserService;
import icu.wjz.util.UserThread;
import org.apache.catalina.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionTemplate;
import javax.annotation.Resource;
@SpringBootTest(classes = App.class)
@RunWith(SpringRunner.class)
public class AppTest {
@Autowired
private UserService userService;
@Resource
private TransactionTemplate transactionTemplate;
@Test
public void test1(){
UserDTO user = new UserDTO();
user.setAge(11);
user.setName("wangsichu1");
userService.a1(user);
}
@Test
public void test2(){
UserDTO user = new UserDTO();
user.setAge(12);
user.setName("wangsichu2");
userService.a2(user);
}
@Test
public void test3(){
UserDTO user = new UserDTO();
user.setAge(13);
user.setName("wangsichu3");
userService.a3(user);
}
@Test
public void test4(){
UserDTO user = new UserDTO();
user.setAge(14);
user.setName("wangsichu4");
userService.a4(user);
}
@Test
public void test5(){
UserDTO user = new UserDTO();
user.setAge(15);
user.setName("wangsichu5");
userService.a5(user);
}
@Test
public void synTest1(){
for(int i = 0; i< 3; i++){
UserThread userThread = new UserThread();
userThread.setUserService(userService);
Thread thread = new Thread(userThread);
thread.start();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Test
public void synTest2(){
int j = 0;
for(int i = 0; i < 1000; i++){
synchronized ("aaa"){
j += 1;
}
}
System.out.println("计算后的值为:"+ j);
}
@Test
public void t1(){
}
}
server:
port: 8080
tomcat:
uri-encoding: UTF-8
servlet:
context-path: /v1
#spring配置
spring:
#mysql配置
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://rm-wz9rcaor575hp790oqo.mysql.rds.aliyuncs.com:3306/wjz?useUnicode=true&characterEncoding=UTF-8&useSSL=false&tinyInt1isBit=false&useOldAliasMetadataBehavior=true&testOnBorrow=true&serverTimezone=Asia/Shanghai
username: wjz
password: wjzmysql1314
defaultAutoCommit: false
connectionInitSqls: set names utf8mb4;
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 600000
testWhileIdle: true
validationQuery: SELECT 1 FROM DUAL
initialSize: 10
maxActive: 10
minIdle: 0
autoReconnect: true
mybatis:
mapper-locations: classpath:mapper/*.xml
configuration:
map-underscore-to-camel-case: true
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
<?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="icu.wjz.mapper.UserMapper">
<insert id="insert" parameterType="icu.wjz.entity.dto.user.UserDTO">
insert into user
( `name`, age)
values
(#{name}, #{age})
</insert>
<select id="select" parameterType="string" resultType="icu.wjz.entity.po.user.UserPO">
select
id,
`name`,
age
from
user
where
name = #{name}
limit 1
</select>
<insert id="errorInsert" parameterType="icu.wjz.entity.dto.user.UserDTO">
insert into user
(`name`, age1)
values
(#{name}, #{age})
</insert>
</mapper>
package icu.wjz;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Run {
public static void main(String[] args) {
System.out.println("Hello World");
SpringApplication.run(Run.class);
}
}
package icu.wjz.entity;
import lombok.Data;
@Data
public class Wjz {
// 姓名
private String name;
// 年纪
private short age;
}
#spring配置
spring:
#mysql配置
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://rm-wz9z40fs394wti63aio.mysql.rds.aliyuncs.com:3306/ketong?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&tinyInt1isBit=false&useOldAliasMetadataBehavior=true&testOnBorrow=true
#url: jdbc:mysql://localhost:3306/ketong?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&tinyInt1isBit=false&useOldAliasMetadataBehavior=true&testOnBorrow=true
username: dankal_master
password: Dankal_system
defaultAutoCommit: false
connectionInitSqls: set names utf8mb4;
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 600000
testWhileIdle: true
validationQuery: SELECT 1 FROM DUAL
initialSize: 5
maxActive: 50
minIdle: 0
autoReconnect: true
logging:
level:
root: DEBUG
com:
github:
javarunfast:
mbye: TRACE
spring:
profiles:
active: dev
devtools:
remote:
secret: test
debug:
local-port: 14001
restart:
enabled: true
freemarker:
checkTemplateLocation: false
mybatis:
check_config_location: true
config-locations: classpath:mybatis/mybatis-config.xml
mapper-locations: classpath*:/mappers/*.xml
type-aliases-package: com.github.javarunfast.mbye.domain
scripting-language-driver:
freemarker:
template-file:
base-dir: mappers/
path-provider:
includes-package-path: false
separate-directory-per-mapper: false
mbye:
jwt:
secret: bGsyMzRqbGs4MDIzNGxzZOWPr+i/nuaOpeWFi+mHjOaWr+acteWkq2lzb2Zpb3MyM3U4NDMybmRzZGZzb2tqampza2xmanNsayVeJl4mJSQjJCQlIzgzIDEyPTEyeTN1aXV5Jl4=
expire: 1800
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment