Search in sources :

Example 1 with DbType

use of com.baomidou.mybatisplus.annotation.DbType in project diboot by dibo-software.

the class ContextHelper method getDatabaseType.

/**
 * 获取数据库类型
 * @return
 */
public static String getDatabaseType() {
    if (DATABASE_TYPE != null) {
        return DATABASE_TYPE;
    }
    String jdbcUrl = getJdbcUrl();
    if (jdbcUrl != null) {
        DbType dbType = JdbcUtils.getDbType(jdbcUrl);
        DATABASE_TYPE = dbType.getDb();
        if (DATABASE_TYPE.startsWith(DbType.SQL_SERVER.getDb())) {
            DATABASE_TYPE = DbType.SQL_SERVER.getDb();
        }
    } else {
        SqlSessionFactory sqlSessionFactory = getBean(SqlSessionFactory.class);
        if (sqlSessionFactory != null) {
            DATABASE_TYPE = sqlSessionFactory.getConfiguration().getDatabaseId();
        }
    }
    if (DATABASE_TYPE == null) {
        log.warn("无法识别数据库类型,请检查数据源配置:spring.datasource.url等");
    }
    return DATABASE_TYPE;
}
Also used : SqlSessionFactory(org.apache.ibatis.session.SqlSessionFactory) DbType(com.baomidou.mybatisplus.annotation.DbType)

Example 2 with DbType

use of com.baomidou.mybatisplus.annotation.DbType in project chao-cloud by chaojunzi.

the class MybatisUtil method getOne.

/**
 * 获取一条数据;<br>
 * 注:<br>
 * 1.请注入 @EnableMybatisPlusConfig <br>
 * 2.目前只支持一种数据源类型 <br>
 * 3.oracle|mysql
 *
 * @param <T>     数据实体
 * @param service 实体对应的service层
 * @param wrapper 封装条件
 * @return 实体
 */
static <T> T getOne(IService<T> service, AbstractWrapper<T, ?, ?> wrapper) {
    // 判断方言
    String dbType = SpringUtil.getProperty(Constants.MYBATIS_PLUS + ".db-type");
    DbType type = DbType.getDbType(dbType);
    switch(type) {
        case ORACLE:
            wrapper.apply(MybatisConstant.LIMIT_1_ORACLE);
            break;
        case MYSQL:
        default:
            // 默认mysql
            wrapper.last(MybatisConstant.LIMIT_1_MYSQL);
            break;
    }
    return service.getOne(wrapper);
}
Also used : DbType(com.baomidou.mybatisplus.annotation.DbType)

Example 3 with DbType

use of com.baomidou.mybatisplus.annotation.DbType in project chao-cloud by chaojunzi.

the class DateTableNameHandler method getTableNodesComplete.

private TableNodesComplete getTableNodesComplete() {
    Map<String, TableNodesComplete> beans = SpringUtil.getBeansOfType(TableNodesComplete.class);
    Assert.notNull(beans, "请注入一个生成表的实现类:{}", TableNodesComplete.class.getSimpleName());
    DbType dbType = rule.getDbType();
    TableNodesComplete complete = CollUtil.findOne(beans.values(), b -> b.getDbType() == dbType);
    Assert.notNull(complete, "未找到该数据库表生成插件:{}", dbType);
    return complete;
}
Also used : DbType(com.baomidou.mybatisplus.annotation.DbType) TableNodesComplete(com.chao.cloud.common.extra.mybatis.plugin.TableNodesComplete)

Example 4 with DbType

use of com.baomidou.mybatisplus.annotation.DbType in project mybatis-plus-samples by baomidou.

the class MpConfig method globalConfiguration.

