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

聚诚商务做网站多少钱网络培训证书

聚诚商务做网站多少钱,网络培训证书,网页设计与网站建设主要内容,网站开发费用怎么账务处理目录 1. 添加依赖 2. 配置数据库连接 3. 启用事务管理 4. 创建实体类和存储库 5. 创建服务类并使用Transactional注解 6. 编写测试用例 7. 运行应用程序 在Springboot中开启数据库的事务的应用开发过程中非常重要的业务,以下是一个使用MySQL数据库&#xff0…

目录

1. 添加依赖

2. 配置数据库连接

3. 启用事务管理

4. 创建实体类和存储库

5. 创建服务类并使用@Transactional注解

6. 编写测试用例

7. 运行应用程序


在Springboot中开启数据库的事务的应用开发过程中非常重要的业务,以下是一个使用MySQL数据库,并在方法上开启事务管理的完整Spring Boot示例。

这个示例将展示如何配置Spring Boot应用程序以连接MySQL数据库,并在服务层的方法上使用@Transactional注解来管理事务。

1. 添加依赖

pom.xml文件中添加必要的依赖,包括Spring Data JPA和MySQL驱动程序:

<dependencies><!-- Spring Data JPA --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><!-- MySQL Driver --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency><!-- Other dependencies... -->
</dependencies>

2. 配置数据库连接

application.propertiesapplication.yml文件中配置MySQL数据库连接信息。

application.properties:

spring.datasource.url=jdbc:mysql://localhost:3306/testdb
spring.datasource.username=root
spring.datasource.password=yourpassword
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect

application.yml:

spring:datasource:url: jdbc:mysql://localhost:3306/testdbusername: rootpassword: yourpassworddriver-class-name: com.mysql.cj.jdbc.Driverjpa:hibernate:ddl-auto: updatedatabase-platform: org.hibernate.dialect.MySQLDialect

3. 启用事务管理

在你的Spring Boot主类或者任何配置类上添加@EnableTransactionManagement注解:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.transaction.annotation.EnableTransactionManagement;@SpringBootApplication
@EnableTransactionManagement
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}
}

4. 创建实体类和存储库

创建一个简单的用户实体类和对应的存储库接口。

User.java:

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;@Entity
public class User {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Long id;private String name;// Getters and setterspublic Long getId() {return id;}public void setId(Long id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}
}

UserRepository.java:

import org.springframework.data.jpa.repository.JpaRepository;public interface UserRepository extends JpaRepository<User, Long> {
}

5. 创建服务类并使用@Transactional注解

在服务层的方法上使用@Transactional注解来管理事务。

UserService.java:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;@Service
public class UserService {@Autowiredprivate UserRepository userRepository;@Transactionalpublic void saveUser(User user) {userRepository.save(user);// 模拟可能抛出异常的操作if (true) {throw new RuntimeException("Simulated Exception");}}
}

6. 编写测试用例

编写一个测试用例来验证事务管理是否正常工作。

UserServiceTest.java:

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.transaction.TransactionSystemException;import static org.junit.jupiter.api.Assertions.assertThrows;@SpringBootTest
public class UserServiceTest {@Autowiredprivate UserService userService;@Testpublic void testTransaction() {User user = new User();user.setName("John");assertThrows(TransactionSystemException.class, () -> {userService.saveUser(user);});}
}

7. 运行应用程序

确保MySQL数据库正在运行,并且数据库testdb已创建。然后运行Spring Boot应用程序,测试用例应该能够验证事务管理是否按预期工作。

通过这些步骤,你可以在Spring Boot应用程序中使用MySQL数据库,并在方法上使用@Transactional注解来管理事务。这样可以确保数据库操作在事务范围内执行,保证数据的一致性和完整性。

http://www.yayakq.cn/news/531347/

相关文章:

  • 动漫电影做英语教学视频网站搭积木建网站软件
  • 内网网站开发报价苏州网站开发建设方案
  • 建网站多少钱一个平台商城网站建设定制网站建设
  • 网站关键词排名批量查询在线做app的网站
  • 临时展厅搭建方案蒙阴网站优化
  • 国外好的网页设计汕头seo推广外包
  • 做网站后台服务器什么最好表情包在线制作网站
  • 做外贸需要做个英文网站吗情侣wordpress模板下载
  • 创建一个个人网站交互设计产品有哪些
  • 技术支持 东莞网站建设鞋子聊城做网站的公司价位
  • 建设工程资质证书二维码扫描网站网站建设 化工
  • 哪个网站可以做创意短视频明月 WordPress
  • 中国电子系统建设公司网站chplayer wordpress
  • 电商网站项目建设潍坊网络推广网站建设
  • 茂名高端模板建站东莞网站托管
  • 建设数据库搜索网站wordpress是英文版
  • win7本地做网站中文网站开发
  • 网站建设链接怎么加上去西安网站建设公司云网
  • 衡阳市建设局网站中国采购网招标公告
  • 网页设计怎么建站点网站收录目录源码
  • 网站上的文章做参考文献网站建设个人先进材料
  • 请问网上有没有比较好的网站可以做照片书的呀?要求质量比较好的!网页加速器手机版哪个好
  • 语音网站怎么做6网站建设做网站
  • 百度网站怎么建设《网站建设方案》
  • 网站开发相关期刊c2c电子商务平台举例
  • 常德做网站的公司怎么免费建公司网站
  • vps 网站发布知名网站名称
  • 网站模板去哪下载关于网络营销的方法
  • 我的手机网站网站建设教程 迅雷下载
  • 哈尔滨建站系统wordpress menu_walker