本文基于springboot2.0以上版本
springboot1.5 版本搭建
spring.datasource.basic.driverClassName = com.mysql.jdbc.Driver
spring.datasource.basic.url = jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf-8
spring.datasource.basic.username = root
spring.datasource.basic.password = 123456
springboot2.0以上版本
spring.datasource.basic.driver-class-name = com.mysql.jdbc.Driver
spring.datasource.basic.jdbc-url = jdbc:mysql://localhost:3306/test1
spring.datasource.basic.username = root
spring.datasource.basic.password = 123456
要不然会报错 jdbcUrl is required with driverClassName
数据源配置
package com.example.demo.datasource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
/**
* Created by zhaosiji on 2018/11/27.
*/
@Configuration
@MapperScan(basePackages = "com.example.demo.mapper.basic", sqlSessionTemplateRef = "basicSqlSessionTemplate")
public class BasicDataSourceConfig {
@Bean(name = "basicDataSource")
@ConfigurationProperties(prefix = "spring.datasource.basic")
@Primary
public DataSource testDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "basicSqlSessionFactory")
@Primary
public SqlSessionFactory testSqlSessionFactory(@Qualifier("basicDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/basic/*.xml"));
return bean.getObject();
}
@Bean(name = "basicTransactionManager")
@Primary
public DataSourceTransactionManager testTransactionManager(@Qualifier("basicDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "basicSqlSessionTemplate")
@Primary
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("basicSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
文件配置对应配置即可
增加事务处理
@Transactional(value = "basicTransactionManager")
* 事务处理代码
* @Transactional(value = "basicTransactionManager")
* @Transactional(propagation = Propagation.REQUIRED)
* REQUIRED:如果当前存在事务,则加入该事务;如果当前没有事务,则创建一个新的事务。
* SUPPORTS:如果当前存在事务,则加入该事务;如果当前没有事务,则以非事务的方式继续运行。
* MANDATORY:如果当前存在事务,则加入该事务;如果当前没有事务,则抛出异常。
* REQUIRES_NEW:创建一个新的事务,如果当前存在事务,则把当前事务挂起。
* NOT_SUPPORTED:以非事务方式运行,如果当前存在事务,则把当前事务挂起。
* NEVER:以非事务方式运行,如果当前存在事务,则抛出异常。
* NESTED:如果当前存在事务,则创建一个事务作为当前事务的嵌套事务来运行;
* 如果当前没有事务,则该取值等价于REQUIRED