@Bean
public GlobalConfig globalConfiguration() {
    GlobalConfig conf = new GlobalConfig();
    conf.setDbConfig(new GlobalConfig.DbConfig().setKeyGenerators(Arrays.asList(// h2 1.x 的写法(默认 2.x 的写法)
    new IKeyGenerator() {

        @Override
        public String executeSql(String incrementerName) {
            return "select " + incrementerName + ".nextval";
        }

        @Override
        public DbType dbType() {
            return DbType.POSTGRE_SQL;
        }
    })));
    return conf;
}
Also used : GlobalConfig(com.baomidou.mybatisplus.core.config.GlobalConfig) IKeyGenerator(com.baomidou.mybatisplus.core.incrementer.IKeyGenerator) DbType(com.baomidou.mybatisplus.annotation.DbType) Bean(org.springframework.context.annotation.Bean)

Example 5 with DbType

use of com.baomidou.mybatisplus.annotation.DbType in project chao-cloud by chaojunzi.

the class ShardingActualNodesComplete method sourceOfTableName.

/**
 * 根据表名生成表结构<br>
 * 格式: table:ds.table_202001
 *
 * @param shardingDataSource 数据源
 * @param tableNodes         源表_节点
 * @param defaultDsName      默认数据源
 */
default void sourceOfTableName(ShardingDataSource shardingDataSource, Map<String, List<String>> tableNodes, String defaultDsName) {
    DbType type = DbType.getDbType(shardingDataSource.getDatabaseType().getName());
    switch(type) {
        case // 目前只支持mysql
        MYSQL:
            Map<String, DataSource> dsMap = shardingDataSource.getDataSourceMap();
            // 节点分组
            Map<String, Map<String, List<String>>> dsTableNodes = build(tableNodes);
            // 生成表结构
            dsTableNodes.forEach((dsName, tNodesMap) -> {
                DataSource ds = dsMap.get(dsName);
                List<String> tables = MetaUtil.getTables(ds);
                // 生成数据表
                tNodesMap.forEach((sourceTable, nodes) -> {
                    // 整合需要缓存表节点
                    cacheTableNodes(dsName, sourceTable, nodes, tables);
                    // 需要生成的表
                    Collection<String> needTables = CollUtil.subtract(nodes, tables);
                    // 生成表节点
                    if (CollUtil.isEmpty(needTables)) {
                        return;
                    }
                    String sourceTableDDL = getSourceTableDDL(dsMap.get(defaultDsName), sourceTable);
                    createTable(sourceTable, sourceTableDDL, dsName, ds, needTables);
                });
            });
        default:
            break;
    }
}
Also used : Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) DbType(com.baomidou.mybatisplus.annotation.DbType) ShardingDataSource(org.apache.shardingsphere.shardingjdbc.jdbc.core.datasource.ShardingDataSource) DataSource(javax.sql.DataSource)

Aggregations

DbType (com.baomidou.mybatisplus.annotation.DbType)9 GlobalConfig (com.baomidou.mybatisplus.core.config.GlobalConfig)2 IKeyGenerator (com.baomidou.mybatisplus.core.incrementer.IKeyGenerator)2 ColumnInfo (com.hccake.ballcat.codegen.model.bo.ColumnInfo)2 DataSource (javax.sql.DataSource)2 Bean (org.springframework.context.annotation.Bean)2 DynamicRoutingDataSource (com.baomidou.dynamic.datasource.DynamicRoutingDataSource)1 TableNodesComplete (com.chao.cloud.common.extra.mybatis.plugin.TableNodesComplete)1 DbTypeConverter (com.hccake.ballcat.codegen.database.DbTypeConverter)1 IColumnType (com.hccake.ballcat.codegen.database.IColumnType)1 TableInfoMapper (com.hccake.ballcat.codegen.database.TableInfoMapper)1 ColumnProperties (com.hccake.ballcat.codegen.model.bo.ColumnProperties)1 GenerateProperties (com.hccake.ballcat.codegen.model.bo.GenerateProperties)1 TableDetails (com.hccake.ballcat.codegen.model.bo.TableDetails)1 TableInfo (com.hccake.ballcat.codegen.model.bo.TableInfo)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 SqlSessionFactory (org.apache.ibatis.session.SqlSessionFactory)1 ShardingDataSource (org.apache.shardingsphere.shardingjdbc.jdbc.core.datasource.ShardingDataSource)1