Quering using String sql statement

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Quering using String sql statement

Mansour
Hello all,


I am trying to use query methods in SqlSession to query some tables. For example:

String sql = "SELECT * FROM USERS" ;
List<MyEntity> entities = this.sqlSession.selectList(sql) ;

Unfortunately this does not work, as need to define the SQL statement in the XML mapping file, and call the query using:

List<MyEntity> entities = this.sqlSession.selectList("id_of_query_we_need") ;

Basically, we are more comforable using SQL statement builder described in http://www.mybatis.org/mybatis-3/statement-builders.html

What methods executes the given SQL statements and provide the results map without using annotation magic ?
And example will be highly appreciated.


Thank you.


--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Quering using String sql statement

Guy Rouillier-2
In the MyBatis source code, take a look at the classes in src/test/java/org/apache/ibatis/jdbc and src/test/java/org/apache/ibatis/binding.  The examples should give you some ideas.  See, for example, BoundBlogMapper.java under binding, the method selectBlogsUsingProvider();  I've never used these classes, but the SQL class is meant to be used with (e.g.) SelectProvider.  It just allows you to generate the SQL statement in Java code rather than in XML or an annotation.  You still use the rest of the MyBatis framework; otherwise, why bother using MyBatis at all, just use straight JDBC.

--
Guy Rouillier

------ Original Message ------
From: "Mansour" <[hidden email]>
To: "mybatis-user" <[hidden email]>
Sent: 11/13/2018 12:45:41 PM
Subject: Quering using String sql statement

Hello all,


I am trying to use query methods in SqlSession to query some tables. For example:

String sql = "SELECT * FROM USERS" ;
List<MyEntity> entities = this.sqlSession.selectList(sql) ;

Unfortunately this does not work, as need to define the SQL statement in the XML mapping file, and call the query using:

List<MyEntity> entities = this.sqlSession.selectList("id_of_query_we_need") ;

Basically, we are more comforable using SQL statement builder described in http://www.mybatis.org/mybatis-3/statement-builders.html

What methods executes the given SQL statements and provide the results map without using annotation magic ?
And example will be highly appreciated.


Thank you.


--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.