spring boot集成mybatis+事务控制
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring boot集成mybatis+事务控制相关的知识,希望对你有一定的参考价值。
一下代码为DEMO演示,采用注解的方式完成Spring boot和Mybatis的集成,并进行事物的控制
数据源的配置:
1 spring.datasource.url=jdbc:mysql://localhost:3306/book 2 spring.datasource.username=root 3 spring.datasource.password= 4 spring.datasource.driver-class-name=com.mysql.jdbc.Driver
依赖包:
1 <dependency> 2 <groupId>mysql</groupId> 3 <artifactId>mysql-connector-java</artifactId> 4 </dependency> 5 <dependency> 6 <groupId>org.mybatis.spring.boot</groupId> 7 <artifactId>mybatis-spring-boot-starter</artifactId> 8 <version>1.0.0</version> 9 </dependency>
Mapper:
@Configuration public interface BookMapper { @Update("update books set publicsher =#{publicsher} where bookid = #{id}") public int update(@Param("publicsher")String publicsher,@Param("id")int id); }
Service:
@Service public class BookServiceImpl implements BookService { @Autowired private BookMapper bookMapper; @Transactional public void update(String publicsher , int bookId) { bookMapper.update(publicsher,bookId); } }
Controller:
@RestController public class BookController { @Autowired private BookService bookService; @RequestMapping("/updataBook") public String updataBook(String name,int id) { bookService.update(name,id); return "updata Success"; } }
App:
@ComponentScan(basePackages = { "com.szzt.controller" , "com.szzt.service","com.szzt.exceptionHandler"}) @MapperScan(basePackages="com.szzt.mapper") @EnableAutoConfiguration public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }
以上是关于spring boot集成mybatis+事务控制的主要内容,如果未能解决你的问题,请参考以下文章