Ibatis multiple parameters

Source: Internet
Author: User
When multiple parameters are entered in ibatis, you can find that only one of the input parameters is allowed. Therefore, when multiple input parameters are required, you need to find a solution, I see the following two better methods to solve this problem:
1) Use string instead
<Select id = "checklogin" parameterclass = "Java. Lang. String" resultclass = "Java. Lang. Integer">
Select count (*) as value from userinfo where $ SQL $
</SELECT>
For example, in the map statement code above, set the input parameter to the string type, and directly use this string in the SELECT statement, therefore, you can manually implant the parameters to be matched in the Java program code.

String SQL = "uid = '" + username + "' and Pwd = '" + password + "'";
Integer r = (integer) sqlmap. queryforobject ("checklogin", SQL );

This method is simple, but it has many drawbacks. First, you must manually write the SQL statement code in Java. This is against the nature of ibatis to separate the operations at the data layer from the business logic operations. Second, this method may be exploited to cause SQL Injection problems. For example, add a sentence at the end of the SQL statement; drop some table. The results are obvious and catastrophic.

2) Use Map
<Select id = "checklogin2" parameterclass = "Java. util. Map" resultclass = "Java. Lang. Integer">
Select count (*) as value from userinfo where uid = # uid # And Pwd = # PWD #
</SELECT>

This method is much clearer, and you do not need to manually write SQL into Java.
Map map = new hashmap ();
Map. Put ("uid", username );
Map. Put ("PWD", password );
Integer r = (integer) sqlmap. queryforobject ("checklogin2", MAP );

Generate the required map in Java and pass it in as an input parameter. This method should be good and recommended.

3) Some people use the method of compiling JavaBean as needed, but the disadvantage of this method is that if your logical operation involves a lot of combinations of different attributes, you will waste a lot of resources to generate various beans.

Ibatis multiple parameters

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.