代码如下:
import com.alibaba.druid.pool.xa.DruidXADataSource;
import org.apache.ibatis.logging.stdout.StdOutImpl;
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.jta.atomikos.AtomikosDataSourceBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import javax.sql.DataSource;
/**
* mysql数据源
* @author yumeng
* @createTime 2022/04/21/ 09:20
* @Description:
*/
@Configuration
@MapperScan(basePackages = {"扫包路径"}, sqlSessionTemplateRef = "mysqlSqlSessionTemplate")
public class MySqlDBConfig {
@Bean(name = "mysqlDataSource")
public DataSource dataSourceCar(@Qualifier("mysqlProperty") DruidXADataSource druidXADataSource) {
AtomikosDataSourceBean xaDataSource = new AtomikosDataSourceBean();
xaDataSource.setXaDataSource(druidXADataSource);
xaDataSource.setUniqueResourceName("mysqlDataSource");
return xaDataSource;
}
@Bean(name = "mysqlSqlSessionFactory")
public SqlSessionFactory sqlSessionFactory(@Qualifier("mysqlDataSource") DataSource dataSource)
throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/dws/*Mapper.xml"));//扫描指定目录的xml
//此处创建一个Configuration 注意包不要引错了
org.apache.ibatis.session.Configuration configuration=new org.apache.ibatis.session.Configuration();
//配置日志实现
configuration.setLogImpl(StdOutImpl.class);
//此处可以添加其他mybatis配置 例如转驼峰命名
configuration.setMapUnderscoreToCamelCase(true);
//bena工厂装载上面配置的Configuration
bean.setConfiguration(configuration);
return bean.getObject();
}
@Bean(name = "mysqlSqlSessionTemplate")
public SqlSessionTemplate sqlSessionTemplate(
@Qualifier("mysqlSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
此处配置后yml不需要再配置
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)