mybatis-plus自定义数据源

wuchangjian2021-11-09 15:25:46编程学习
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.extension.incrementer.OracleKeyGenerator;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

@Configuration
@MapperScan(
        basePackages = {"com.xxx.mapper"},
        sqlSessionTemplateRef = "mySqlSessionTemplate"
)
public class DataSourceConfig {
    public static final String MAPPER_LOCATION_PATTERN = "classpath:mapper/*.xml";

    @Bean
    public DataSourceTransactionManager myTransactionManager(@Autowired DataSource myDataSource){
        return new DataSourceTransactionManager(myDataSource);
    }

    @Bean
    public SqlSessionFactory mySqlSessionFactory(@Autowired DataSource myDataSource) throws Exception{
        MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
        bean.setDataSource(myDataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MAPPER_LOCATION_PATTERN));
        // 全局配置
        bean.setGlobalConfig(globalConfig());
        // 配置打印sql语句
        MybatisConfiguration configuration = new MybatisConfiguration();
        configuration.setLogImpl(StdOutImpl.class);

        return bean.getObject();
    }

    @Bean
    public GlobalConfig globalConfig(){
        GlobalConfig config = new GlobalConfig();
        GlobalConfig.DbConfig dbConfig = new GlobalConfig.DbConfig();
        dbConfig.setKeyGenerator(new OracleKeyGenerator());
        dbConfig.setIdType(IdType.values()[1]);
        config.setDbConfig(dbConfig);
        // 全局自动填充配置
        //config.setMetaObjectHandler(new CustomMetaObjectHandler());

        return config;
    }
}

相关文章

Vue3新特性体验--中(Composition API)

Vue3中的Composition API主要作用是便于整合代码,将实现相...

学习前端CSS的第七天

1.块元素     常见的块元素有<h1>~<h6>,<...

21 天好习惯”第一期-5

   今天学了函数递归,短短的代码可以做很多事情,压缩...

使用java8的lambda将list转为map(转)

常用方式 代码如下: public Map<Long, Stri...

发表评论    

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。