How <selectKey> support multiple DataSource in MyBatis - mybatis

In Mybatis, we use <selectedKey> for generated column when define the insert statement, but for different DB it has different content in selectedKey element.
For MySQL like this:
<selectKey resultType="java.lang.Long" order="AFTER" keyProperty="productId">
SELECT LAST_INSERT_ID()
</selectKey>
For Oracle like this:
<selectKey resultType="java.math.BigDecimal" order="BEFORE" keyProperty="id">
SELECT U_USER_INFO_SEQ.Nextval as ID from DUAL </selectKey>
So how this support multiple DataSource?

You can try using http://www.mybatis.org/mybatis-3/configuration.html#databaseIdProvider.
Doing so allows you to use _databaseId as described http://www.mybatis.org/mybatis-3/dynamic-sql.html#Multi-db_vendor_support

Related

Use criteria or query dsl to find all tables names in a given schema

Is there a way to find all table names that begin with t_ in a given schema name with criteria API or query DSL( or even database metadata)? If it exists, could you please show me how I can do it using a schema name or view? I'm using PostgreSQL for the database.
I don't want to use a native query.
yes, you can use below query:
SELECT table_catalog,table_schema,table_name
FROM information_schema.tables
WHERE table_name LIKE 't\_%'
AND table_type='BASE TABLE' -- to filter out Tables only, remove if you need to see views as well

How to write a select statement for a table in postgres that is inside a schema?

I have a postgres DB and inside of it there are many schemas.
Each one of those schemas contains tables. For example:
Schema Name: personal has tables actions_takes, page_views etc
How can i write a SQL query or ActiveRecord query to query the table inside the schema?
Something like:
select * from actions_takes where user_id = 123;
I can create a model for each table and query it that way, but i want to write a script that passed a user goes over all tables and get the data for that user.
in pgAdmin 4 web console should use double quotation marks like following select statement
SELECT "col1", "col2"
FROM "schemaName".profile;
Point to specific table within a given schema using a dot notation schema.table_name. In your case it translates to
select * from personal.actions_takes where user_id = 123;
For me this query worked : select * from schemaName."Table_Name"

Get all except built-in schema names from a specific database using T-SQL query

As the title entails, is this possible?
Using the following code returns a list of the built-in schemas as well as all the other schemas:
select name from MyDbName.sys.schemas
What I want is only the schemas I created for each table. I also tried doing the following code but its not very solid.
select name from MyDbName.sys.schemas where name not like '%db_%'
select distinct SCHEMA_NAME(schema_id) from sys.objects
I think this will work for you.

Does Mybatis support DDL?

My project is a Database center, like PLSQL but used in a web browser. Sometimes I need to create or alter a table, but I don't know if Mybatis supports DDL, and I haven't found any documents about this.
For the most part DDL works just like DML using mybatis. The one difference is that you will need to use ${} instead of #{} for parameters. Most databases do not support prepared statements with DDL. The $ notation is a string substitution rather than a parameter for a prepared statement.
<update id="exchangePartition" parameterType="java.util.Map">
alter table ${destinationTableName}
exchange partition ${destinationPartitionName}
with table ${sourceTableName}
including indexes
with validation
</update>
It is also helpful to know the call syntax with the statement type callable to invoke stored procedures.
<update id="gatherStatistics" statementType="CALLABLE" parameterType="Map">
{call
dbms_stats.gather_table_stats(
ownname => #{tableOwner},
tabname => #{tableName}
<if test="partitionName != null">
, partname => #{partitionName}
</if>
)
}
</update>
MyBatis supports any native SQL/PlSql commands.
So yes, it supports DDL statements.

How to fetch column names of a table using HQL and populate in a list?

I want to fetch column names of a table using HQL and populate the same in a list?..Please guide. I am using IBM DB2 database.
I have not tested, but you can query the DB2 catalog in order to get the metadata. If you execute the equivalent in HQL, it should work
select colname
from syscat.columns
where tabschema like 'MYSCHEMA%' and tabname = 'MYTABLE'
order by colno