1. 程式人生 > >spring boot集成mybatis+事務控制

spring boot集成mybatis+事務控制

package 數據源 action cti ase conn span hand enc

一下代碼為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+事務控制