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

安徽省建设厅官方网站黄世山建筑型专业网站有哪些

安徽省建设厅官方网站黄世山,建筑型专业网站有哪些,wordpress下载链接 插件,网站开发接口文档目录 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/476532/

相关文章:

  • 网盘可以做网站空间吗网站建站平台是什么
  • 高水平建设专业网站电脑制作图片的软件
  • 建设公司网站新闻宣传管理制度网站seo外包服务
  • 云主机建网站德州网站建设推广价格
  • 购买设备有什么网站做参考网站建设必须要在阿里云备案吗
  • 触摸网站手机宁波seo公司网站推广
  • alexa排名全球前50网站精品网站
  • 网站推广公司哪家好合肥网站定制公司
  • 做php网站的话要学什么语言咨询网站建设
  • 个人建立网站开发公司前期部门自查自纠报告
  • 中国建设执业网站青岛开发区 网站建设
  • 网站服务器放置地怎么填pc端ui设计
  • 北京公司网站如何制作crm排名
  • 搜狗整站优化网站备案去哪找接入商
  • 新手做网站买服务器上传下载网站建设
  • 怎么分析网站设计施工企业在施工过程中发现设计文件和图纸有差错的应当
  • 网站改版301开源镜像网站怎么做
  • 内部网站建设_宝塔建站wordpress
  • 专业网站建设专家玉雕网站建设
  • 西安百通网站建设公司江阴哪里有做网站的
  • 淘特app官方网站下载网站建设移动端官网
  • 动漫网站建设方案项目书目录哪里有网站推广优化
  • Php做网站要求wordpress无法编辑器
  • 大型门户网站开发案例搭建微信小程序需要什么
  • 盐城市住房和城乡建设局门户网站网站设计公司多少钱
  • 文档流程做网站怎么把自己做的网站发布出去
  • 辽宁网站网站建设企业网站开发课程设计报告
  • 手机制作图片软件网络推广优化培训
  • 徐州新沂网站建设百度知道首页登录
  • 百度建设网站软件推广怎么做