Comments (23)
Original comment by [email protected]
on 17 May 2010 at 3:22
- Added labels: Type-Feature, Priority-Low, Version-Release3.x, Component-SqlMaps
- Removed labels: Type-Enhancement, Priority-Medium
from mybatis.
Original comment by [email protected]
on 2 Jun 2010 at 4:46
- Changed state: Accepted
from mybatis.
I got to this issue searching for the word "Spring" :) I think this could be a
really insteresting feature for building muti-db configurable apps.
But I don´t like the idea to build it at "mybatis-spring" level.
I don´t know well mubatis 3 internals but I wonder if a new attribute
"dataBase" could be added
<mapper namespace="com.acme.banking.persistence.dao.ITransactionTypeDao">
<resultMap id="get-transactionType-result"
type="TransactionType">
<result property="code" column="code"/>
<result property="title" column="title"/>
</resultMap>
<select id="getAll" dataBase="db2 resultType="TransactionType">
SELECT code, title
FROM transaction_type
</select>
<select id="getAll" dataBase="oracle" resultType="TransactionType">
SELECT title, code
FROM transaction_type
</select>
<select id="getAll" resultType="TransactionType">
SELECT title, code
FROM transaction_type
</select>
</mapper>
Then, when a mapper is executed, the proper xml snippet should be used for
execution by inspecting SqlSession inner database metatadata information.
Original comment by eduardo.macarron
on 9 Oct 2010 at 6:02
from mybatis.
The best way to do this is to use a separate set of files and a separate
mapper. It would probably take you an hour to create something to satisfy this
need. In addition, it keeps your mapper files from growing with every database
you add.
Original comment by [email protected]
on 9 Oct 2010 at 2:07
from mybatis.
But in that case you should have to repackage you app or control that by code.
I mean: if you have a MyDB2Mapper and MyOracleMapper both on the same jar you
should control by code which one you call and thats an awful solution.
Otherwise you should have different jars with the sampe mappers DB2Mappers.jar
that holds MyMapper and OracleMappers.jar that holds again MyMapper both
mappers with different xml files or different annotations but the same
signature. This way you should make a new package for each database.
That will indeed work but what Russ Jackson wants to do is to auto-detect with
database are you connecting to and transparently use the right annotation or
the right pice of xml. I would say that there is no way to do that with mappers
unless you make your own version of the binding package (MapperProxy &
MapperMethod).
Original comment by eduardo.macarron
on 16 Oct 2010 at 9:58
from mybatis.
After thinking about that, maybe Spring can do it. It seems easy for mappers:
http://groups.google.com/group/mybatis-user/browse_thread/thread/defa83a5b65cd81
b
This code will load different mapper interfaces based on the datasource
metadata. All them will extend the same mapper interface so business code will
see just one data access layer although there will be as many versions as
databases are supported.
This can also be done in the SqlSessionTemplate (similar to SqlSessionManager),
changing statement names instead of whole mappers.
I don´t know if this should go in MyBatis-Spring or will be better let users
do their extensions.
Original comment by eduardo.macarron
on 17 Nov 2010 at 6:01
from mybatis.
This is a working version of the MapperFactoryBean that can be used if someone
needs this feature.
Original comment by eduardo.macarron
on 21 Nov 2010 at 3:58
Attachments:
from mybatis.
The idea Eduardo suggested is good, but this seems like it's general enough and
useful enough that we would want it to be in base MyBatis, not just the Spring
integration.
What I would really like to see is the following:
1) Same namespace, no matter the DB
2) Same interface, no matter the DB
3) Base mapper xml file for "common" elements
4) Mapper xml for statements that only apply to one DB
In other words, if I have a com.example.mapper.UserMapper interface, I would
expect the namespace to be com.example.mapper.UserMapper. These should not
change just because I am using a different DB. But for different databases, I
think having the namespace define the "base package" is reasonable. For
example, the mapper xml file for MySql would be in
com/example/mapper/mysql/UserMapper and the Oracle mapper xml file would be in
com/example/mapper/oracle/UserMapper.
I think this can be supported very easily with a change to
org.apache.ibatis.builder.annotation.MapperAnnotationBuilder.loadXmlResource().
That function could check the Environment.id and attempt to load a resource
based on the environment id and fallback to the base if not found. This allows
different mapper xml files for different Environment ids but it doesn't allow
merging of multiple xml files.
How big of a change would it be to support merging mapper xml files? Is this
worth doing?
Note, the idea of using the environment id came from this discussion thread:
http://groups.google.com/group/mybatis-user/browse_thread/thread/defa83a5b65cd81
b/9bac913bf96aac9a?lnk=gst&q=properties#9bac913bf96aac9a
In addition to merging xml files, it would also be nice to support the
environment id as a built-in property that could be used for substitution in
the xml file. This would allow minor changes (i.e. things like JDBCType, casts,
function names) to be put into the base xml file using <if> for different
databases.
Original comment by [email protected]
on 21 Nov 2010 at 8:15
from mybatis.
Attached a simple patch that is consistent with Hunter objectives:
1) Same namespace, no matter the DB
2) Same interface, no matter the DB
3) Base mapper xml file for "common" elements
4) Mapper xml for statements that only apply to one DB
The good point about this patch is that is quite easy and small and add almost
no complexity to MyBatis.
Basically it is just adding a databaseId attribute to xml statements. So a
mapper file will look like this:
<select id="select1" resultType="string">
select '1-common' from names
</select>
<select id="select1" databaseId="hsql" resultType="string">
select '1-hsql' from names
</select>
In the case the datasource's vendor is hsql the second statement will be used,
otherwise the first one will be used.
There are three problems with this patch:
1.- It will fail if MyBatis is configured with (for example) an Oracle
datasource and SqlSession is created out of a DB2 connection using the same
SqlSessionFactory. (The user will probably deserve the error he will get ;) )
2.- If there are repeated statements MyBatis will fire an exception depending
on their order. Imagine we are using an Oracle datasource.
This will work:
<select id="ambiguousSelect" databaseId="Oracle" resultType="string">
...
</select>
<select id="ambiguousSelect" resultType="string">
...
</select>
<select id="ambiguousSelect" resultType="string">
...
</select>
but this will fail:
<select id="ambiguousSelect" resultType="string">
...
</select>
<select id="ambiguousSelect" resultType="string">
...
</select>
<select id="ambiguousSelect" databaseId="Oracle" resultType="string">
...
</select>
This is because once the specific vendor statement has been read, all
no-specific vendor statements are ignored.
3.- I will not work with annotated mappers. This just works for xml files.
Original comment by eduardo.macarron
on 21 Jun 2011 at 4:12
Attachments:
from mybatis.
[deleted comment]
from mybatis.
Original comment by eduardo.macarron
on 11 Sep 2011 at 4:04
- Added labels: Target-Release3.1.0
from mybatis.
Hi there, the patch has been applied but the status is still accepted. Does it
mean that it is not production ready ? Thanks
Original comment by [email protected]
on 26 Oct 2011 at 1:09
from mybatis.
Oh, sorry that was wrong. That was removed from 3.0.x relese and should be
available on 3.1. Do you need it?
Original comment by eduardo.macarron
on 26 Oct 2011 at 4:43
from mybatis.
Everybody wants it. It's a killer feature ! When is it planned to release 3.1 ?
Original comment by [email protected]
on 26 Oct 2011 at 4:54
from mybatis.
:) Good! There is not a release date yet, but is not going to be soon.
Original comment by eduardo.macarron
on 26 Oct 2011 at 5:28
from mybatis.
hi, i was creating a select query and found this feature (databaseId) from
http://mybatis.org/dtd/mybatis-3-mapper.dtd... but then using last release
3.0.6 was not working, it was removed from trunk.
why you rollbacked this feature? is great!
thanks
Original comment by [email protected]
on 4 Nov 2011 at 3:49
from mybatis.
Original comment by eduardo.macarron
on 26 Nov 2011 at 6:17
- Changed state: Started
from mybatis.
Original comment by eduardo.macarron
on 25 Dec 2011 at 9:37
- Changed state: Fixed
from mybatis.
does this feature is available in myBatis 3.1 snapshop?
does any doc will present this feature?
Original comment by [email protected]
on 31 Dec 2011 at 2:05
from mybatis.
yes, have a look at www.mybatis.org/core/
Original comment by eduardo.macarron
on 31 Dec 2011 at 2:26
from mybatis.
Here's the direct link...
http://www.mybatis.org/core/configuration.html#databaseIdProvider
Original comment by [email protected]
on 31 Dec 2011 at 2:37
from mybatis.
[deleted comment]
from mybatis.
Original comment by eduardo.macarron
on 17 Jan 2012 at 6:14
from mybatis.
Related Issues (20)
- [mybatis-spring] Mapper scanning should not rely on proprietary @Mapper annotation HOT 1
- Migrations archive unzips to "ibatis-migrations" instead of "mybatis-migrations" HOT 2
- OSGI Support HOT 4
- Remove mapper order restriction when referencing SQL fragment in another file. HOT 40
- Non-existent object id is incorrectly resolved when there is an entry with the same short name. HOT 13
- Allow applying @Transactional to types and interfaces and add a @NoTransaction attribute HOT 4
- Please prioritize issues HOT 1
- Adjust issue's status HOT 1
- Unable to get auto-generated ID with mysql using annotations HOT 1
- Mybatis-spring doesn't propagate exceptions up the stack when using batch mode HOT 17
- how to free temp tablespace allocated for a select stmt return parameter HOT 1
- rollback on BatchExecutor executes batched statements HOT 8
- resultMap doesn't support mapping of unnamed columns by index HOT 2
- mybatis3.0.3 bug in WebService ?? HOT 2
- Annotation-based and AOP-based sessions HOT 3
- Global parameter typeHandler not invoked in case of null values HOT 2
- Exception: Result Maps collection does not contain value for test.User-result HOT 2
- Handling of update counts before result sets in select HOT 8
- configure behavior when all columns values are null for some rows HOT 5
- queryForMap does not use a column as map HOT 14
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mybatis.