mysql事务mybatis_springboot+mybatis+mysql事务

一、通过maven加载类库

spring-boot-parent

org.springframework.boot

2.1.16.RELEASE

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-jdbc

mysql

mysql-connector-java

8.0.21

org.mybatis.spring.boot

mybatis-spring-boot-starter

1.3.2

org.projectlombok

lombok

1.18.12

provided

org.springframework.boot

spring-boot-maven-plugin

二、建立目录结构

5430fd8d03e959286a387ef11b998970.png

三、修改配置

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring.datasource.url=jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf-8&useLegacyDatetimeCode=false&serverTimezone=UTC

spring.datasource.username=root

spring.datasource.password=111111

mybatis.type-aliases-package=com.aliyun.edu.dxh

mybatis.mapper-locations=classpath*:mappers/*.xml

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

4.1 控制器:

@RestController

@RequestMapping("/student")

public class StudentController {

@Autowired

private StudentService studentService;

@RequestMapping("/detail")

public Student detail(@RequestParam("id") Long id) {

return studentService.detail(id);

}

@RequestMapping("/modify")

public int modify() {

return studentService.modifyStudentAddress(1L, "test-4", 1L, "chinese-4");

}

}

4.2 编写service

4.2.1 service的接口层

public interface StudentService {

public Student detail(Long id);

int modifyStudentAddress(Long studentId, String address, Long sourceId, String sourceName);

}

4.2.2 service的实现层

@Service

public class StudentServiceImpl implements StudentService {

@Resource

private StudentMapper studentMapper;

@Resource

private SourceMapper sourceMapper;

public Student detail(Long id) {

return studentMapper.detail(id);

}

@Transactional

public int modifyStudentAddress(Long studentId, String address, Long sourceId, String sourceName) {

studentMapper.modifyStudentAddress(studentId, address);

int a = 1/0;

sourceMapper.modifySource(sourceId, sourceName);

return 1;

}

}

4.3 mapper层

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

@Mapper

public interface SourceMapper {

public int modifySource(@Param("sourceId") Long sourceId, @Param("sourceName") String sourceName);

}

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

@Mapper

public interface StudentMapper {

public Student detail(@Param("studentId") Long id);

public int modifyStudentAddress(@Param("studentId") Long id, @Param("address") String address);

}

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

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

@Data

public class Source {

private Long id;

private String sourceName;

private String isDelete;

}

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

@Data

public class Student {

private Long id;

private String name;

private String sex;

private String address;

}

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

4.5.1 SourceMapper.xml

update source

set source_name=#{sourceName}

where id=#{sourceId}

4.5.2 StudentMapper.xml

select id,name,sex,address

from student

where id = #{studentId}

update student

set address=#{address}

where id=#{studentId}

五、主应用类

@SpringBootApplication

//@EnableTransactionManagement

public class SpringbootMybatisTx {

public static void main(String[] args) {

SpringApplication.run(SpringbootMybatisTx.class, args);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值