JAVA全系列 教程
3762个小节阅读:7089.6k
目录
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
xxxxxxxxxx
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.itbaizhan</groupId>
<artifactId>itbaizhan-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- dubbo和springboot整合依赖包 -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<!-- dubbo 注册中心nacos依赖包 -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
</dependency>
xxxxxxxxxx
server:
port: 8102
#====================================stream =============================================
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/seata-test?useSSL=false&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
username: root
password: 123456
application:
name: dubbo-account-example
#====================================Dubbo config===============================================
dubbo:
application:
id: dubbo-account-example
name: dubbo-account-example
qosEnable: false
protocol:
id: dubbo
name: dubbo
port: -1
registry:
id: dubbo-account-example-registry
address: nacos://192.168.47.100:8848
xxxxxxxxxx
@Slf4j
@SpringBootApplication
@MapperScan("com.itbaizhan.mapper")
@EnableDubbo
public class AccountExampleApplication {
public static void main(String[] args) {
SpringApplication.run(AccountExampleApplication.class,args);
log.info("********************* 账户服务启动成功 *************");
}
}
xxxxxxxxxx
CREATE TABLE `account_tbl` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` varchar(255) DEFAULT NULL,
`money` int(11) DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
xxxxxxxxxx
@Data
@TableName("account_tbl")
public class TAccount {
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
@TableField("user_id")
private String userId;
private Integer money;
}
TAccountMapper
接口xxxxxxxxxx
public interface TAccountMapper extends BaseMapper<TAccount> {
}
xxxxxxxxxx
/**
* 帐户服务接口
*/
public interface IAccountService {
/**
* 从用户账户中借出
*/
BaseResponse debit(String userId, Integer money);
}
xxxxxxxxxx
@DubboService
public class AccountDubboServiceImpl implements IAccountService {
@Autowired
private TAccountMapper tAccountMapper;
/**
* 扣用户钱
* @param userId
* @param money
*/
@Transactional
@Override
public BaseResponse debit(String userId, Integer money) {
LambdaQueryWrapper<TAccount> lambdaQueryWrapper = new LambdaQueryWrapper<TAccount>();
lambdaQueryWrapper.eq(TAccount::getUserId,userId);
TAccount tAccount = tAccountMapper.selectOne(lambdaQueryWrapper);
if (tAccount == null){
return BaseResponse.error("用户不存在");
}
if (tAccount.getMoney() < money){
return BaseResponse.error("账户余额不足");
}
LambdaUpdateWrapper<TAccount> luw = new LambdaUpdateWrapper<TAccount>();
luw.eq(TAccount::getUserId,userId);
luw.set(TAccount::getMoney,tAccount.getMoney() - money);
int update = tAccountMapper.update(null, luw);
if (update > 0){
return BaseResponse.success();
}else {
return BaseResponse.error("服务异常");
}
}
}