springboot開啓事務很簡單,只須要一個註解@Transactional 就能夠了。由於在springboot中已經默認對jpa、jdbc、mybatis開啓了事事務,引入它們依賴的時候,事物就默認開啓。固然,若是你須要用其餘的orm,好比beatlsql,就須要本身配置相關的事物管理器。java
以上一篇文章的代碼爲例子,即springboot整合mybatis,上一篇文章是基於註解來實現mybatis的數據訪問層,這篇文章基於xml的來實現,並開啓聲明式事務。mysql
在pom文件中引入mybatis啓動依賴:spring
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.0</version> </dependency>
引入mysql 依賴sql
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.29</version> </dependency>
-- create table `account` # DROP TABLE `account` IF EXISTS CREATE TABLE `account` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(20) NOT NULL, `money` double DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8; INSERT INTO `account` VALUES ('1', 'aaa', '1000'); INSERT INTO `account` VALUES ('2', 'bbb', '1000'); INSERT INTO `account` VALUES ('3', 'ccc', '1000');
spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver mybatis.mapper-locations=classpath*:mybatis/*Mapper.xml mybatis.type-aliases-package=com.forezp.entity
經過配置mybatis.mapper-locations來指明mapper的xml文件存放位置,我是放在resources/mybatis文件下的。mybatis.type-aliases-package來指明和數據庫映射的實體的所在包。數據庫
通過以上步驟,springboot就能夠經過mybatis訪問數據庫來。springboot
public class Account { private int id ; private String name ; private double money; getter.. setter.. }
Spring Cloud大型企業分佈式微服務雲架構源碼請加企鵝求求:一七九一七四三三八零mybatis