美文网首页
mybatis plus 整合SqlSessionFactory

mybatis plus 整合SqlSessionFactory

作者: thinking2019 | 来源:发表于2020-05-20 11:28 被阅读0次
        @Bean("sqlSessionFactory")
        public SqlSessionFactory sqlSessionFactory(com.baomidou.mybatisplus.core.handlers.MetaObjectHandler metaObjectHandler, PaginationInterceptor paginationInterceptor) throws Exception {
            MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
            // 配置多数据源
            sqlSessionFactory.setDataSource(myRoutingDataSource);
            sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/*.xml"));
            // 实体扫描,多个package用逗号或者分号分隔
            sqlSessionFactory.setTypeAliasesPackage("org.community.mall.po");
    
            GlobalConfig.DbConfig dbConfig = new GlobalConfig.DbConfig();
            // 主键类型  AUTO:"数据库ID自增", INPUT:"用户输入ID",ID_WORKER:"全局唯一ID (数字类型唯一ID)", UUID:"全局唯一ID UUID";
            dbConfig.setIdType(IdType.AUTO);
            // 字段策略 IGNORED:"忽略判断",NOT_NULL:"非 NULL 判断"),NOT_EMPTY:"非空判断"
            dbConfig.setFieldStrategy(FieldStrategy.NOT_NULL);
            // 数据库大写下划线转换
            dbConfig.setCapitalMode(true);
            // 逻辑删除配置
            dbConfig.setLogicDeleteValue("1");
            dbConfig.setLogicNotDeleteValue("0");
            dbConfig.setDbType(DbType.MYSQL);
            // 表前缀
            dbConfig.setTablePrefix("t_");
    
            // 全局配置
            GlobalConfig globalConfig = new GlobalConfig();
            //#刷新mapper 调试神器
            globalConfig.setDbConfig(dbConfig);
            globalConfig.setMetaObjectHandler(metaObjectHandler);
            sqlSessionFactory.setGlobalConfig(globalConfig);
            // mybatis
            MybatisConfiguration configuration = new MybatisConfiguration();
            configuration.setMapUnderscoreToCamelCase(true);
            configuration.setCacheEnabled(false);
            configuration.setCallSettersOnNulls(true);
            sqlSessionFactory.setConfiguration(configuration);
            //  添加分页功能
            sqlSessionFactory.setPlugins(new Interceptor[]{
                    paginationInterceptor
            });
            return sqlSessionFactory.getObject();
        }
    

    相关文章

      网友评论

          本文标题:mybatis plus 整合SqlSessionFactory

          本文链接:https://www.haomeiwen.com/subject/amygohtx.html