本文共 2447 字,大约阅读时间需要 8 分钟。
其实,我们引入jdbc或者web的启动器,就已经引入事务相关的依赖及默认配置了
至于事务,SpringBoot中通过注解来控制。就是我们熟知的@Transactional
@Servicepublic class UserService { @Autowired private UserMapper userMapper; public User queryById(Long id){ return this.userMapper.selectByPrimaryKey(id); } @Transactional public void deleteById(Long id){ this.userMapper.deleteByPrimaryKey(id); }}
在UserController中添加测试方法,内容:
@RestController@RequestMapping("user")public class UserController { @Autowired private UserService userService; @GetMapping("{id}") public User queryUserById(@PathVariable("id")Long id){ return this.userService.queryById(id); } @GetMapping("hello") public String test(){ return "hello ssm"; }}
完整的pom.xml:
4.0.0 cn.learn.user learn-user 1.0.0-SNAPSHOT org.springframework.boot spring-boot-starter-parent 2.0.6.RELEASE org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-jdbc mysql mysql-connector-java org.mybatis.spring.boot mybatis-spring-boot-starter 1.3.2 tk.mybatis mapper-spring-boot-starter 2.0.2
完整的application.properties:
# 映射端口server.port=8888# 设置org.springframework包的日志级别为debug#logging.level.org.springframework=debug# 连接四大参数spring.datasource.url=jdbc:mysql://localhost:3306/mybatisdbspring.datasource.username=rootspring.datasource.password=123456# 可省略,SpringBoot自动推断spring.datasource.driverClassName=com.mysql.jdbc.Driverspring.datasource.hikari.idle-timeout=60000spring.datasource.hikari.maximum-pool-size=30spring.datasource.hikari.minimum-idle=10# mybatis 别名扫描mybatis.type-aliases-package=cn.learn.user.pojo# mapper.xml文件位置,如果没有映射文件,请注释掉#mybatis.mapper-locations=classpath:mybatis/mappers/**/*.xml
转载地址:http://ykuzb.baihongyu.com/