以下是我通过自己试验得出的结论,不敢保证准确性,如有错误希望指出,谢谢!
1、如果启动类没有禁用数据库自动配置,则只需要在allpication.properties进行数据库配置就可以了,我认为这时应该使用的是默认的数据源:
@EnableSwagger2
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
#数据库配置
spring.datasource.driver-class-name= com.mysql.jdbc.Driver
spring.datasource.url= jdbc:mysql://localhost:3306/test
spring.datasource.username= root
spring.datasource.password= root
mybatis.mapper-locations=classpath:mapper/*.xml
注意:此处如果不在application.properties中做数据库配置,启动时则会报未指定属性的错,如:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
2、如果启动类禁用了数据库自动配置,则需要手动配置数据源,否则会报下面的错:
Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required
据说原因是mybatis在某个版本后取消了sqlSessionFactory的自动注入,需要显示注入,就要设置dataSource,所以dataSource也要显示声明(druidDataSource:德鲁伊,一种数据源类型),代码如下:
package com.example.demo.config;
import com.alibaba.druid.pool.DruidDataSource;
import com.github.pagehelper.PageInterceptor;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.*;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
@Configuration
@MapperScan(basePackages = {"com.example.demo.mapper"})
//@PropertySource("classpath:jdbc.properties")
public class DBConfig {
@Value("${spring.datasource.url}")
private String dbUrl;
@Value("${spring.datasource.username}")
private String dbUserName;
@Value("${spring.datasource.password}")
private String dbPassword;
@Value("${spring.datasource.driver-class-name}")
private String dbDriverName;
@Value("${mybatis.mapper-locations}")
private String mapperXMLLocation;
@Primary
@Bean("druidDataSource")
public DataSource getDataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl(dbUrl);
dataSource.setUsername(dbUserName);
dataSource.setPassword(dbPassword);
dataSource.setDriverClassName(dbDriverName);
dataSource.setMaxActive(50);
return dataSource;
}
@Bean
public SqlSessionFactory sqlSessionFactoryBean(DataSource dataSource) {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
//添加XML目录
ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
try {
//读取多个文件下的xml文件
List<Resource> resources = new ArrayList<>();
Resource[] resource = resourcePatternResolver.getResources(mapperXMLLocation);
resources.addAll(Arrays.asList(resource));
bean.setMapperLocations(resources.toArray(new Resource[]{})); //这里这样写是为了可以添加多个xmllocation
//bean.setMapperLocations(resource);
return bean.getObject();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
注:此处的@MapperScan也可以加到启动类中,只要能扫描到就可以了。