Quantcast

How can I execute sql directly without xml and annotation?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

How can I execute sql directly without xml and annotation?

Joey
Hi ,

I'm working on a project, the SQL has to  be dynamically generated  in runtime by Java code .
So, we choose "SelectBuilder" and "SqlBuilder" .

After the SQL string was generated, how can I execute it? Is "SqlRunner" recommended?

How about the params? How to bound them into the dynamically generated SQL ?

----------for example-----------------------------------------------------
Dynamically Generated SQL String:
    "Select * from t_usr where sex=${sex} and age=${age} "
PARAM:
    {{sex:female},{age:26}}

How can I execute it without xml and annotation?
------------------------------------------------------------------------------


In another way,
How can I init the SQL mapping information in java code instead of in xml and annotation?
If it's supported by MyBatis 3, I will init the sql mapping after the SQL is generated, so I can choose "SqlSession" to execute the SQL.

Thanks!
 
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: How can I execute sql directly without xml and annotation?

Jeff Butler
No XML and no annotations means no MyBatis.  Plain and simple - use JDBC.

If you are open to some minimal annotations, MyBatis supports
dynamically generated SQL in Java through the @SelectProvider
annotation.  Check it out - it's probably close to what you want.
SelectBuilder and SqlBuilder were designed to work with
@SelectProvider annotations.

Jeff Butler


On Tue, Feb 7, 2012 at 10:03 AM, Joey <[hidden email]> wrote:

> Hi ,
>
> I'm working on a project, the SQL has to  be dynamically generated  in
> runtime by Java code .
> So, we choose "SelectBuilder" and "SqlBuilder" .
>
> After the SQL string was generated, how can I execute it? Is "SqlRunner"
> recommended?
>
> How about the params? How to bound them into the dynamically generated SQL ?
>
> ----------for example-----------------------------------------------------
> Dynamically Generated SQL String:
>    "Select * from t_usr where sex=${sex} and age=${age} "
> PARAM:
>    {{sex:female},{age:26}}
>
> How can I execute it without xml and annotation?
> ------------------------------------------------------------------------------
>
>
> In another way,
> How can I init the SQL mapping information in java code instead of in xml
> and annotation?
> If it's supported by MyBatis 3, I will init the sql mapping after the SQL is
> generated, so I can choose "SqlSession" to execute the SQL.
>
> Thanks!
>
>
> --
> View this message in context: http://mybatis-user.963551.n3.nabble.com/How-can-I-execute-sql-directly-without-xml-and-annotation-tp3722765p3722765.html
> Sent from the mybatis-user mailing list archive at Nabble.com.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: How can I execute sql directly without xml and annotation?

Joey
This post was updated on .
Hi Jeff,

Thanks for your reply!

So, the package "org.apache.ibatis.jdbc" is designed for the MyBatis Internal use only, not for the user of MyBatis ?

The annotations of MyBatis depend on the specific functions, Such as : saveCar(Car car), saveDriver(Driver driver).
But in our project, what we are working for is a "Generic object persistence framework".
For the specific bean-definition classes, we hope that we can save the instances of them by a single saveObject(Object object) function.
So, the annotations of MyBatis are not close to what we want.

Because we have to use MyBatis XML mapping on other scene of this project, so we were looking for the solution with MyBatis.

Maybe you're right, JDBC may be the better choice.

Thank you !

Joey


Jeff Butler wrote
No XML and no annotations means no MyBatis.  Plain and simple - use JDBC.

If you are open to some minimal annotations, MyBatis supports
dynamically generated SQL in Java through the @SelectProvider
annotation.  Check it out - it's probably close to what you want.
SelectBuilder and SqlBuilder were designed to work with
@SelectProvider annotations.

Jeff Butler
Quoted from:
http://mybatis-user.963551.n3.nabble.com/How-can-I-execute-sql-directly-without-xml-and-annotation-tp3722765p3724726.html
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: How can I execute sql directly without xml and annotation?

Joey
This post was updated on .
In reply to this post by Jeff Butler
Your idea is really great!
A demo has been built, and it seems to work well.

The only trouble is that, we have to get the query result as a map instead of an object.
The return type of the query interface can only be Map or List<Map> , but cann't be Object or List< Object>, because Object is not a specific Class definition.
I don't know whether we could use a dynamic resultType.

We plan to deal with the conversion between Object and map ourselves.
It means that we will have to give up the object-reflection feature of MyBatis.

The solution has been very close to what we want.
Thanks!

Joey

Jeff Butler wrote
No package in MyBatis is "off limits". If something works for you,
then certainly feel free to use it.

You could write an annotated method like this:

@SelectProvider(type=GenericQueryProvider.class, method="genericQuery")
List<Map> genericQuery(Object parms)

Then your select provider could write and execute any query and return
any result set. I'm not sure how helpful this is in practice, but it
would allow you to hook into the MyBatis framework if it important to
do that.

You can do something similar with XML mapping too, although parameter
mapping would be better in the annotated method.

Jeff Butler
Quoted from:  mail
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: How can I execute sql directly without xml and annotation?

Eduardo Macarron


> The only trouble is that, we have to get the query result as a map instead
> of an object.

Just set a return type map and call selectOne or if using mapper
interfaces declare Map as a return method.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: How can I execute sql directly without xml and annotation?

Joey
Do you mean "@ResultMap"?
Could you kindly give me a further support?

Base on the following code:
    public interface ObjectMapper
    {
        @SelectProvider(type = SqlProvider.class, method = "getObject")
        public Map getObject(Map<String, Object> paras);
    }
How can I declare the return type as a method?

Thank you!

Eduardo wrote
Just set a return type map and call selectOne or if using mapper
interfaces declare Map as a return method.
Loading...