当前位置:   article > 正文

springboot mysql 事务_springboot整合mysql开启事务

springboot整合mysql开启事务

一、通过maven加载类库

  1. <parent>
  2. <artifactId>spring-boot-parent</artifactId>
  3. <groupId>org.springframework.boot</groupId>
  4. <version>2.1.16.RELEASE</version>
  5. </parent>
  6. <dependencies>
  7. <dependency>
  8. <groupId>org.springframework.boot</groupId>
  9. <artifactId>spring-boot-starter-web</artifactId>
  10. </dependency>
  11. <dependency>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-jdbc</artifactId>
  14. </dependency>
  15. <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
  16. <dependency>
  17. <groupId>mysql</groupId>
  18. <artifactId>mysql-connector-java</artifactId>
  19. <version>8.0.21</version>
  20. </dependency>
  21. <dependency>
  22. <groupId>org.mybatis.spring.boot</groupId>
  23. <artifactId>mybatis-spring-boot-starter</artifactId>
  24. <version>1.3.2</version>
  25. </dependency>
  26. <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
  27. <dependency>
  28. <groupId>org.projectlombok</groupId>
  29. <artifactId>lombok</artifactId>
  30. <version>1.18.12</version>
  31. <scope>provided</scope>
  32. </dependency>
  33. </dependencies>
  34. <build>
  35. <plugins>
  36. <plugin>
  37. <groupId>org.springframework.boot</groupId>
  38. <artifactId>spring-boot-maven-plugin</artifactId>
  39. </plugin>
  40. </plugins>
  41. </build>

二、建立目录结构

三、修改配置

  1. spring.datasource.driver-class-name=com.mysql.jdbc.Driver
  2. spring.datasource.url=jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf-8&useLegacyDatetimeCode=false&serverTimezone=UTC
  3. spring.datasource.username=root
  4. spring.datasource.password=111111
  5. mybatis.type-aliases-package=com.aliyun.edu.dxh
  6. mybatis.mapper-locations=classpath*:mappers/*.xml

四、编写控制器、业务、PO、mapper、xml:

4.1 控制器:

  1. @RestController
  2. @RequestMapping("/student")
  3. public class StudentController {
  4. @Autowired
  5. private StudentService studentService;
  6. @RequestMapping("/detail")
  7. public Student detail(@RequestParam("id") Long id) {
  8. return studentService.detail(id);
  9. }
  10. @RequestMapping("/modify")
  11. public int modify() {
  12. return studentService.modifyStudentAddress(1L, "test-4", 1L, "chinese-4");
  13. }
  14. }

4.2 编写service

4.2.1 service的接口层

  1. public interface StudentService {
  2. public Student detail(Long id);
  3. int modifyStudentAddress(Long studentId, String address, Long sourceId, String sourceName);
  4. }

4.2.2 service的实现层

  1. @Service
  2. public class StudentServiceImpl implements StudentService {
  3. @Resource
  4. private StudentMapper studentMapper;
  5. @Resource
  6. private SourceMapper sourceMapper;
  7. public Student detail(Long id) {
  8. return studentMapper.detail(id);
  9. }
  10. @Transactional
  11. public int modifyStudentAddress(Long studentId, String address, Long sourceId, String sourceName) {
  12. studentMapper.modifyStudentAddress(studentId, address);
  13. int a = 1/0;
  14. sourceMapper.modifySource(sourceId, sourceName);
  15. return 1;
  16. }
  17. }

4.3 mapper层

4.3.1 SourceMapper.java (课程表对应的mapper接口)

  1. @Mapper
  2. public interface SourceMapper {
  3. public int modifySource(@Param("sourceId") Long sourceId, @Param("sourceName") String sourceName);
  4. }

4.3.2 StudentMapper.java (学生信息表对应的mapper接口)

  1. @Mapper
  2. public interface StudentMapper {
  3. public Student detail(@Param("studentId") Long id);
  4. public int modifyStudentAddress(@Param("studentId") Long id, @Param("address") String address);
  5. }

4.4 po(持久化对象:表对应的实体类)

4.4.1 Source.java (课程表对应的实体类)

  1. @Data
  2. public class Source {
  3. private Long id;
  4. private String sourceName;
  5. private String isDelete;
  6. }

4.4.2 Student.java(学生表对应的实体类)

  1. @Data
  2. public class Student {
  3. private Long id;
  4. private String name;
  5. private String sex;
  6. private String address;
  7. }

4.5、数据库操作对应的XML(resources/mappers)

4.5.1 SourceMapper.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
  3. <mapper namespace="com.aliyun.edu.dxh.mapper.SourceMapper">
  4. <resultMap id="student" type="com.aliyun.edu.dxh.po.Source">
  5. <id column="id" jdbcType="BIGINT" property="id"/>
  6. <id column="source_name" jdbcType="VARCHAR" property="sourceName"/>
  7. <id column="is_delete" jdbcType="VARCHAR" property="isDelete"/>
  8. </resultMap>
  9. <update id="modifySource">
  10. update source
  11. set source_name=#{sourceName}
  12. where id=#{sourceId}
  13. </update>
  14. </mapper>

4.5.2 StudentMapper.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
  3. <mapper namespace="com.aliyun.edu.dxh.mapper.StudentMapper">
  4. <resultMap id="student" type="com.aliyun.edu.dxh.po.Student">
  5. <id column="id" jdbcType="BIGINT" property="id"/>
  6. <id column="name" jdbcType="VARCHAR" property="name"/>
  7. <id column="sex" jdbcType="VARCHAR" property="sex"/>
  8. <id column="address" jdbcType="VARCHAR" property="address"/>
  9. </resultMap>
  10. <select id="detail" resultType="student">
  11. select id,name,sex,address
  12. from student
  13. where id = #{studentId}
  14. </select>
  15. <update id="modifyStudentAddress">
  16. update student
  17. set address=#{address}
  18. where id=#{studentId}
  19. </update>
  20. </mapper>

五、主应用类

  1. @SpringBootApplication
  2. //@EnableTransactionManagement
  3. public class SpringbootMybatisTx {
  4. public static void main(String[] args) {
  5. SpringApplication.run(SpringbootMybatisTx.class, args);
  6. }
  7. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/165665
推荐阅读
相关标签
  

闽ICP备14008679号