mybatisplus动态schema配置

1.场景

查询测试环境数据库与生产环境数据库时,sql中的schema一致

1.1@TableName

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@TableName(value = "role",schema = "test", autoResultMap = true)
public class SysRole {

    @TableId(type = IdType.ASSIGN_ID)
    private String roleId;
    private String deptId;
    private String roleName;
    private String parentId;
    private String createTime;
    private String createUserId;
    private String updateTime;
    private String updateUserId;
    private String roleDesc;
    private String status;

}
@TableName注解详情:注解用来将指定的数据库表和 JavaBean 进行映射。
该注解属性如下:
属性名称属性含义备注
value字符串类型,不是必填,用来指定数据表名称上面实例中,将 role数据表和 SysUserBean 实体进行映射。
shcema 属性用来指定模式名称。如果你使用的是 mysql 数据库,则指定数据库名称。如果你使用的是 oracle,则为 schema上面实例中,schema="test",其中:test就是 oracle 中的 schema。例如:
keepGlobalPrefix
是否保持使用全局的 tablePrefix 的值,如果设置了全局 tablePrefix 且自行设置了 value 的值。
resultMap
对应 Mapper XML 文件中 <resultMap> 标签的 id 属性的值。用法见 “autoResultMap” 属性
autoResultMap
控制是否自动构建 resultMap 并使用它

2.解决方案:

上述方案适合测试与生产环境一致时,如果不一致则需要上线前进行修改

@Bean(name = "sqlSessionFactory")
    public SqlSessionFactory zocrmSqlSessionFactory(@Qualifier("dataSource") DataSource dataSource)
            throws Exception {
         GlobalConfig config = new GlobalConfig();
        //多数据源,自增主键生成配置要单独配
        config.setDbConfig(new GlobalConfig.DbConfig().setKeyGenerators(Collections.singletonList(new OracleKeyGenerator())));
        //多数据源分页拦截器也要单独配置
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.ORACLE));
        MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
        MybatisConfiguration configuration = new MybatisConfiguration();
        configuration.setJdbcTypeForNull(JdbcType.NULL);

        //配置动态schema
        DynamicTableNameInnerInterceptor innerInterceptor = new DynamicTableNameInnerInterceptor();
        innerInterceptor.setTableNameHandler((sql, tableName) -> schema + "." + tableName);
        interceptor.addInnerInterceptor(innerInterceptor);

        bean.setConfiguration(configuration);
        bean.setGlobalConfig(config);
        bean.setPlugins(interceptor);
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/**/*.xml"));
        return bean.getObject();
    }

注:Mapping.selectPage(page, queryWrapper)时不生效

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在yml配置文件中,需要添加如下配置: ``` server: port: 8080 spring: application: name: xxxx datasource: dynamic: primary: pgsql #设置默认的数据源或者数据源组,默认值即为pgsql strict: false #严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使用默认数据源 datasource: pgsql: driver-class-name: org.postgresql.Driver url: jdbc:postgresql://192.168.1.167:5432/aaa?currentSchema=aaa username: aaa password: aaa mysql: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/bbb?useUnicode=true&characterEncoding=utf8 username: bbb password: bbb mybatis-plus: type-aliases-package: com.dms.gateway.api.entity mapper-locations: classpath:/mapper/*Mapper.xml global-config: db-config: id-type: auto field-strategy: not_empty logic-delete-value: 1 logic-not-delete-value: 0 configuration: map-underscore-to-camel-case: true cache-enabled: false call-setters-on-nulls: true log-impl: org.apache.ibatis.logging.stdout.StdOutImpl ``` 其中,需要注意的是,需要在`spring.datasource.dynamic`下设置默认的数据源或者数据源组,即`primary`属性,这里设置为`pgsql`。同时,需要在`spring.datasource.datasource`下配置`pgsql`数据源的相关信息,包括`driver-class-name`、`url`、`username`和`password`等。 另外,需要在`mybatis-plus.global-config`下配置相关的全局配置信息,包括`id-type`、`field-strategy`、`logic-delete-value`、`logic-not-delete-value`等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值