|
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! |
|
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. |
|
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 Quoted from: http://mybatis-user.963551.n3.nabble.com/How-can-I-execute-sql-directly-without-xml-and-annotation-tp3722765p3724726.html |
|
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 Quoted from: mail |
|
> 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. |
|
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!
|
| Powered by Nabble | Edit this page |
