Spring boot配置数据源

以下是我通过自己试验得出的结论,不敢保证准确性,如有错误希望指出,谢谢!

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也可以加到启动类中,只要能扫描到就可以了。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值