简体   繁体   English

如何使用Oracle DB在Spring / mybatis应用程序中设置默认架构?

[英]How to set default schema in a Spring/mybatis application with Oracle DB?

Coming from a mysql background, I am able to set the default schema name that I want to use for all my sql queries in the connection url. 来自mysql背景,我能够在连接url中设置我想用于所有sql查询的默认模式名称。 I now have an Oracle DB that I need to access. 我现在有一个我需要访问的Oracle数据库。 I am aware that I cannot specify the schema I want to use in the URL since the user is the schema name being used. 我知道我无法指定我想在URL中使用的模式,因为用户是正在使用的模式名称。

I realize that I can use a line of SQL code: 我意识到我可以使用一行SQL代码:

ALTER SESSION SET CURRENT_SCHEMA=default_schema

The project is using mybatis 2.3.5 as my SQL framework, but I am completely new to mybatis. 该项目使用mybatis 2.3.5作为我的SQL框架,但我对mybatis完全不熟悉。 Is there a simple way to configure mybatis to accomplish this? 有没有一种简单的方法来配置mybatis来实现这一目标? My application is a Spring 3 application, so I am using the Spring DataSourceTransactionManager to manage my transactions. 我的应用程序是Spring 3应用程序,因此我使用Spring DataSourceTransactionManager来管理我的事务。 I would presume that the manager must be made aware of this requirement to ensure that the command is sent whenever creating a new connection. 我认为必须让管理员了解这一要求,以确保在创建新连接时发送命令。

I've tried searching online, but most of the examples I find all have the schema names included within the sql queries in the SqlMaps, which I find to be bad practice. 我尝试过在线搜索,但我发现的大多数示例都在SqlMaps中的sql查询中包含了模式名称,我认为这是不好的做法。

In an ideal world, the schema name would be part of the URL such that I can make changes to the schema name for different environments (ex: dev, test, prod, etc) without touching the code (ie: only configured at the JNDI/application server level). 在理想的世界中,模式名称将是URL的一部分,以便我可以更改不同环境的模式名称(例如:dev,test,prod等),而无需触及代码(即:仅在JNDI上配置) /应用程序服务器级别)。 I would be happy if I could use a Spring configuration value to set this as well as I could still use a JNDI lookup or a system environment property to retrieve the value. 如果我可以使用Spring配置值来设置它,我仍然会很高兴我仍然可以使用JNDI查找或系统环境属性来检索值。

Can anyone point me in the right direction? 谁能指出我正确的方向?

Thanks, 谢谢,

Eric 埃里克

As far as I know, there is no option in Oracle to change your URL in order to connect to a specific user schema. 据我所知,Oracle中没有选项可以更改您的URL以连接到特定的用户架构。

1) mybatis: You may set your current schema to a deserved one before you start your operations. 1)mybatis:您可以在开始操作之前将当前架构设置为应得的架构。 You can write your specification in a property file and set your method's arguments from that file. 您可以在属性文件中编写规范,并从该文件设置方法的参数。 You do not need to change your code to change your schema in that case. 在这种情况下,您无需更改代码即可更改架构。

<update id="mySetSchemaMethod" parameterClass="String">
ALTER SESSION SET CURRENT_SCHEMA = ${schemaName}
</update>

2) trigger: If you are using this connection only for this particular java application, you can set a client event trigger so set your CURRENT_SCHEMA. 2)触发器:如果仅对此特定Java应用程序使用此连接,则可以设置客户端事件触发器,以便设置CURRENT_SCHEMA。 This time, you need to change the trigger in order to manage test/prod changes. 这次,您需要更改触发器以管理测试/产品更改。

CREATE OR REPLACE TRIGGER Set_Schema_On_Logon
  AFTER LOGON  
  ON MY_SCHEMA  
BEGIN  
  ALTER SESSION SET CURRENT_SCHEMA = MY_TEST_SCHEMA;  
END;  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Spring Boot 应用程序中设置 oracle db 连接超时 - How to set oracle db connection timeout in spring Boot application Spring Boot - 如何为 PostgreSQL 设置默认架构? - Spring Boot - How to set the default schema for PostgreSQL? 如何在Spring Boot Jdbc中为MySQL数据库指定特定的默认架构 - How to specific default schema for MySQL db in Spring Boot Jdbc 如何在 Spring Boot 应用程序的 application.properties 文件中设置 MyBatis 配置属性? - How do I set MyBatis configuration properties in an application.properties file in a Spring Boot application? 如何为 Spring Data JPA 查询设置默认模式名称? - How to set default schema name for Spring Data JPA queries? 使用MyBatis和Spring进行Oracle数据库轮询 - Oracle database polling with MyBatis and Spring 如何在 MyBatis 中获取从 Oracle 触发器创建的 ID - Spring Boot - How to get ID created from Oracle trigger in MyBatis - Spring Boot 如何使用MyBatis和Spring集成将unicode插入Oracle NVARCHAR - How to insert unicode to Oracle NVARCHAR using MyBatis with Spring integration Spring myBatis强制将ZonedDateTime保存在系统默认时区中,如何防止? - Spring myBatis forcing to save ZonedDateTime in system default timezone, how to prevent? 如何为 spring 引导应用程序上的数据库连接设置 TCP NODELAY - How to set TCP NODELAY for db connections on a spring boot application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM