MyBatis using annotations instead of XML configuration to generate SQL dynamically

Source: Internet
Author: User

MyBatis when using annotations instead of XML configuration, it is difficult to find out if the criteria are null or empty @Select.

Mybatis3 adds a new feature that uses annotations to configure Mapper, and uses Selectprovider to dynamically generate SQL.

Typical usage scenarios

1. No parameter @selectprovide method
There are no parameters on the Mapper interface method and on the @selectprovide specified class method:
Usermapper.java:

1 @SelectProvider (type = Sqlprovider.class, method = "Selectalluser")
2 @ResultMap ("UserMap")
3 public list<user> getalluser (); Sqlprovider.java:1 public String Selectalluser () {
2 Return "SELECT * from user";
3} 2. Single parameter using @selectprovider

2.1. Single parameter without @param annotations use @selectprovider

@SelectProvider is declared in the method essentially, this method is defined on the interface corresponding to the mapper.
1 public interface Usermapper {
2 @SelectProvider (type = Sqlprovider.class, method = "Selectuser")
3 @ResultMap ("UserMap")
4 Public User GetUser (long userId);
5} The above example is a simple mapper interface that defines a method: GetUser, which queries the user information based on the provided user ID, and returns a single user entity Bean.
This is a very simple and common query scenario: Querying records based on key and encapsulating the results as entity beans. which
@SelectProvider annotations are used to generate SQL statements for queries, as opposed to @select annotations, @SelectProvide specify a class and its methods, and obtain SQL statements by calling this method on class. In our example, the way to get a query for SQL is sqlprovider.selectuser.
@ResultMap annotations are used to fetch data from the query result set recordset and then assemble the entity bean.
The class class specified by the type parameter in the @SelectProvide must be able to be initialized with an argument-free constructor.
Methods specified by the method parameter in the @SelectProvide must be public, the return value must be string, and can be static. 1 public class SQLProvider {
2 public String Selectuser (long userId) {
3 Return "SELECT * from user where userid=" + userId;
4}
5}

2.2. @selectprovide method with @param annotation for one parameter
For cases where there is only one parameter, you can use it directly, see GetUser and Selectuser above.
However, if the @param annotation is used for the UserID method in the GetUser method, then the corresponding Selectuser method must accept the map<string and object> as the parameter:
Usermapper.java:

1 @SelectProvider (type = Sqlprovider.class, method = "SelectUser2")
2 @ResultMap ("UserMap")
3 Public User GetUser2 (@Param ("userid") long userId);

Sqlprovider.java:

1 public String SelectUser2 (map<string, object> para) {
2 Return "SELECT * from user where userid=" + para.get ("userId");
3}


3. Multi-Parameter @selectprovide method
In the case of more than one parameter, the @SelectProvide method must accept Map<string, object> as a parameter,
If the parameter uses the @param annotation, then the parameter in map with the value of @param is key, the following example of the UserID;
If the parameter does not use the @param annotation, then the parameter is key in the order of the parameters in the map, as in the following example password:

Usermapper.java:1 @SelectProvider (type = Sqlprovider.class, method = "Selectusercheck")
2 @ResultMap ("UserMap")
3 Public User Getusercheck (@Param ("userid") long userId, String password); Sqlprovider.java:
1 public String Selectusercheck (map<string, object> para) {
2 Return "SELECT * from user where userid=" + para.get ("userId") + "and password= '" + para.get ("1") + "'";
3}
4. Some limitations
In the Mapper interface and the @selectprovide method class, do not use overloading, that is, do not use methods with the same method name parameters, to avoid the occurrence of a strange problem. References: "1" http://www.blogjava.net/dbstar/archive/2011/08/08/355825.html

MyBatis using annotations instead of XML configuration to generate SQL dynamically

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.